首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

VB中通信 mscomm有关问题

2012-01-19 
VB中通信 mscomm问题下位机发送指令:0X23 OX03 OX00 OX51 校验和上位机判断指令正确后,向下位机发送:OX21

VB中通信 mscomm问题
下位机发送指令:0X23 OX03 OX00 OX51 校验和
上位机判断指令正确后,向下位机发送:
OX21 OXxx(xx表示后面的字节长度) 0X00 0X51 当前值(四字节) 校验和

谁能告诉我上位机的代码该怎样写 ?有点茫然

[解决办法]

VB code
Option Explicit    Dim strData As String                                               Private Sub send()    Dim i As Integer    Dim sendSj(4) As Byte    sendSj(0) = &H21    sendSj(1) = &H3    sendSj(2) = &H0    sendSj(3) = &H51    sendSj(4) = (sendSj(0) + sendSj(1) + sendSj(2) + sendSj(3)) Mod 256    MSComm1.Output = sendSjEnd SubPrivate Sub Form_Load()    MSComm1.InputMode = comInputModeBinary    MSComm1.RThreshold = 1    MSComm1.PortOpen = True    Text1 = ""End SubPrivate Sub MSComm1_OnComm()    Dim buffer() As Byte    Dim i As Integer    Select Case MSComm1.CommEvent        Case 2            buffer = MSComm1.Input            For i = 0 To UBound(buffer)                If Len(Hex(buffer(i))) = 1 Then                    strData = strData & "0" + Hex(buffer(i))                Else                    strData = strData & Hex(buffer(i))                End If            Next i            If Mid(strData, 1, 2) = "23" And Len(strData) = 10 Then                Text1.Text = strData                send                strData = ""            End If    End SelectEnd Sub
[解决办法]
VB code
Private Function GetCheckSum(a() As Byte, ByVal iBegin As Long, ByVal iEnd As Long) As Long    Dim l As Long    Dim i As Long        For i = iBegin To iEnd        l = l + a(i)    Next        GetCheckSum = lEnd FunctionPrivate Sub MSComm1_OnComm()    Dim buffer() As Byte    Dim send() As Byte    Dim l As Long    Dim i As Integer    Select Case MSComm1.CommEvent        Case comEvReceive            buffer = MSComm1.Input            For i = 0 To UBound(buffer)                strData = strData & Right$("0" & Hex(buffer(i)), 2)            Next i            If Hex(GetCheckSum(buffer, 0, 3) Mod 256) = buffer(4) Then                Text1.Text = strData                strData = ""                ReDim send(8)                send(0) = &H21                send(1) = 7    '先按校验和1字节计算                send(2) = &H0                send(3) = &H51                send(4) = "&h" & Mid(Label2.Caption, 1, 2)                send(5) = "&h" & Mid(Label2.Caption, 3, 2)                send(6) = "&h" & Mid(Label2.Caption, 5, 2)                send(7) = "&h" & Mid(Label2.Caption, 7, 2)                l = GetCheckSum(send, 0, 7)                If l <= 255 Then                    send(8) = l                Else    '校验和最多2字节                    ReDim Preserve send(9)                    send(1) = 8                    l = GetCheckSum(send, 0, 7)                    send(8) = l Mod 256                    send(9) = l \ 256                End If                MSComm1.Output = send            End If    End SelectEnd SubPrivate Sub Text2_Change()    Label2.Caption = Right$("0000000" & Hex(Val(Text2.Text)), 8)End Sub 

热点排行