-
基于单片机串口通信的上位机和下位机实践
串口是计算机上一种非常通用设备通信的协议(不要与通用串行总线
Universal Serial
Bus
或者
USB
混淆)
。
p>
大多数计算机包含两个基于
RS232
的串
口。
串口同时也是仪器仪表设备通
用的通信协议;很多
GPIB
兼容的设备也带有
RS-232
口。同时,串口通信协议也可以用于获取
远程采集设备的数据。
串口通
信的概念非常简单,串口按位(
bit
)发送和接收字节。尽管
比按字节(
byte
)的并
行通信慢,
但是串口可以在使用一根线发送数据的同时用另一根线接收数据。
它很简单并且能够
实现远距离通信。比如
IEEE488
p>
定义并行通行状态时,规定设备线总常不得超过
20
米,并且任
意两个设备间的长度不得超过
2
米;而对于串口而言,长度可达
1200
米。
p>
首先亮出
C#
的源程序吧。
主要界面:
只是作为简单的运用,可以扩展的。
源代码:
using System;
using c;
using
entModel;
using
using g;
using
using
using
using
namespace
单片机功能控制
{
public partial
class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SerialPort sp =
new SerialPort();
private void
button1_Click(object sender, EventArgs e)
{
String str1 =
;//
串口号
String str2 =
;//
波特率
String str3 =
;//
校验位
String str4 =
;//
停止位
String str5 =
;//
数据位
Int32 int2 =
32(str2);//
将字符串转为整型
Int32 int5 =
32(str5);//
将字符串转为整型
d =
true;//LED
控制界面变可选
try
{
晩
?
畢瑴湯
?
敔瑸
??
打
开串口
)
{
if (str1 == null)
{
敍獳条
?
硯匮潨
?
请
先选择串口!
, Error);
return;
}
();
sp = new SerialPort();
me =
;//
串口编号
te = int2;//
波特率
switch
(str4)//
停止位
{
case :
ts =
break;
case .5:
ts
= ntFive;
break;
case :
ts =
break;
default:
(Error
:参数不正确
,
Error);
break;
}
switch (str3)
{
case NONE:
= break;
case ODD:
=
; break;
case EVEN:
= break;
default:
(Error
:参数不正确
,
Error);
break;
}
ts =
int5;//
数据位
= //
设置串口属性
();//
打开串口
畢瑴湯
?
敔
瑸
?
尠关闭串口
;
整瑸潂
?
吮
硥
??
潃癮牥
?
潔瑓楲杮猨
?
潐瑲慎敭
??
已开启!
;
}
else
{
();
p>
畢瑴湯
?
敔瑸
?<
/p>
尠打开串口
;
d =
false;//LED
控制界面变灰色
p>
整瑸潂
?
吮硥
??
潃癮牥
?
潔瑓楲杮猨
< br>?
潐瑲慎敭
??
已关闭!
;
}
}
catch
(Exception er)
{
(Error: + e, Error);
return;
}
}
private void
Form1_Load(object sender, EventArgs e)
{
初始化
//
整瑸潂
?
吮硥
??
欢
迎使用简易的串口助手!
;
d =
false;//LED
控制界面变灰色
d = false;
d = false;
d =
false;
d = false;
d = false;
();
try
{
foreach (string
com in tNames())
串行口名称
(com);
//
默认设置
p>
选择第一个
com
口
edIndex = 0;//
4800
edIndex = 4;//
波特率
NONE edIndex =
0;//
校验位
1
edIndex = 0;//
停止位为
8 edIndex =
0;//
数据位为
}
catch
{
);
串口调试助手
敍獳条
?
硯匮潨
?
找不到通讯端口!
?
尠
}
}
private void timer1_Tick(object sender,
EventArgs e)
{
= ng();
}
private void button2_Click(object
sender, EventArgs e)
{
try {
)
晩<
/p>
?
畢瑴湯
?
敔瑸
??
开启
{
d = true;
d = false;
//
获取
自动
d = false;
d = false;
d =
false;
d = false;
d = false;
d = false;
d = false;
d =
false;
d = false;
d = false;
d = false;
d =
true;
=
畢瑴湯
?
敔
瑸
?
尠关闭
;
}
else
{
d = false;
d = false;
畢瑴湯
?
敔瑸
?
尠开
启
;
=
}
}
catch (Exception er)
{
(Error: + e,
Error);
return;
}
}
private void
button3_Click(object sender, EventArgs e)
{
d =
true;
慬敢
?<
/p>
吮硥
??
已发送
;
if ( == )
p>
敍獳条
?
硯匮潨
?
发送失败,请选择发送的数据!
);
else
ine();//
往串口写数据
}
private void
checkBox1_CheckedChanged(object sender, EventArgs
e)
{
try
{
if (d)
{
d = true;
d = false;
d = false;
d = false;
d =
false;
d = false;
d = false;
d = false;
慬敢<
/p>
?
吮硥
??
准备
发送
;
=
}
}
catch (Exception er)
{
(Error: + e, Error);
return;
}
}
private void
checkBox2_CheckedChanged(object sender, EventArgs
e)
{
try {
if (d)
{
d = false;
d = true;
d =
false;
d = false;
d = false;
d = false;
d =
false;
d = false;
慬敢
?
吮硥
??
准备发送
;
=
d = false;
d =
false;
d = false;
d = false;
}
}
catch (Exception er)
{
(Error: + e,
Error);
return;
}
}
private void
checkBox3_CheckedChanged(object sender, EventArgs
e)
{
try
{
if
(d)
{
d = false;
d =
false;
d = true;
d = false;
d = false;
d =
false;
d = false;
d = false;
d = false;
d =
false;
d = false;
d = false;
p>
慬敢
?
吮硥
??<
/p>
准备发送
;
=
;
}
}
catch (Exception er)
{
(Error: + e,
Error);
return;
}
}
private void
checkBox4_CheckedChanged(object sender, EventArgs
e)
{
try
{
if
(d)
{
d = false;
d =
false;
d = false;
d = true;
d = false;
d = false;
d =
false;
d = false;
d = false;
d = false;
d =
false;
d = false;
慬敢
?
吮硥
??
准备发送
;
=
;
}
}
catch
(Exception er)
{
(Error: + e, Error);
return;
}
}
private void
checkBox5_CheckedChanged(object sender, EventArgs
e)
{
try
{
if
(d)
{
d = false;
d =
false;
d = false;
d = false;
d = true;
d =
false;
d = false;
d = false;
d = false;
d =
false;
d = false;
d = false;
p>
慬敢
?
吮硥
??<
/p>
准备发送
;
=
_x0005_;
}
}
catch
(Exception er)
{
(Error: + e, Error);
return;
}
}
private void
checkBox6_CheckedChanged(object sender, EventArgs
e)
{
try
{
if
(d)
{
d = false;
d =
false;
d = false;
d = false;
d = false;
d =
true;
d = false;
d = false;
d = false;
d =
false;
d = false;
d = false;
p>
慬敢
?
吮硥
??<
/p>
准备发送
;
=
;
}
}
catch (Exception er)
{
(Error: + e, Error);
return;
}
}
-
-
-
-
-
-
-
-
-
上一篇:最简单的八字算命方法
下一篇:上海牛津版二年级下册英语全册教材内容