2 rthan008 rthan008 于 2015.06.26 16:13 提问

使用MODBUS再TCPIP上通信的问题

Public Sub WriteQW16(ByVal Address As Short, ByVal value As Short)
Dim buffer As Byte()
If Me.Connected = False Then
'Information.Err().Raise(0x215, "", "not connected", null, null);
Throw New Exception("not connected!")
End If

    Dim buffer3(14) As Byte
    Dim num As Integer = 0

    Dim bytes As Byte()
    bytes = BitConverter.GetBytes(value)

    Dim nsStream As NetworkStream = Me.nsStream
    SyncLock nsStream
        'Address = (short) Math.Round((double) (((double) Address) / 2.0));
        'Dim bytes As Byte()
        'bytes = BitConverter.GetBytes(Address)

        Dim buffer4 As Byte()
        buffer4 = BitConverter.GetBytes(Address)

        buffer3(0) = 0
        buffer3(1) = 0
        buffer3(2) = 0
        buffer3(3) = 0
        buffer3(4) = 0
        buffer3(5) = 9

        buffer3(6) = 255 '0xff
        buffer3(7) = 16 '0x10;
        buffer3(8) = buffer4(1)
        buffer3(9) = buffer4(0)
        buffer3(10) = 0
        buffer3(11) = 1
        buffer3(12) = 2
        buffer3(13) = bytes(1)
        buffer3(14) = bytes(0)


        Me.nsStream.Write(buffer3, 0, 15)
        Me.nsStream.Flush()

        Do
            Thread.Sleep(1)
            num = num + 1
            If num > 200 Then
                Throw New Exception("超时!")
            End If
        Loop While (Me.t1Client.Client.Available < 12)
        ReDim buffer(Me.t1Client.Client.Available - 1 + 1)
        Me.nsStream.Read(buffer, 0, Me.t1Client.Client.Available)
        Dim ii As Integer
        ii = 0


    End SyncLock



End Sub


    以上是写的过程,发送单个任务,不明白数组是根据MODBUS哪个功能码,对方是如何识别数组当中的数据的,麻烦大神解释一下,看完MODBUS协议,不知道这个是如何进行通信的,为什么数组长度偏偏是15个?
Csdn user default icon
上传中...
上传图片
插入图片