我在写一个串口设备的数据通信问题,现在要以2进制的方法向设备输出数据,如何实现,请高手们赐教!!请提供源代码!!

解决方案 »

  1.   


    Private Sub MSComm_OnComm()
      Dim DataIn As Variant, InData As String, i As Integer,lsStr as string 
      On Error Resume Next   If MSComm.InBufferCount >= 8 Then
          DataIn = MSComm.Input
          glIn() = DataIn
        
          For i = LBound(glIn) To UBound(glIn)
             InData = Trim(InData) + Right("0" & Hex(glIn(i)), 2) + Chr(32)
          Next i    
          lsstr=StrConv(InData , vbUnicode)
       End If
    end sub
    发送时如果是汉字的话要传换成vbformunicode,接收时要把vbFormUnicode转换成vbunicode
    dim Lsout2() as bytte
    LsOut2 = StrConv(Trim(lsName), vbFromUnicode)
      

  2.   

    dim CmdStr as string
    CmdStr ="11001010010"
    MSComm.output=CmdStr & chr(13)
    楼上好像弄错了吧?别人是要发,不是要收。
      

  3.   

    jerrycui(梦随心菲),源码我已经发给你,查收一下吧.
      

  4.   

    我也在写跟机器通讯,能不能也发一份给我:[email protected]
      

  5.   

    我也要。我在写并行口的,你这份也许有帮助 我的油香[email protected]
      

  6.   

    修改MSComm属性InputMode为二进制方式:
    MSComm.InputMode =comInputModeBinary
      

  7.   

    如楼上的所述:
    修改MSComm属性InputMode为二进制方式:
    MSComm.InputMode =comInputModeBinary发送Binary数据到串口,可用一个BYTE数组存储需发送的数值,然后将此数组名传递给MScomm的Output属性即可,以下是发送三个字节的例程:
    dim SendDate(3) as ByteSendDate(0)=10
    SendDate(1)=20
    SendDate(2)=30Comm.Output = SendDate
    若需连续发送且中间要求停顿或需等待一定时间后检查返回数据判断超时,可使用API函数SLEEP。
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    比如:
    SendDate(0)=&H1C
    Comm.Output = SendDate  '发送命令类别码
    Sleep(10)               '等待10毫秒一边下位机处理
    SendDate(0)=&H21
    Comm.Output = SendDate  '发送命令码