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

創(chuàng)建C#串口通信程序詳解

開發(fā) 后端
創(chuàng)建C#串口通信程序需要注意什么呢?創(chuàng)建C#串口通信程序的步驟是什么?那么本文就向你詳細(xì)介紹創(chuàng)建C#串口通信程序集體的內(nèi)容。

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

創(chuàng)建C#串口通信程序之命名空間

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

創(chuàng)建C#串口通信程序之創(chuàng)建SerialPort 對(duì)象

通過(guò)創(chuàng)建SerialPort 對(duì)象,我們可以在程序中控制串口通信的全過(guò)程。

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

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

WriteLine(string):寫入輸出緩沖

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

Close():關(guān)閉

  1. //create a Serial Port object  
  2. SerialPort sp = new SerialPort (); 

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

BaudRate:串口的波特率

StopBits:每個(gè)字節(jié)的停止位數(shù)量

ReadTimeout:當(dāng)讀操作沒有完成時(shí)的停止時(shí)間。單位,毫秒

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

創(chuàng)建C#串口通信程序之串口的硬件知識(shí)

在數(shù)據(jù)傳輸?shù)臅r(shí)候,每個(gè)字節(jié)的數(shù)據(jù)通過(guò)單個(gè)的電纜線傳輸。包包括開始位,數(shù)據(jù),結(jié)束為。一旦

開始位傳出,后面就會(huì)傳數(shù)據(jù),可能是5,6,7或8位,就看你的設(shè)定了。發(fā)送和接收必須設(shè)定同樣

的波特率和數(shù)據(jù)位數(shù)。

創(chuàng)建C#串口通信程序之無(wú)貓模式

沒有Modem模式的電纜只是簡(jiǎn)單地交叉?zhèn)魉秃徒邮站€。同樣DTR & DSR, 和 RTS & CTS也需要交叉。

這里,我們?nèi)龡l線?;ミB2和3(一段的2pin連接3pin),連接兩端的5pin。

創(chuàng)建C#串口通信程序示例程序

如果想使用默認(rèn)屬性,按“Save Status”按鈕,如果想改變屬性按“Property”。設(shè)定好之后,可以通信了。

主窗口的代碼

  1. #region Using directives  
  2.  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.ComponentModel;  
  6. using System.Data;  
  7. using System.Drawing;  
  8. using System.Windows.Forms;  
  9. using System.IO.Ports;  
  10.  
  11. #endregion  
  12.  
  13. namespace Serialexpample  
  14. {  
  15. partial class Form1 : Form  
  16. {  
  17. //create instance of property page  
  18. //property page is used to set values for stop bits and  
  19. //baud rate  
  20.  
  21. PropertyPage pp = new PropertyPage();  
  22.  
  23. //create an Serial Port object  
  24. SerialPort sp = new SerialPort();  
  25.  
  26. public Form1()  
  27. {  
  28. InitializeComponent();  
  29. }  
  30.  
  31. private void propertyButton_Click(object sender, EventArgs e)  
  32. {  
  33. //show property dialog  
  34. pp.ShowDialog();  
  35.  
  36. propertyButton.Hide();  
  37. }  
  38.  
  39. private void sendButton_Click(object sender, EventArgs e)  
  40. {  
  41. try 
  42. {  
  43. //write line to serial port  
  44. sp.WriteLine(textBox.Text);  
  45. //clear the text box  
  46. textBox.Text = "";  
  47. }  
  48. catch (System.Exception ex)  
  49. {  
  50. baudRatelLabel.Text = ex.Message;  
  51. }  
  52.  
  53. }  
  54.  
  55. private void ReadButton_Click(  
  56. object sender, EventArgs e)  
  57. {  
  58. try 
  59. {  
  60. //clear the text box  
  61. textBox.Text = "";  
  62. //read serial port and displayed the data in text box  
  63. textBox.Text = sp.ReadLine();  
  64. }  
  65. catch(System.Exception ex)  
  66. {  
  67. baudRatelLabel.Text = ex.Message;  
  68. }  
  69. }  
  70.  
  71. private void Form1_Load(object sender, EventArgs e)  
  72. {  
  73.  
  74. }  
  75.  
  76. private void Form1_FormClosing(  
  77. object sender, FormClosingEventArgs e)  
  78. {  
  79. MessageBox.Show("Do u want to Close the App");  
  80. sp.Close();  
  81. }  
  82.  
  83. private void startCommButton_Click(  
  84. object sender, EventArgs e)  
  85. {  
  86. startCommButton.Hide();  
  87. sendButton.Show();  
  88. readButton.Show();  
  89. textBox.Show();  
  90. }  
  91.  
  92. //when we want to save the status(value)  
  93. private void saveStatusButton_Click_1(  
  94. object sender, EventArgs e)  
  95. {  
  96. //display values  
  97. //if no property is set the default values  
  98. if (pp.bRate == "" && pp.sBits == "")  
  99. {  
  100. dataBitLabel.Text = "BaudRate = " +  
  101.  sp.BaudRate.ToString();  
  102. readTimeOutLabel.Text = "StopBits = " +   
  103. sp.StopBits.ToString();  
  104. }  
  105. else 
  106. {  
  107. dataBitLabel.Text = "BaudRate = " +  
  108.  pp.bRate;  
  109. readTimeOutLabel.Text = "StopBits = " + pp.sBits;  
  110. }  //創(chuàng)建C#串口通信程序
  111.  
  112. parityLabel.Text = "DataBits = " +  
  113.  sp.DataBits.ToString();  
  114. stopBitLabel.Text = "Parity = " +  
  115.  sp.Parity.ToString();  
  116. readTimeOutLabel.Text = "ReadTimeout = " +  
  117.   sp.ReadTimeout.ToString();  
  118.  
  119. if (propertyButton.Visible == true)  
  120. propertyButton.Hide();  
  121. saveStatusButton.Hide();  
  122. startCommButton.Show();  
  123.  
  124. try 
  125. {  
  126. //open serial port  
  127. sp.Open();  
  128. //set read time out to 500 ms  
  129. sp.ReadTimeout = 500;  
  130. }  
  131. catch (System.Exception ex)  
  132. {  
  133. baudRatelLabel.Text = ex.Message;  
  134. }  
  135. }  
  136. }  
  137. }  

創(chuàng)建C#串口通信程序之屬性設(shè)置對(duì)話框代碼:

  1. #region Using directives  
  2.  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.ComponentModel;  
  6. using System.Data;  
  7. using System.Drawing;  
  8. using System.Text;  
  9. using System.Windows.Forms;  
  10.  
  11. #endregion  
  12.  
  13. namespace Serialexpample  
  14. {  
  15. partial class PropertyPage : Form  
  16. {  
  17. //variables for storing values of baud rate and stop bits  
  18. private string baudR="";  
  19. private string stopB="";  
  20.  
  21. //property for setting and getting baud rate and stop bits  
  22. public string bRate  
  23. {  
  24. get 
  25. {  
  26. return baudR;  
  27. }  
  28. set 
  29. {  
  30. baudR = value;  
  31. }  
  32. }  
  33.  
  34. public string sBits  
  35. {  
  36. get 
  37. {  
  38. return stopB;  
  39. }  
  40. set 
  41. {  
  42. stopB = value;  
  43. }  
  44. }  
  45.  
  46. public PropertyPage()  
  47. {  
  48. InitializeComponent();  
  49. }  
  50.  
  51. private void cancelButton_Click(  
  52. object sender, EventArgs e)  
  53. {  
  54. this.bRate = "";  
  55. this.sBits = "";  
  56. //close form  
  57. this.Close();  
  58. }  
  59.  
  60. private void okButton_Click_1(  
  61. object sender, EventArgs e)  
  62. {  
  63. //here we set the value for stop bits and baud rate.  
  64. this.bRate = BaudRateComboBox.Text;  
  65. this.sBits = stopBitComboBox.Text;  
  66. //  
  67. this.Close();  
  68.  
  69. }  
  70. }  

C#串口通信程序創(chuàng)建的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你了解創(chuàng)建C#串口通信程序的步驟和需要注意的事宜。

【編輯推薦】

  1. C#工具欄的編程實(shí)現(xiàn)淺析
  2. C#串口操作的使用淺析
  3. 深入了解Mscomm控件
  4. C#串口操作實(shí)際應(yīng)用開發(fā)詳解
  5. C#串口編程步驟詳解
責(zé)任編輯:仲衡 來(lái)源: 新浪博客
相關(guān)推薦

2024-12-24 07:38:44

C#串口通信

2024-06-03 10:11:13

2009-08-25 17:13:57

C#串口編程

2009-08-25 17:43:17

C#串口監(jiān)聽

2011-06-29 14:23:08

Qt 串口

2011-06-29 14:42:06

Qt 串口

2011-06-29 14:32:25

Qt 串口

2011-06-29 14:06:15

Qt 串口

2011-06-29 13:50:15

Qt 串口

2009-08-25 17:02:20

C#串口操作

2009-09-01 11:07:58

C#項(xiàng)目

2009-08-14 11:00:16

C#創(chuàng)建Windows

2009-08-21 11:39:58

C# Socket通信

2009-01-19 11:07:42

C#Web.NET

2009-08-21 16:14:52

服務(wù)端與客戶端通信

2009-08-25 15:59:28

C#串口操作

2009-09-03 17:36:13

C#創(chuàng)建Web應(yīng)用程序

2009-08-25 09:39:21

創(chuàng)建C# Window

2011-07-01 13:03:32

QT 線程 串口

2010-08-31 09:46:23

C#
點(diǎn)贊
收藏

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