日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)建C#串口通信程序詳解

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

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

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

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

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

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

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

WriteLine(string):寫入輸出緩沖

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

Close():關(guān)閉

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

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

BaudRate:串口的波特率

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

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

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

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

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

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

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

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

沒有Modem模式的電纜只是簡單地交叉?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è)置對話框代碼:

 
 
 
  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)容就向你介紹到這里,希望對你了解創(chuàng)建C#串口通信程序的步驟和需要注意的事宜。

【編輯推薦】

  1. C#工具欄的編程實現(xiàn)淺析
  2. C#串口操作的使用淺析
  3. 深入了解Mscomm控件
  4. C#串口操作實際應(yīng)用開發(fā)詳解
  5. C#串口編程步驟詳解

當(dāng)前文章:創(chuàng)建C#串口通信程序詳解
文章分享:http://www.dlmjj.cn/article/dhjejji.html