现在已经在装载窗口时对串口进行了初始化:Private Sub Form_Load()
Mscomm1.CommPort = 1
Mscomm1.PortOpen = True
MSComm1.Settings = "9600,n,8,1"
Mscomm1.OutBufferCount = 0
MSComm1.InBufferCount = 0
MSComm1.InputMode = 1
MSComm1.InputLen = 5
MSComm1.RThreshold = 5
End Sub
发送指令各处理数据部分程序该怎样入手呢?

解决方案 »

  1.   

    留下email,给你一个mscomm通信程序的完整例子,今晚12点前有效。
      

  2.   

    [email protected]给我发一个,谢谢!
      

  3.   

    Option Explicit
    Dim a() As Byte
    Dim b() As LongPrivate Function CheckSum(aBytes As Byte) '检验和
    Dim i As Integer, Sum As Integer
        Sum = 0
        For i = 0 To aBytes - 1
            Sum = Sum + a(i)
        Next i
        CheckSum = Sum Mod 256
    End FunctionPrivate Sub StopAll() '全部停止
    ReDim a(5) As Byte
        
        a(0) = 2
        a(1) = txtID.Text
        a(2) = 14
        a(3) = 0
        a(4) = 0
        a(5) = CheckSum(5)
        MSComm1.Output = a
        Do While (MSComm1.OutBufferCount <> 0)
        Loop
        Call showdata
        For i = 0 To 1000000
        Next i
        a(0) = 2
        a(1) = txtID.Text
        a(2) = 1
        a(3) = 0
        a(4) = 0
        a(5) = CheckSum(5)
        MSComm1.Output = a
        Do While (MSComm1.OutBufferCount <> 0)
        Loop
        Call showdata
    End SubPrivate Sub Form_Load()
    On Error GoTo err
    txtAddress.Text = 255
    txtSpeed.Text = 180 MSComm1.CommPort = 3
    MSComm1.Settings = "4800,n,8,1"
    If MSComm1.PortOpen = False Then
       MSComm1.PortOpen = True
    End Iferr:
    End SubPrivate Sub Form_Unload(Cancel As Integer)
    If MSComm1.PortOpen Then
       MSComm1.PortOpen = False
    End If
    End Sub
    Private Sub cmdUp_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) '上
    ReDim a(5) As Byte    a(0) = 2
        a(1) = txtID.Text
        a(2) = 14
        a(3) = 0 '水平方向速度为0
        a(4) = txtSpeed.Text
        a(5) = CheckSum(5)
        MSComm1.Output = a '设置速度
        Do While (MSComm1.OutBufferCount <> 0)
        Loop
      
         a(0) = 2
        a(1) = txtID.Text
        a(2) = 1
        a(3) = 4
        a(4) = 0
        a(5) = CheckSum(5)
        MSComm1.Output = a '设置上
        Do While (MSComm1.OutBufferCount <> 0)
        Loop
    End SubPrivate Sub cmdUp_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    StopAll
    End SubPrivate Sub cmdLeft_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) '左
    ReDim a(5) As Byte
        
        a(0) = 2
        a(1) = txtID.Text
        a(2) = 14
        a(3) = txtSpeed.Text
        a(4) = 0 '垂直方向速度为0
        a(5) = CheckSum(5)
        MSComm1.Output = a '设置速度
        Do While (MSComm1.OutBufferCount <> 0)
        Loop    a(0) = 2
        a(1) = txtID.Text
        a(2) = 1
        a(3) = 2
        a(4) = 0 '垂直方向速度为0
        a(5) = CheckSum(5)
        MSComm1.Output = a '设置左
        Do While (MSComm1.OutBufferCount <> 0)
        LoopEnd SubPrivate Sub cmdLeft_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    StopAll
    End SubPrivate Sub cmdRight_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) '右
    ReDim a(6) As Byte    a(0) = 2
        a(1) = txtID.Text
        a(2) = 14
        a(3) = txtSpeed.Text
        a(4) = 0 '垂直方向速度为0
        a(5) = CheckSum(5)
        a(6) = 0
        MSComm1.Output = a '设置速度
        Do While (MSComm1.OutBufferCount <> 0)
        Loop    a(0) = 2
        a(1) = txtID.Text
        a(2) = 1
        a(3) = 1
        a(4) = 0
        a(5) = CheckSum(5)
        a(6) = 0
        MSComm1.Output = a '设置右
        Do While (MSComm1.OutBufferCount <> 0)
        LoopEnd SubPrivate Sub cmdRight_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    StopAll
    End SubPrivate Sub cmdDown_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) '下
    ReDim a(5) As Byte
        
        a(0) = 2
        a(1) = txtID.Text
        a(2) = 14
        a(3) = 0 '水平方向速度为0
        a(4) = txtSpeed.Text
        a(5) = CheckSum(5)
        MSComm1.Output = a '设置速度
        Do While (MSComm1.OutBufferCount <> 0)
        Loop    a(0) = 2
        a(1) = txtID.Text
        a(2) = 1
        a(3) = 8
        a(4) = 0
        a(5) = CheckSum(5)
        MSComm1.Output = a '设置下
        Do While (MSComm1.OutBufferCount <> 0)
        LoopEnd SubPrivate Sub cmdDown_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    StopAll
    End Sub
    Private Sub Timer1_Timer()
    MSComm1.Output = a
    End Sub