自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#串口操作的使用淺析

開(kāi)發(fā) 后端
C#串口操作在工業(yè)通信方面的應(yīng)用是如何的呢?那么本文就向你介紹C#串口操作在工業(yè)通信編程中使用的心得體會(huì),希望對(duì)你了解和學(xué)習(xí)C#串口操作有所幫助。

C#串口操作的使用:做工業(yè)通信有很長(zhǎng)時(shí)間了,特別是串口(232/485),有VB/VC/C各種版本的串口操作代碼,這些代碼也經(jīng)過(guò)了多年的現(xiàn)場(chǎng)考驗(yàn),應(yīng)該說(shuō)是比較健壯的代碼,但是目前卻沒(méi)有C#相對(duì)成熟的串口操作代碼,最近用Moxa的設(shè)備開(kāi)發(fā)基于WinCE5.0的C#串口操作代碼,所以就擴(kuò)充完善了一下C#串口操作,特別是SendCommand函數(shù),這是我比較常用的主從通信代碼,不喜歡用事件或線程接數(shù)據(jù),在規(guī)定的超時(shí)時(shí)間內(nèi)直接循環(huán)判斷要接收的數(shù)據(jù)。

下面是具體C#串口操作的代碼:

  1. public class PortData  
  2. {  
  3. public event PortDataReceivedEventHandle Received;  
  4. public event SerialErrorReceivedEventHandler Error;   
  5. public SerialPort port;  
  6. public bool ReceiveEventFlag = false;   
  7.  //接收事件是否有效 false表示有效  
  8.  
  9. public PortData(string sPortName,   
  10. int baudrate,Parity parity,SerialInterface.SerialMode mode)  
  11. {  
  12. port = new SerialPort(sPortName,   
  13. baudrate, parity, 8, StopBits.One);  
  14. port.RtsEnable = true;  
  15. port.ReadTimeout = 3000;  
  16. port.DataReceived +=   
  17. new SerialDataReceivedEventHandler(DataReceived);  
  18. port.ErrorReceived +=   
  19. new SerialErrorReceivedEventHandler(ErrorEvent);  
  20. }  
  21.  
  22. ~PortData()  
  23. {  
  24. Close();  
  25. }  
  26. public void Open()  
  27. {  
  28. if (!port.IsOpen)  
  29. {     
  30. port.Open();  
  31. }  
  32. }  
  33.  
  34. public void Close()  
  35. {  
  36. if (port.IsOpen)  
  37. {  
  38. port.Close();  
  39. }  
  40. }  
  41. //C#串口操作之?dāng)?shù)據(jù)發(fā)送  
  42. public void SendData(byte[] data)  
  43. {  
  44. if (port.IsOpen)  
  45. {  
  46. port.Write(data, 0, data.Length);  
  47. }  
  48. }  
  49. public void SendData(byte[] data,  
  50. int offset,int count)  
  51. {  
  52. if (port.IsOpen)  
  53. {  
  54. port.Write(data, offset, count);  
  55. }  
  56. }  
  57. //C#串口操作之發(fā)送命令  
  58. public int SendCommand(byte[] SendData,  
  59.  ref  byte[] ReceiveData,int Overtime)  
  60. {  
  61.  
  62. if(port.IsOpen)  
  63. {  
  64. ReceiveEventFlag = true;//關(guān)閉接收事件  
  65. port.DiscardInBuffer(); //清空接收緩沖區(qū)   
  66. port.Write(SendData, 0, SendData.Length);  
  67. int num=0,ret=0;  
  68. while (num++ < Overtime)  
  69. {  
  70. if (port.BytesToRead >= ReceiveData.Length) break;  
  71. System.Threading.Thread.Sleep(1);   
  72. }  
  73. if (port.BytesToRead >= ReceiveData.Length)   
  74. ret = port.Read(ReceiveData, 0, ReceiveData.Length);  
  75. ReceiveEventFlag = false;   //打開(kāi)事件  
  76. return ret;  
  77. }  
  78. return -1;  
  79. }  
  80.  
  81. public void ErrorEvent(object sender,  
  82.  SerialErrorReceivedEventArgs e)  
  83. {  
  84. if (Error != null) Error(sender, e);  
  85. }  
  86. //C#串口操作之?dāng)?shù)據(jù)接收  
  87. public void DataReceived(object sender,  
  88.  SerialDataReceivedEventArgs e)  
  89. {  
  90. //禁止接收事件時(shí)直接退出  
  91. if (ReceiveEventFlag) return;  
  92.  
  93. byte[] data = new byte[port.BytesToRead];  
  94. port.Read(data, 0, data.Length);  
  95. if (Received != null) Received(sender,   
  96. new PortDataReciveEventArgs(data));  
  97. }  
  98.  
  99. public bool IsOpen()  
  100. {  
  101. return port.IsOpen;  
  102. }  
  103. }  
  104. public delegate void PortDataReceivedEventHandle(  
  105. object sender, PortDataReciveEventArgs e);  
  106. public class PortDataReciveEventArgs : EventArgs  
  107. {  
  108. public PortDataReciveEventArgs()  
  109. {  
  110. this.data = null;  
  111. }  
  112.  
  113. public PortDataReciveEventArgs(byte[] data)  
  114. {  
  115. this.data = data;  
  116. }  
  117.  
  118. private byte[] data;  
  119.  
  120. public byte[] Data  
  121. {  
  122. get { return data; }  
  123. set { data = value; }  
  124. }  
  125. }  

注:1~9 串口的名稱是 "COMx:",>9的以前用\\\\.\\COMx:比較好使,但是在moxa 661設(shè)備上卻不行,要用如下格式"$device\\COM" + PortNo.ToString() + "\0",也許這是moxa修改了相應(yīng)的串口驅(qū)動(dòng)。

C#串口操作的一些內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)使用C#串口操作有所幫助。

【編輯推薦】

  1. 深度剖析C#序列化和反序列化
  2. 深入探討C#序列化和反序列化
  3. C# XML序列化應(yīng)用淺析
  4. C#對(duì)象序列化應(yīng)用淺析
  5. C#工具欄的編程實(shí)現(xiàn)淺析
責(zé)任編輯:仲衡 來(lái)源: 博客園
相關(guān)推薦

2009-08-17 13:34:02

C#異步操作

2009-09-11 11:27:38

AttributeUsC# Attribut

2009-09-07 06:07:46

C#窗體設(shè)計(jì)

2009-08-18 13:49:21

C# 操作Excel

2009-08-14 15:23:10

C#使用ErrorPr

2009-08-19 10:25:14

C#操作Word

2009-08-26 13:48:31

C#打印條碼

2009-09-04 15:45:29

C#緩存流

2009-08-18 09:37:14

C#枚舉類型

2009-08-20 10:25:37

C#操作內(nèi)存

2009-08-20 10:53:23

C#操作內(nèi)存

2009-08-18 16:04:12

C# 操作Excel

2009-08-19 09:42:52

C#操作Word書(shū)簽

2009-08-31 18:38:59

C#寫(xiě)文件

2009-09-18 10:58:31

C#數(shù)組操作

2009-08-19 11:13:49

C#操作Word

2009-08-19 11:34:06

C#操作Word

2009-08-19 16:42:41

C#如何使用XML

2009-08-13 13:29:04

C#結(jié)構(gòu)體使用

2009-08-25 16:29:33

C#使用sqlserv
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)