国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

創建C#串口通信程序詳解

2019-11-17 02:53:49
字體:
來源:轉載
供稿:網友
創建C#串口通信程序詳解

在.NET平臺下創建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空間是System.IO.Ports。這個新的框架不但可以訪問計算機上的串口,還可以和串口設備進行通信。我們將使用標準的RS 232 C 在PC間通信。它工作在全雙工模式下,而且我們不打算使用任何的握手或流控制器,而是使用無modem連接。創建C#串口通信程序的具體實現是如何的呢?讓我們開始吧:

創建C#串口通信程序之命名空間

System.IO.Ports命名空間中最重用的是SerialPort 類。

創建C#串口通信程序之創建SerialPort 對象

通過創建SerialPort 對象,我們可以在程序中控制串口通信的全過程。

我們將要用到的SerialPort 類的方法:

ReadLine():從輸入緩沖區讀一新行的值,如果沒有,會返回NULL

WriteLine(string):寫入輸出緩沖

Open():打開一個新的串口連接

Close():關閉

  1. //createaSerialPortobject
  2. SerialPortsp=newSerialPort();

默認情況下,DataBits 值是8,StopBits 是1,通信端口是COM1。這些都可以在下面的屬性中重新設置:

BaudRate:串口的波特率

StopBits:每個字節的停止位數量

ReadTimeout:當讀操作沒有完成時的停止時間。單位,毫秒

還有不少其它公共屬性,自己查閱MSDN。

創建C#串口通信程序之串口的硬件知識

在數據傳輸的時候,每個字節的數據通過單個的電纜線傳輸。包包括開始位,數據,結束為。一旦

開始位傳出,后面就會傳數據,可能是5,6,7或8位,就看你的設定了。發送和接收必須設定同樣

的波特率和數據位數。

創建C#串口通信程序之無貓模式

沒有Modem模式的電纜只是簡單地交叉傳送和接收線。同樣DTR & DSR, 和 RTS & CTS也需要交叉。

這里,我們三條線。互連2和3(一段的2pin連接3pin),連接兩端的5pin。

創建C#串口通信程序示例程序

如果想使用默認屬性,按“Save Status”按鈕,如果想改變屬性按“PRoperty”。設定好之后,可以通信了。

主窗口的代碼

  1. #regionUsingdirectives
  2. usingSystem;
  3. usingSystem.Collections.Generic;
  4. usingSystem.ComponentModel;
  5. usingSystem.Data;
  6. usingSystem.Drawing;
  7. usingSystem.Windows.Forms;
  8. usingSystem.IO.Ports;
  9. #endregion
  10. namespaceSerialexpample
  11. {
  12. partialclassForm1:Form
  13. {
  14. //createinstanceofpropertypage
  15. //propertypageisusedtosetvaluesforstopbitsand
  16. //baudrate
  17. PropertyPagepp=newPropertyPage();
  18. //createanSerialPortobject
  19. SerialPortsp=newSerialPort();
  20. publicForm1()
  21. {
  22. InitializeComponent();
  23. }
  24. privatevoidpropertyButton_Click(objectsender,EventArgse)
  25. {
  26. //showpropertydialog
  27. pp.ShowDialog();
  28. propertyButton.Hide();
  29. }
  30. privatevoidsendButton_Click(objectsender,EventArgse)
  31. {
  32. try
  33. {
  34. //writelinetoserialport
  35. sp.WriteLine(textBox.Text);
  36. //clearthetextbox
  37. textBox.Text="";
  38. }
  39. catch(System.Exceptionex)
  40. {
  41. baudRatelLabel.Text=ex.Message;
  42. }
  43. }
  44. privatevoidReadButton_Click(
  45. objectsender,EventArgse)
  46. {
  47. try
  48. {
  49. //clearthetextbox
  50. textBox.Text="";
  51. //readserialportanddisplayedthedataintextbox
  52. textBox.Text=sp.ReadLine();
  53. }
  54. catch(System.Exceptionex)
  55. {
  56. baudRatelLabel.Text=ex.Message;
  57. }
  58. }
  59. privatevoidForm1_Load(objectsender,EventArgse)
  60. {
  61. }
  62. privatevoidForm1_FormClosing(
  63. objectsender,FormClosingEventArgse)
  64. {
  65. MessageBox.Show("DouwanttoClosetheApp");
  66. sp.Close();
  67. }
  68. privatevoidstartCommButton_Click(
  69. objectsender,EventArgse)
  70. {
  71. startCommButton.Hide();
  72. sendButton.Show();
  73. readButton.Show();
  74. textBox.Show();
  75. }
  76. //whenwewanttosavethestatus(value)
  77. privatevoidsaveStatusButton_Click_1(
  78. objectsender,EventArgse)
  79. {
  80. //displayvalues
  81. //ifnopropertyissetthedefaultvalues
  82. if(pp.bRate==""&&pp.sBits=="")
  83. {
  84. dataBitLabel.Text="BaudRate="+
  85. sp.BaudRate.ToString();
  86. readTimeOutLabel.Text="StopBits="+
  87. sp.StopBits.ToString();
  88. }
  89. else
  90. {
  91. dataBitLabel.Text="BaudRate="+
  92. pp.bRate;
  93. readTimeOutLabel.Text="StopBits="+pp.sBits;
  94. } //創建C#串口通信程序
  95. parityLabel.Text="DataBits="+
  96. sp.DataBits.ToString();
  97. stopBitLabel.Text="Parity="+
  98. sp.Parity.ToString();
  99. readTimeOutLabel.Text="ReadTimeout="+
  100. sp.ReadTimeout.ToString();
  101. if(propertyButton.Visible==true)
  102. propertyButton.Hide();
  103. saveStatusButton.Hide();
  104. startCommButton.Show();
  105. try
  106. {
  107. //openserialport
  108. sp.Open();
  109. //setreadtimeoutto500ms
  110. sp.ReadTimeout=500;
  111. }
  112. catch(System.Exceptionex)
  113. {
  114. baudRatelLabel.Text=ex.Message;
  115. }
  116. }
  117. }
  118. }

創建C#串口通信程序之屬性設置對話框代碼:

  1. #regionUsingdirectives
  2. usingSystem;
  3. usingSystem.Collections.Generic;
  4. usingSystem.ComponentModel;
  5. usingSystem.Data;
  6. usingSystem.Drawing;
  7. usingSystem.Text;
  8. usingSystem.Windows.Forms;
  9. #endregion
  10. namespaceSerialexpample
  11. {
  12. partialclassPropertyPage:Form
  13. {
  14. //variablesforstoringvaluesofbaudrateandstopbits
  15. privatestringbaudR="";
  16. privatestringstopB="";
  17. //propertyforsettingandgettingbaudrateandstopbits
  18. publicstringbRate
  19. {
  20. get
  21. {
  22. returnbaudR;
  23. }
  24. set
  25. {
  26. baudR=value;
  27. }
  28. }
  29. publicstringsBits
  30. {
  31. get
  32. {
  33. returnstopB;
  34. }
  35. set
  36. {
  37. stopB=value;
  38. }
  39. }
  40. publicPropertyPage()
  41. {
  42. InitializeComponent();
  43. }
  44. privatevoidcancelButton_Click(
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新宁县| 合江县| 靖宇县| 房产| 隆德县| 西峡县| 定陶县| 桐庐县| 普定县| 泾阳县| 天祝| 乌恰县| 始兴县| 渭源县| 称多县| 湖北省| 垣曲县| 桂东县| 历史| 合肥市| 原平市| 泌阳县| 长顺县| 汝南县| 定陶县| 墨竹工卡县| 元江| 永昌县| 毕节市| 宝丰县| 孝昌县| 铅山县| 翁牛特旗| 阳信县| 讷河市| 尉犁县| 达尔| 富裕县| 沾化县| 九台市| 宜都市|