ǰλãҳ > VB控件Mscomm控件与PLC进行RS485(Modbus)通讯源码 - 百度文库
VBؼMscommؼPLCRS485(Modbus)ͨѶԴ
õModbus RTUͨѶģʽͨתRS485Χ豸ͨͨѶдΧ豸ַָݣӶﵽԶԶ豸
Dim HiByte As Byte Dim LoByte As Byte Dim CRC16Lo As Byte Dim CRC16Hi As Byte Dim ReturnData(1) As Byte Dim K As Integer Dim CmdLenth As Integer
Private Sub Command1_Click() K = Text9.Text 'д6 ֽ
Text13.Text = \
'===========
ֵ
=======================================================================================
'<< 㷨һ >> Dim WriteStr() As Byte Dim u As Integer
ReDim WriteStr(K + 2) For u = 0 To K
WriteStr(u) = Val(\ext) Next
'<< 㷨 >> Dim CRC_2() As Byte Dim v As Integer ReDim CRC_2(K) For v = 0 To K
CRC_2(v) = Val(\Next
'==================================================================================================
Call CRC161(CRC_2())
Call CRC16(WriteStr(), K)
MSComm1.InBufferCount = 0
'==========
ʾʹ
========================================================================================
Dim m As Integer For m = 0 To 23 If m <= K Then
Text8(m).Text = Hex(WriteStr(m)) Else
Text8(m).Text = \End If Next
'================================================================================================== WriteStr(K + 1) = LoByte WriteStr(K + 2) = HiByte
' ʹ Text4.Text = \Dim g As Integer
For g = 0 To K + 2
Text4.Text = Text4.Text + \ \Next
'дͺ,յ8 ֽʱж CmdLenth = 8
MSComm1.RThreshold = CmdLenth
MSComm1.Output = WriteStr End Sub
Private Sub Command2_Click() End End Sub
Private Sub Command3_Click()
Label34.Caption = \Text13.Text = \
K = Text9.Text 'д6 ֽ
'=========== ========
'<< 㷨 >> Dim CRC_2() As Byte Dim v As Integer ReDim CRC_2(K)
For v = 0 To K
CRC_2(v) = Val(\Next
'==================================================================================================
Call CRC161(CRC_2()) Call CRC16(WriteStr(), K)
MSComm1.InBufferCount = 0
'========== ʾʹ ========================================================================================
Dim m As Integer For m = 0 To 23
If m <= K Then
Text8(m).Text = Hex(WriteStr(m)) Else
Text8(m).Text = \End If Next
'================================================================================================== WriteStr(K + 1) = LoByte
ֵ
===============================================================================
WriteStr(K + 2) = HiByte
' ʹ Text4.Text = \Dim g As Integer
For g = 0 To K + 2
Text4.Text = Text4.Text + \ \Next
'ͺ, 5 + SendStr(5) * 2 ֽʱж CmdLenth = 5 + WriteStr(5) * 2 MSComm1.RThreshold = CmdLenth
MSComm1.Output = WriteStr '
'****************************************************************************************************************************************
'********************************************************** *********************************************************
'**************************************************************************************************************************************** ' Dim sAddr As String '
' Dim CheckString As String ' Dim CheckCode As String ' Dim CmdCode As String ' Dim Sum As Integer ' Dim a As Integer ' Dim tmp As String 'a = 0 'tmp = 0 ' ' '
' Do While Len(tmp) < 8
'
' tmp = tmp + MSComm1.Input
' testNO.Caption = testNO.Caption + \ \' a = a + 1
' If a >= 3000 Then
' MSComm1.PortOpen = False
92ƪĵ