我用Mscomm控件想读取串口的数据。
显示在text中
如:
Text1.Text=Mscomm1.input
但显示出来的都是乱码。
如果用binary模式:乱码类似中文字符,但无法辨认
如果用text模式:乱码是一个类似“耳朵”的单个字符
请各位帮忙,谢谢!

解决方案 »

  1.   

    应该是字符集的原因。
    Mscomm1.input中的字节应该转换成字符串。下面的函数希望对你有帮助。
    '字节转换成字符串
    Public Function Bytes2Str(vByteIn() As Byte, sSTROut As String) As Boolean
        Dim stemp As String
        Dim lSize As Long, USize As Long
        Dim varAsc As Long
        Dim i As Long
        
        On Error GoTo err1
        lSize = LBound(vByteIn)
        USize = UBound(vByteIn)
        stemp = ""
        For i = lSize To USize
            varAsc = vByteIn(i)
            If varAsc > 127 Then
                stemp = stemp & Chr("&H" & Hex(vByteIn(i)) & Hex(vByteIn(i + 1)))
                i = i + 1
            Else
                stemp = stemp & Chr(vByteIn(i))
            End If
        Next
        sSTROut = stemp
        Bytes2Str = True
        Exit Function
    err1:
        'debug.Print sSTROut 'Asc(Right(sSTROut, 1)) 'Err.Description
    End Function
      

  2.   

    同意楼上,不过byte数组转字串不用那么麻烦(假如byte数组为buff):
    dim s as string
    s=strconv(buff,vbunicode)基本上就够用了
      

  3.   

    谢谢二位!
    还要请教如何将Mscomm1.input转为byte数组呢?
      

  4.   

    回复人: huaben(华本) ( ) 信誉:100  2004-03-15 10:51:00  得分:0 
     
     
      谢谢二位!
    还要请教如何将Mscomm1.input转为byte数组呢?
      
     
    --------------------
    Dim b() As Byte
    Dim s As Strings = "asdf"
    b = s
    是这个?
      

  5.   

    Private Sub MSComm1_OnComm()
        Dim vstr() As Byte
        vstr = MSComm1.Input '这里过不去。
        Text1.Text = Text1.Text & vstr & vbCrLf
    End Sub是这段代码的问题能帮我吗?我就是想将串口的数据读到文本框中。
      

  6.   

    Private Sub MSComm1_OnComm()
        Dim vstr() As Byte
        dim s as string
        s= MSComm1.Input '这里过不去。
        vstr=s
        Text1.Text = Text1.Text & vstr & vbCrLf
    End Sub
    你看看
      

  7.   

    谢谢flyingscv(zlj)我照您的方法做了,提示的错误是:Compile error:
    Type mismatch可以再帮帮我吗?
      

  8.   

    晕,没注意被误导了,应该不是2进制的问题,上午也没发成功
    Private Sub MSComm1_OnComm()
        Dim vstr() As Byte
        If MSComm1.InBufferCount > 0 Then
            vstr = MSComm1.Input 
            Text1.Text = Text1.Text & vstr & vbCrLf
        end if 
    End Sub
    你试试这样
    这段代码放timer中比较好
      

  9.   

    其它相关
    Public Function AscII2Unicode(ByVal s As String) As String
    '正确返回转换得到的字符串,出错返回""
    On Error GoTo UExpErr
        Dim i As Integer
        Dim r As String
        Dim Char As String * 1
        r = ""
        For i = 1 To Len(s)
            Char = Mid(s, i, 1)
            r = r + Right("00" + Hex$(AscB(RightB(Char, 1))), 2) + Right("00" + Hex$(AscB(Char)), 2)
        Next
        AscII2Unicode = r
        Exit Function
    UExpErr:
        AscII2Unicode = ""
    End FunctionPublic Function Unicode2AscII(ByVal s As String) As String
    '正确返回转换得到的字符串,出错返回""
    On Error GoTo UExpErr
        Dim i As Integer
        Dim r As String
        For i = 1 To Len(s) Step 4
        r = r + ChrB("&H" & Mid(s, i + 2, 2)) & ChrB("&H" & Mid(s, i, 2))
        Next
        Unicode2AscII = r
        Exit Function
    UExpErr:
        Unicode2AscII = ""
    End Function
      

  10.   

    不行,还是老错误。完整的代码如下:Private Sub Form_Load()   ' 保存输入子串的缓冲区
       Dim Instring As String
       ' 使用 COM1。
       MSComm1.CommPort = 1
       ' 9600 波特,无奇偶校验,8 位数据,一个停止位。
       MSComm1.Settings = "9600,N,8,1"
       ' 当输入占用时,
       ' 告诉控件读入整个缓冲区。
       'MSComm1.InputLen = 0
       ' 打开端口。
       MSComm1.PortOpen = True
       MSComm1.InputMode = comInputModeBinaryEnd Sub
    Private Sub Form_Unload(Cancel As Integer)
        ' 关闭串行端口。
       MSComm1.PortOpen = False
    End SubPrivate Sub MSComm1_OnComm()
        Dim vstr() As Byte
        If MSComm1.InBufferCount > 0 Then
            vstr = MSComm1.Input 
            Text1.Text = Text1.Text & vstr & vbCrLf
        end if 
    End Sub