您好,欢迎来到尔游网。
搜索
您的当前位置:首页C# 编写的串口通信程序

C# 编写的串口通信程序

来源:尔游网
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 {

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 } } 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 }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- axer.cn 版权所有 湘ICP备2023022495号-12

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务