`
guoyiqi
  • 浏览: 963454 次
社区版块
存档分类
最新评论

创建C#串口通信程序详解(转自:http://www.csharpwin.com/csharpspace/5544r7857.shtml)

 
阅读更多

在.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(
  45. objectsender,EventArgse)
  46. {
  47. this.bRate="";
  48. this.sBits="";
  49. //closeform
  50. this.Close();
  51. }
  52. privatevoidokButton_Click_1(
  53. objectsender,EventArgse)
  54. {
  55. //herewesetthevalueforstopbitsandbaudrate.
  56. this.bRate=BaudRateComboBox.Text;
  57. this.sBits=stopBitComboBox.Text;
  58. //
  59. this.Close();
  60. }
  61. }
  62. }

C#串口通信程序创建的相关内容就向你介绍到这里,希望对你了解创建C#串口通信程序的步骤和需要注意的事宜。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics