我用VB做了个程序,发给单片机。发送十六进制“F0”,单片机接收后返回“F0 0D 0A F0”,但我现在接收到“32 34 30 F0”,把大致程序发一下(接收部分):
dim buffer as variant
dim byte(0) as byte
buffer = mscomm1.input
byte(0) = AscB(buffer)
text1.text = text1.text + byte(0) 
我是用二进制接收的。mscomm1.inputlen=0
接收一个字节就触发oncomm事件

解决方案 »

  1.   

    dim Byte() as byte
    Byte=Mscomm1.Input
      

  2.   

    Private Sub cmdSend_Click()
    Dim bytData(0) As Byte
    bytData(0) = &HF0
    Call SendData(bytData)
    End SubPublic Function SendData(ByRef bytData() As Byte) As Long
        On Error Resume Next
        MSComm1.InBufferCount = 0 '清空接收缓冲区
        MSComm1.Output = bytData '发送数据
        Do
            DoEvents
        Loop Until MSComm1.OutBufferCount = 0 '等待,直到数据发送完毕
        MSComm1.OutBufferCount = 0          '清空发送缓冲区
    End Function
    Private Sub Form_Load()
        MSComm1.CommPort = 1                   'COM端口
        MSComm1.Settings = "9600,n,8,1"
        MSComm1.InputMode = comInputModeBinary      '采用二进制传输
        MSComm1.InBufferCount = 0   '清空接受缓冲区
        MSComm1.OutBufferCount = 0  '清空传输缓冲区
        MSComm1.RThreshold = 1      '产生MSComm事件
        MSComm1.PortOpen = True '打开端口
        Text3 = ""
    End SubPrivate Sub MSComm1_OnComm()
    On Error Resume Next
        Dim BytReceived() As Byte
        Dim strBuff As String
        Dim strData As String
        Dim i As Integer
        Dim x As Integer
        Select Case MSComm1.CommEvent
            Case 2
                MSComm1.InputLen = 0
                strBuff = MSComm1.Input
                BytReceived() = strBuff
                For i = 0 To UBound(BytReceived)
                    If Len(Hex(BytReceived(i))) = 1 Then
                        strData = strData & "0" & Hex(BytReceived(i))
                    Else
                        strData = strData & Hex(BytReceived(i))
                    End If
                Next
                Text3 = Text3 + strData
                If Left(strData, 2) = "F0" And Len(strData) = 8 Then
                    Text1.Text = strData
                    Call DataClear
                End If
        End Select
    End SubPublic Sub DataClear()
        MSComm1.OutBufferCount = 0   '清空发送缓冲区
        MSComm1.InBufferCount = 0
        Text3 = ""
    End Sub
      

  3.   

    ' 超时通讯Public Function IT800D2_GetCommData(cMsCOMM As MSComm, Optional strFrameHead As String = "FEFE68", Optional lngOvertime As Long = 30000) As Long
        Dim bytBuffer() As Byte
        Dim strTemp As String, lret As Long
        Dim i As Long, k As Long, lr As Long, lngPos As Long, lngLen As Long
        Dim lngStartTime As Long, strReturn As String, strFrame As String    strReturn = "Failed"
        lngStartTime = GetTickCount()
        strTemp = ""
        Do
            DoEvents
            If cMsCOMM.InBufferCount Then
                bytBuffer = cMsCOMM.Input
                For i = 0 To UBound(bytBuffer)
                    strTemp = Right("00" & Hex$(bytBuffer(i)), 2)
                    strCommGetStringEx = strCommGetStringEx & strTemp
                Next
            End If
            DoEvents
        Loop Until GetTickCount() - lngStartTime >= lngOvertime
        
        IT800D2_GetCommData = lr
    End Function
      

  4.   

    to zdingyun
    我照你的程序试了一下,发送“F0”本应试收到“F0 0D 0A F0”,现在只能收到“F0”,后面的全部都收不到。
    哪位好心人帮帮我。很急的~~ 小妹的QQ是3041101。请加我。谢谢
      

  5.   

    将MSComm1的属性RThreshold设置为4:
    Private Sub Form_Load()
        MSComm1.CommPort = 1                   'COM端口
        MSComm1.Settings = "9600,n,8,1"
        MSComm1.InputMode = comInputModeBinary      '采用二进制传输
        MSComm1.InBufferCount = 0   '清空接受缓冲区
        MSComm1.OutBufferCount = 0  '清空传输缓冲区
        MSComm1.RThreshold = 4      '产生MSComm事件
        MSComm1.PortOpen = True '打开端口
        Text3 = ""
    End SubPrivate Sub MSComm1_OnComm()
    On Error Resume Next
        Dim BytReceived() As Byte
        Dim strBuff As String
        Dim strData As String
        Dim i As Integer
        Dim x As Integer
        Select Case MSComm1.CommEvent
            Case 2
                MSComm1.InputLen = 0
                strBuff = MSComm1.Input
                BytReceived() = strBuff
                For i = 0 To UBound(BytReceived)
                    If Len(Hex(BytReceived(i))) = 1 Then
                        strData = strData & "0" & Hex(BytReceived(i))
                    Else
                        strData = strData & Hex(BytReceived(i))
                    End If
                Next
                Text3 = Text3 + strData
                If Left(strData, 4) = "F00D" And Len(strData) = 8 Then
                    Text1.Text = strData
                    Call DataClear
                End If
        End Select
    End Sub
      

  6.   

    to zdingyun 万分感谢,这个问题解决了。请问怎么给分?
    PS:还有个问题,我从单片机接收了数据过来,现在又需要把从单片机接收的数据再从串口发回到单片机。如:发送F0,单片机回:F0,发送0F,单片机回09(这个数据是随机的,不是固定的)。现在我需要把这个随机的数据再从串口发到单片机中,如,接着发送09,单片机回09 0F。现在我的问题是:发送F0以及0F可以接收到F0 09,但是再将09发回去就不行了,必须要按两次cmdSend_Click 才行。而且按第一次的时候,在MSCOMM1_OnComm函数中,随机数a1可以显示在text2中,但不能显示在SendData2函数中的text4,要按第二次cmdSend_Click ,才能显示在text4中。但是从单片机接收到的数据就不对了。
    我照你的程序大致如下:dim a1 as variant '定义a1为全局变量
    Private Sub cmdSend_Click()
    Dim bytData(0) As Byte
    dim bytData(0) As Byte
    dim bytData2 As variant
    bytData(0) = &HF0
    bytData1(0)=&HFCall SendData(bytData)
    call SendData1(bytData1)
    bytData2 = a1
    call SendData2(bytData2)
    End SubPublic Function SendData2(ByRef bytData2 As variant) As Long
        text4.text = a1
        On Error Resume Next
        MSComm1.InBufferCount = 0 '清空接收缓冲区
        MSComm1.Output = bytData2 '发送数据
        Do
            DoEvents
        Loop Until MSComm1.OutBufferCount = 0 '等待,直到数据发送完毕
        MSComm1.OutBufferCount = 0          '清空发送缓冲区
    End Function
    Public Function SendData1(ByRef bytData1() As Byte) As Long
        On Error Resume Next
        MSComm1.InBufferCount = 0 '清空接收缓冲区
        MSComm1.Output = bytData 1'发送数据
        Do
            DoEvents
        Loop Until MSComm1.OutBufferCount = 0 '等待,直到数据发送完毕
        MSComm1.OutBufferCount = 0          '清空发送缓冲区
    End Function
    Public Function SendData(ByRef bytData() As Byte) As Long
        On Error Resume Next
        MSComm1.InBufferCount = 0 '清空接收缓冲区
        MSComm1.Output = bytData '发送数据
        Do
            DoEvents
        Loop Until MSComm1.OutBufferCount = 0 '等待,直到数据发送完毕
        MSComm1.OutBufferCount = 0          '清空发送缓冲区
    End Function
    Private Sub Form_Load()
        MSComm1.CommPort = 1                   'COM端口
        MSComm1.Settings = "9600,n,8,1"
        MSComm1.InputMode = comInputModeBinary      '采用二进制传输
        MSComm1.InBufferCount = 0   '清空接受缓冲区
        MSComm1.OutBufferCount = 0  '清空传输缓冲区
        MSComm1.RThreshold = 1      '产生MSComm事件
        MSComm1.PortOpen = True '打开端口
        Text3 = ""
    End SubPrivate Sub MSComm1_OnComm()
    On Error Resume Next
        Dim BytReceived() As Byte
        Dim strBuff As String
        Dim strData As String
        Dim i As Integer
        Dim x As Integer
        Select Case MSComm1.CommEvent
            Case 2
                MSComm1.InputLen = 0
                strBuff = MSComm1.Input
                BytReceived() = strBuff
                For i = 0 To UBound(BytReceived)
                    If Len(Hex(BytReceived(i))) = 1 Then
                        strData = strData & "0" & Hex(BytReceived(i))
                    Else
                        strData = strData & Hex(BytReceived(i))
                    End If
                Next
                Text3 = Text3 + strData
                If Left(strData, 2) = "F0" And Len(strData) = 4 Then
                    Text1.Text = strData
                    text2.text = right(strData,2)
                    a1 = text2.text
                    Call DataClear
                End If
        End Select
    End SubPublic Sub DataClear()
        MSComm1.OutBufferCount = 0   '清空发送缓冲区
        MSComm1.InBufferCount = 0
        Text3 = ""
    End Sub
    麻烦你帮我看一下,谢谢,非常感谢~~
      

  7.   

    我试了试把call SendData2(bytData2)这个函数放在MSComm1_OnComm()中,还是出现我上面所说的同样的问题。要按二次cmdSend_Click ,才能显示在text4中,但是从单片机接收到的数据还是不对。
      

  8.   

    你的通信协议应描述清楚:
        VB发 单片机回复
         F0  F00D0AF0(发贴时间2007-5-9 23:38:27 )
        如按以下要求,整个通信代码算法和结构要作修改
        VB发 单片机回复
         F0  F0      (发贴时间2007-5-14 19:53:16)
         0F  09
         09  **
        ??
      

  9.   

    VB发                               单片机回复
    F0(固定的)                       F0(固定的)
    0F(固定的)                       09(随机数)
    09(要跟这个随机数相同)           0F(固定的)
    现在我发F0跟0F可以分别收到F0跟09,这个是正确的,我截获随机数09后,再从VB中将这个随机数(如09)发到单片机中,就收不到0F了。我把发送那个随机数的函数放到MSComm1_OnComm()中,可以发送到单片机中。但是收不到0F。
      

  10.   

    你应修改
    MSComm1_OnComm中的:
    If Left(strData, 2) = "F0" And Len(strData) = 4 Then
        Text1.Text = strData
        text2.text = right(strData,2)
        a1 = text2.text
        Call DataClear
    End If
     为          
    Text1.Text = strData
    Text2.text = Mid(strData,1,2)
    a1 = Text2.text
    Call DataClear这是因为返回为1字节数据.
    再作其它考虑.
      

  11.   

    我照你的改了。正确的应该是:
    VB发                               单片机回复
    F0(固定的)                       F0(固定的)
    0F(固定的)                       09(随机数)
    09(要跟前一个随机数相同)         0F(固定的)
    我的问题是:我发了F0、0F、随机数(09)后,只收到F0跟随机数(如09),没有收到0F。我把发送随机数的那个函数中的mscomm1.rthreshold以及inputlen改了无数次,都收不到0F。我确定把随机数发送过去了。我设置断点运行的时候可以运行到发送随机数的那个函数。
    我程序如下:
    dim a1 as variant
    dim bytdata4(0) as variantprivate sub 发送_click()
    bytdata(0) = &HF0
    bytdata1(0) = &H0F
    call senddata(bytdata)   '发送F0
    call senddata1(bytdata1) '发送0F
    call senddata4(bytdata4) '发送随机数
    end subprivate function hex2(c as variant) as long '将随机数转为16进制数再发送
    hex2 = Val("&H" + c)
    if len(hex2<2) then
    hex2="0" & hex2
    end if
    end functionpublic function senddata4(byref bytdata4() as variant) as long
    bytdata4(0) = a1
    on error resume next
        MSComm1.InBufferCount = 0 '清空接收缓冲区
        mscomm1.rthreshold = 5
        MSComm1.Output = bytdata4(0) '发送数据
        Do
            DoEvents
        Loop Until MSComm1.OutBufferCount = 0 '等待,直到数据发送完毕
        MSComm1.OutBufferCount = 0          '清空发送缓冲区
    End Functionprivate sub mscomm1_oncomm()
    On Error Resume Next
        Dim BytReceived() As Byte
        Dim strBuff As String
        Dim strData As String
        Dim i As Integer
        Dim x As Integer
        Select Case MSComm1.CommEvent
            Case 2
                MSComm1.InputLen = 0
                strBuff = MSComm1.Input
                BytReceived() = strBuff
                For i = 0 To UBound(BytReceived)
                    If Len(Hex(BytReceived(i))) = 1 Then
                        strData = strData & "0" & Hex(BytReceived(i))
                    Else
                        strData = strData & Hex(BytReceived(i))
                    End If
                Next i
        a1 = mid(strdata,1,2)
        call hex2(a1)
        call dataclear
        end select
        end sub
      

  12.   

    to zdingyun  
    help me ~~~
      

  13.   

    你应该用串口调试精灵来调试.代码在:
    http://zhidao.baidu.com/question/24721391.html
    供参考,代码可修改用于你的通信控制.
      

  14.   

    to zdingyun  
    如何将text文本框中的数据用十六进制的方式发送出去?
      

  15.   

    txtSend文本框内写16进制数据,然后按手动发送,就发送一次。
      

  16.   

    将文本框内的16进制数,转换后存到1个byte变量,发送就行了!我就用它写的modbus RTUPrivate Sub Command3_Click()                                '发送数据
              
           Dim test(1) As Byte
                  
           test(0) = Val("&H" & Text3.Text)      '组织 通讯读指令
              MSComm1.OutBufferCount = 0               '清空输出寄存器           
        MSComm1.Output = test
        
        
    End Sub
      

  17.   

    非常感谢zdingyun以及 hawk7055
    问题已经全部解决了。多谢。~~~
      

  18.   

    to zdingyun
    已给分了。~~