一个串口com1向另一个串口com2发信息 比如发1-10之间的任意一个数,com2接收到后就会回复a ,11-20之间的数,com2就会回复b 以此类推 ,如果没回复,则com1继续向com2重复发。请大神们给个思路,或者最好是参考代码,谢谢
2条回答 默认 最新
- 我不懂电脑 2016-08-05 03:05关注
C#非常的强大而且友好,串口的通信可以通过编程环境(这里我用的是Visual Studio 2010),如果有需要的话可以给我信息,我有完整版的安装包。如下图,简单的调用和选择就完成了串口的基本配置。
下面主要就是编程的问题了,在窗体Load的进程中可以完成串口的启动
复制代码
1 private void Form1_Load(object sender, EventArgs e)
2 {
3 serialPort1.PortName = "COM4";
4 serialPort1.BaudRate = 9600;
5 serialPort1.Open();
6 this.textBox1.Clear();
7 thread = new Thread(CrossThreadFlush);
8 thread.IsBackground = true;
9 thread.Start();
10 }
复制代码
而后就是读取数据的操作,这时候用到事件1 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
在这个事件里面编程就可以了,但是在对窗体内文本进行操作的时候会发现出现了线程的冲突和错误。网上给出的一种解决方法是采用代理的方式具体的程序如下
复制代码
1 public partial class Form1 : Form
2 {
3 private delegate void FlushClient(); //代理
4 Thread thread = null;
5 uint DateTemp;
6 uint Datefalg = 0;
7 uint counter = 0;
8 uint DateTemp1 = 0;
9 uint TMP1;
10 uint RH1;
11 uint PRESS1;
12 double TMP;
13 double RH;
14 double PRESS;
15
16 public Form1()
17 {
18 InitializeComponent();
19 }
20
21
22 private void Form1_Load(object sender, EventArgs e)
23 {
24 serialPort1.PortName = "COM4";
25 serialPort1.BaudRate = 9600;
26 serialPort1.Open();
27 this.textBox1.Clear();
28 thread = new Thread(CrossThreadFlush);
29 thread.IsBackground = true;
30 thread.Start();
31 }
32
33
34 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
35 {
36 counter=counter+1;
37 DateTemp = (uint)this.serialPort1.ReadByte();
38 if (DateTemp == 0xF1)
39 {
40 Datefalg = 1;
41 DateTemp1 = 0;
42 DateTemp = 0;
43 counter=0;
44 }
45 if (DateTemp == 0xF2)
46 {
47 Datefalg = 2;
48 DateTemp1 = 0;
49 DateTemp = 0;
50 counter=0;
51 }
52 if (DateTemp == 0xF3)
53 {
54 Datefalg = 3;
55 DateTemp1 = 0;
56 DateTemp = 0;
57 counter=0;
58 }
59 if (Datefalg == 1 && DateTemp != 0xF1)
60 { if(counter==1)
61 DateTemp1 = DateTemp;
62 if (counter == 2)
63 {
64 TMP1 = DateTemp1 * 256 + DateTemp;
65 TMP = (0.01 * TMP1) - 40;
66 }
67
68 }
69 if (Datefalg == 2 && DateTemp != 0xF2)
70 {
71 if (counter == 1)
72 DateTemp1 = DateTemp;
73 if (counter == 2)
74 {
75 RH1 = DateTemp1 * 256 + DateTemp;
76 RH = (0.0405 * RH1) - 4-0.0000028*RH1*RH1;
77 }
78
79 }
80 if (Datefalg == 3 && DateTemp != 0xF3)
81 {
82 if (counter == 1)
83 DateTemp1 = DateTemp;
84 if (counter == 2)
85 {
86 PRESS1 = DateTemp1 * 256 + DateTemp;
87 PRESS = (PRESS1-16384)*90/16384 + 30;
88 }
89 }
90 }
91
92 private void CrossThreadFlush()
93 {
94 while (true)
95 {
96 //将sleep和无限循环放在等待异步的外面
97 Thread.Sleep(500);
98 ThreadFunction();
99 }
100 }
101
102 private void ThreadFunction()
103 {
104 if (this.textBox1.InvokeRequired)//等待异步
105 {
106 FlushClient fc = new FlushClient(ThreadFunction);
107 this.Invoke(fc); //通过代理调用刷新方法
108
109 }
110 else
111 textBox1.Text = TMP.ToString("0.00");
112 if (this.textBox2.InvokeRequired)//等待异步
113 {
114 FlushClient fc = new FlushClient(ThreadFunction);
115 this.Invoke(fc); //通过代理调用刷新方法
116
117 }
118 else
119 textBox2.Text = RH.ToString("0.00");
120 if (this.textBox3.InvokeRequired)//等待异步
121 {
122 FlushClient fc = new FlushClient(ThreadFunction);
123 this.Invoke(fc); //通过代理调用刷新方法
124
125 }
126 else
127 textBox3.Text = PRESS.ToString("0.00");
128
129 }
130
131
132 }
复制代码
通过这样的一番调试之后才得以程序正确的运行,由于工科程序员和计算机程序员的编程思想还是有差别的,所以写的不好的地方请轻喷。解决 无用评论 打赏 举报
悬赏问题
- ¥15 求差集那个函数有问题,有无佬可以解决
- ¥15 【提问】基于Invest的水源涵养
- ¥20 微信网友居然可以通过vx号找到我绑的手机号
- ¥15 寻一个支付宝扫码远程授权登录的软件助手app
- ¥15 解riccati方程组
- ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
- ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
- ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
- ¥50 树莓派安卓APK系统签名
- ¥65 汇编语言除法溢出问题