通过串口助手读的时候显示数据正常,显示为“01 02 03”
但用VB读串口却显示乱码 估计我VB程序写的肯定有问题 。。 求各位帮忙!(波特率设置没有错,inputmode=0,=1都试了都乱码)
Private Sub Form_Load()
MSComm1.CommPort = 3
MSComm1.Settings = "4800,n,8,1"
MSComm1.InputMode = 1
MSComm1.InputMode = comInputModeText
MSComm1.PortOpen = True
End SubPrivate Sub Timer1_Timer()
Dim buf$
buf = Trim(MSComm1.Input)
If Len(buf) <> 0 Then
Text1.Text = Text1.Text + Chr(13) + Chr(10) + buf
End If
End SubPrivate Sub cmdquit_click()
MSComm1.PortOpen = False
Unload Me
End Sub
但用VB读串口却显示乱码 估计我VB程序写的肯定有问题 。。 求各位帮忙!(波特率设置没有错,inputmode=0,=1都试了都乱码)
Private Sub Form_Load()
MSComm1.CommPort = 3
MSComm1.Settings = "4800,n,8,1"
MSComm1.InputMode = 1
MSComm1.InputMode = comInputModeText
MSComm1.PortOpen = True
End SubPrivate Sub Timer1_Timer()
Dim buf$
buf = Trim(MSComm1.Input)
If Len(buf) <> 0 Then
Text1.Text = Text1.Text + Chr(13) + Chr(10) + buf
End If
End SubPrivate Sub cmdquit_click()
MSComm1.PortOpen = False
Unload Me
End Sub
Dim i As Long
If Len(buf) > 0 Then
Text1.Text = Text1.Text & Chr(13) & Chr(10)
For i = 0 To Len(buf) - 1
Text1.Text = Text1.Text & Right("0" & Hex(buf(i)), 2)
Next i
End If
但是用VB读串口,就是乱码,具体表现是:串口里数据是300以下的,就什么也不显示;是300以上的,就从9开始显示,并递增
但是用VB读串口,就是乱码,具体表现是:串口里数据是300以下的,就什么也不显示;是300以上的,就从9开始显示,并递增
我用单片机给串口发的是一个百位数,比如说123,串口助手读数据的话是显示“00 7B 00”是123的十六进制显示。
但是用VB读串口,就是乱码,具体表现是:串口里数据是300以下的,就什么也不显示;是300以上的,就只显示数据的个位数,十位和百位显示空格
Private Sub Form_Load()
MSComm1.CommPort = 3
MSComm1.Settings = "4800,n,8,1"
MSComm1.InputMode = 1
MSComm1.InputMode = comInputModeText
MSComm1.PortOpen = True
End Sub
Private Sub Timer1_Timer()
Dim buf() As Byte,I As Integer
buf = MSComm1.Input
Text1.Text=Text1.Text & vbCrlf
For I=0 to Ubound(buf)
Text1.Text = Text1.Text & right("0" & Hex(buf(i)),2) & Chr(32)
End If
Text1.Text=Text1.Text & vbCrlf
End Sub
Private Sub cmdquit_click()
MSComm1.PortOpen = False
Unload Me
End Sub我改了Timer函数,运行一下,看下效果在说
Private Sub Form_Load()
MSComm1.CommPort = 3
MSComm1.Settings = "4800,n,8,1"
MSComm1.InputMode = 1
MSComm1.InputMode = comInputModeText
MSComm1.PortOpen = True
End Sub
Private Sub Timer1_Timer()
Dim buf() As Byte,I As Integer
if MSComm1.InputLen>0 then
buf = MSComm1.Input
Text1.Text=Text1.Text & vbCrlf
For I=0 to Ubound(buf)
Text1.Text = Text1.Text & right("0" & Hex(buf(i)),2) & Chr(32)
Next
Text1.Text=Text1.Text & vbCrlf
end if
End Sub
Private Sub cmdquit_click()
MSComm1.PortOpen = False
Unload Me
End Sub
2 用二进制方式接收,接收缓存变量也用 Byte 型。VB 字符串有个毛病,当遇到大于 127 的值时会被系统置换为 "?"。
Dim iBuf() As Byte
Dim bt As Long
bt = bytesTotal
' 收当前流
ReDim iBuf(bt - 1)
tcpRTB.GetData iBuf
' log每个收到的字节
' i = 0
' lnx = "RTB-->BYTE:" + Right("0000000" + Hex(i), 8) + "-"
' For i = 0 To bt - 1
' lnx = lnx + " " + Right("0" + Hex(iBuf(i)), 2)
' If i Mod 16 = 15 Then
' LogRX lnx
' lnx = "RTB-->BYTE:" + Right("0000000" + Hex(i + 1), 8) + "-"
' End If
' Next
' i = bt - 1
' If i Mod 16 <> 15 Then
' LogRX lnx
' End If