很多人在找MODBUS通讯代码,我把以前用VB.NET写的一些代码贴上来供参考
'判断电机是否在转动
Public Sub WaitMotorStop()
'判断电机是否在转动
Dim str1 As String = "1"
Do Until str1 = "0"
Comm2PLC(RCS, Relay901, "")
System.Threading.Thread.Sleep(50)
Dim str2 As String = FormMain.RS232.ReadExisting().ToString()
FormMain.RS232.DiscardInBuffer()
FormMain.RS232.DiscardOutBuffer()
'先高低位转换,再16进制转换为10进制。例如把78563412转换为12345678
If str2.Length > 6 Then
str1 = str2.Substring(6, 1)
End If
Loop
Thread.Sleep(200)
End Sub
‘----------------------------------------------------------------’
'写入PLC
Public Sub Comm2PLC(ByRef _Code As String, ByRef _Address As String, ByVal _Command As String)
Dim str1 As String = _Code & _Address & _Command '代码字+中间继电器+命令字
Dim str2 As String = Calculte_Vertification_bit(str1)
Dim str3 As String =