VB.NET串口通讯

前端之家收集整理的这篇文章主要介绍了VB.NET串口通讯前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


Mitsubishi公司的FX2N系列PLC与计算机之间的串口通讯,参考代码如下

  1. mports System.IO.Ports
  2. Public Class Form1
  3. '使用WithEvents关键字声明一个通信端口对象
  4. Dim WithEvents RS232 As SerialPort
  5. '初始化取得计算机中存在的可用串行通讯端口
  6. Sub initializing()
  7. For Each sp As String In SerialPort.GetPortNames()
  8. cmbCOM.Items.Add(sp) 'cmbCOM是ComboBox控件,该代码用来取得系统找到的所有串口名称
  9. Next
  10. cmbCOM.Sorted = True '排序
  11. cmbCOM.SelectedIndex = 0 '第一个是预设选项
  12. End Sub
  13.  
  14. '联机,使用正确的通讯参数建立一个通讯端口对象
  15. Sub online()
  16. Try
  17. RS232 = New IO.Ports.SerialPort(cmbCOM.SelectedItem.ToString,9600,Parity.Even,7,StopBits.Two)
  18. 'RS232.Encoding = System.Text.Encoding.ASCII,如果系统支持中文,可以使用Encoding.Unicode编码方案
  19. If Not RS232.IsOpen Then RS232.Open()
  20. Catch ex As Exception
  21. MsgBox("通讯端口打开错误!" & vbCrLf & "故障信息:" & vbCrLf & ex.Message,MsgBoxStyle.OkCancel)
  22. End Try
  23. End Sub
  24.  
  25. '离线,关闭通讯端口
  26. Sub offline()
  27. Try
  28. If RS232.IsOpen Then RS232.Close() : RS232 = Nothing '释放资源
  29. Catch ex As Exception
  30. MsgBox("通讯端口关闭错误!" & vbCrLf & "故障信息:" & vbCrLf & ex.Message,MsgBoxStyle.OkCancel)
  31. End Try
  32. End Sub
  33.  
  34. '通讯延时,通讯延时与串口处理数据时的ReadTimeoutWriteTimeout的含义是完全不同的
  35. Sub TimeDelay(ByVal DT As Integer)
  36. Dim ET As Integer
  37. ET = Environment.TickCount()
  38. Do
  39. If Environment.TickCount() - ET >= DT Then Exit Do
  40. Application.DoEvents() '处理队列中的讯息
  41. Loop
  42. End Sub
  43.  
  44. '串口数据接收函数
  45. Function DataReceive() As String
  46. Dim input As String,strfst As String
  47. DataReceive = Nothing
  48. input = RS232.ReadExisting()
  49. If input <> Nothing Then
  50. '以下代码用来判断接收的数据起始字符是STX(Chr(2))/ACK(Chr(6)),NAK/(Chr(21))
  51. strfst = Asc(Microsoft.VisualBasic.Left(input,1)).ToString
  52. Select Case strfst
  53. Case "2"
  54. If rCheckSum(input) = "OKSUM" Then Return (input)
  55. If rCheckSum(input) = "ERSUM" Then Return "ERSUM"
  56. Case "6"
  57. Return Asc(input).ToString
  58. Case "21"
  59. Return Asc(input).ToString
  60. Case Else
  61. Return "ERCOM" '如果所选串口返回的未知数据,说明串口通讯故障/计算机连接到未知设备!
  62. End Select
  63. Else
  64. Return "ERCOM" '如果所选串口没有任何数据返回,说明串口通讯故障/PC Command ERROR!
  65. End If
  66. End Function
  67.  
  68. '**************************************************************
  69.  
  70. Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
  71. initializing()
  72. btnoffline.Enabled = False
  73. stx.Text = "Chr(2)"
  74. cmd.Text = "0"
  75. End Sub
  76.  
  77. Private Sub btnonline_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnonline.Click
  78. online()
  79. If RS232.IsOpen Then btnonline.Enabled = False
  80. If RS232.IsOpen Then btnoffline.Enabled = True
  81. End Sub
  82.  
  83. Private Sub btnoffline_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnoffline.Click
  84. offline()
  85. btnoffline.Enabled = False
  86. btnonline.Enabled = True
  87. End Sub
  88.  
  89. Private Sub btnEnd_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnEnd.Click
  90. If RS232 Is Nothing Then '判断是否已建立通信对象
  91. End
  92. Else
  93. If RS232.IsOpen Then RS232.Close() '若已经打开,就将其关闭
  94. End If
  95. End
  96. End Sub
  97. Private Sub btnSend_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnSend.Click
  98. Try
  99. If Not RS232 Is Nothing Then '判断是否已建立通信对象
  100. Dim OutCmd As String
  101. Dim indat As String
  102. OutCmd = Nothing
  103. indat = RS232.ReadExisting() '使用.ReadExisting()方法清空缓冲区
  104. If cmd.Text.Trim = "0" Then OutCmd = cmd.Text.Trim + address.Text.Trim + byten.Text.Trim + Chr(3)
  105. If cmd.Text.Trim = "1" Then OutCmd = cmd.Text.Trim + address.Text.Trim + byten.Text.Trim + data.Text.Trim + Chr(3)
  106. OutCmd = Chr(2) + OutCmd + tCheckSum(OutCmd) '发送命令调用CheckSum函数生成CheckSum字符串
  107. RS232.Write(OutCmd)
  108. TimeDelay(300)
  109. indat = DataReceive()
  110. If indat = "ERCOM" Or indat = "ERSUM" Then
  111. MsgBox("通讯故障!PLC OFFLINE!!!/数据传输错误!!!")
  112. Else
  113. txtResult.Text += OutCmd + "->" + indat + vbCrLf
  114. txtResult.SelectionStart = txtResult.TextLength '将光标自动移至最下一位
  115. txtResult.ScrollToCaret() '将光标自动移至最下一位
  116. End If
  117. Else
  118. MsgBox("通信端口尚未打开!",MsgBoxStyle.OkCancel)
  119. End If
  120. Catch ex As Exception
  121. MsgBox("通讯端口发生故障!" & vbCrLf & "故障信息:" & vbCrLf & ex.Message)
  122. End Try
  123. End Sub
  124.  
  125. '应用程序窗口关闭时的响应事件,如果RS232通讯已建立就将其关闭退出
  126. Private Sub Form1_FormClosing(ByVal sender As Object,ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  127. If RS232 Is Nothing Then '判断是否已建立通信对象
  128. End
  129. Else
  130. If RS232.IsOpen Then RS232.Close() '若已经打开,就将其关闭
  131. End If
  132. End
  133. End Sub
  134.  
  135. End Class

猜你在找的VB相关文章