我在做VB 与单片机的通信时,发现但单片机发送数据快是,VB会丢失很多的数据
我的数据的速率才300到400bytes/S
速率是9600,n,8,1在速率为19200时收到的全是 10该怎么办? 下面是我的程序:
'360°超声波雷达 串口程序 通过UDP 发送数据给 图像显示程序 图像显示程序为display360.exe  程序必须在同一个路径Private Sub close_Click(Index As Integer)
Command2_Click
End SubPrivate Sub Command1_Click()
If MSComm1.PortOpen <> True Then
    With MSComm1
        .InputMode = comInputModeBinary
        .InputLen = 1
        .InBufferSize = 10000
        .RThreshold = 1
        .SThreshold = 1
        .CommPort = Val(Text1.Text)
        .PortOpen = True
    End With
    Label4.Caption = "串口" + Text1.Text + " 已经打开!"
Else
    MsgBox "端口已打开!"
End If
If MSComm1.PortOpen = True Then
    Winsock1.RemoteHost = Text2.Text
    Winsock1.Bind
    Label3.Caption = "正在向" + Winsock1.RemoteHost + ":" + Str(Winsock1.RemotePort) + "端口发送数据……"
End If
End SubPrivate Sub Command2_Click()
If MSComm1.PortOpen = True Then
    MSComm1.PortOpen = False
    Winsock1.close
    Label3.Caption = "网络连接已经关闭!"
    Label4.Caption = "串口" + Text1.Text + "已经关闭!"
End If
End SubPrivate Sub Command3_Click()
  Call Shell("display360.exe", vbHide)
    
End Sub
 
  
Private Sub exit_Click()
Unload Me
End SubPrivate Sub MSComm1_OnComm()
On Error Resume Next
Dim datas0 As Byte, datas As String, space As Long
Static datas1, c, t, tt As Byte
Static datas2, datas3, datas4 As Byte
Dim Buffer     As VariantIf MSComm1.CommEvent = 2 Then
  
  MSComm1.InputLen = 0
  Buffer = MSComm1.Input  For I = LBound(Buffer) To UBound(Buffer)
        datas0 = Buffer(I)
  Next I
  
  If datas0 = 255 And t < 4 Then
        t = t + 1
  Else
        t = 0
  End If  
    If t = 4 Then '判断是否为有数据
        c = c + 1
        If c = 5 Then '连续取5次数据
            space = datas2 * 10 + datas1 * 1000 + datas3
            Winsock1.SendData &HFF000000
            Winsock1.SendData datas4 * 256 + datas0 '步进电机步进计数
             Winsock1.SendData space
            c = 0
        End If
        
        If datas0 = 255 Then
             tt = tt + 1
        Else
            tt = 0
        End If
        
        If tt = 4 Then
            t = 0
        End If
        
       If c = 1 Then datas1 = datas0 '只在第一次时取数据,取厘米
       If c = 2 Then datas2 = datas0
       If c = 3 Then datas3 = datas0
       If c = 4 Then datas = 4 = datas0
    Else
        c = 0 '当有4个0xFF时,开始取数据
    End If
    Label1.Caption = "当前数据:" + Str(datas0)
End If
End SubPrivate Sub open_Click(Index As Integer)
Command1_ClickEnd SubPrivate Sub show_Click(Index As Integer)
Command3_Click
End Sub

解决方案 »

  1.   

    LZ:你代码看下来是由COM口接收数据并判断控制网口发:
        Winsock1.SendData &HFF000000
        Winsock1.SendData datas4 * 256 + datas0 '步进电机步进计数
         Winsock1.SendData space
    但你接收代码中对Buffer(I)值的判断和数据处理代码似乎存在不妥.
    你是想只要任意字节的值为255(即为&HFF),进行计数.
    我不了解你COM口收到的是啥数据.从代码看可能有不是字节的值为255(即为&HFF)的其它数据.
    Private Sub MSComm1_OnComm()
        On Error Resume Next
        Dim datas0 As Byte, datas As String, space As Long
        Static datas1, c, t, tt As Byte
        Static datas2, datas3, datas4 As Byte
        Dim Buffer As Variant
        If MSComm1.CommEvent = 2 Then
            MSComm1.InputLen = 0
            Buffer = MSComm1.Input
            For I = LBound(Buffer) To UBound(Buffer)
                datas0 = Buffer(I)
                If datas0 = 255 And t < 4 Then 'IF判断代码放在FOR NEXT循环中
                    t = t + 1
                Else
                    t = 0
                End If
            Next I
            If t = 4 Then '判断是否为有数据
                c = c + 1
                If c = 5 Then '连续取5次数据
                    space = datas2 * 10 + datas1 * 1000 + datas3
                    Winsock1.SendData &HFF000000
                    Winsock1.SendData datas4 * 256 + datas0 '步进电机步进计数
                    Winsock1.SendData space
                    c = 0
                End If
                If datas0 = 255 Then
                    tt = tt + 1
                Else
                    tt = 0
                End If
                If tt = 4 Then
                    t = 0
                End If
                If c = 1 Then datas1 = datas0 '只在第一次时取数据,取厘米
                If c = 2 Then datas2 = datas0
                If c = 3 Then datas3 = datas0
                If c = 4 Then datas = 4 = datas0
            Else
                c = 0 '当有4个0xFF时,开始取数据
            End If
            Label1.Caption = "当前数据:" + Str(datas0)
        End If
    End Sub
      

  2.   


    如果是这样,你的datas0由于串口的特点是不定值.
      

  3.   

    串口的数据是不定的,我是按数据包送数据的,一包一包的,包开头是0xff0xff0xff0xff。四个0xff保证了和有用数据的不同。 有用数据可能是任意的数据。但不会连续超过四个0xff。winsock是网络部分的,应该不关事吧!!!请各位仔细看看,哪里有问题啊!!!比较急啊!!!  
      

  4.   

    If datas0 = 255 And t < 4 Then 
            t = t + 1 
      Else 
            t = 0 
      End If 这段有些看不明白!  按你程序的意思是接受4次了 t才能=4啊.zdingyun写的意思是判断接收到的数据是4字节。 考虑一下这个地方