单片机串口发送数据到PC ,用VB设计接收界面,为什么接收到的是乱码
VB程序:PrivateSubForm_Load()MSComm1.Settings="9600,N,8,1"MSComm1.CommPort=4MSComm1.InBu...
VB程序:
Private Sub Form_Load()
MSComm1.Settings = "9600,N,8,1"
MSComm1.CommPort = 4
MSComm1.InBufferSize = 8
MSComm1.OutBufferSize = 2
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.RThreshold = 4
MSComm1.SThreshold = 1
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeText
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.InBufferCount = 0
Me.Caption = "温度"
End Sub
Private Sub MSComm1_OnComm()
Dim rec As String
Select Case MSComm1.CommEvent
Case comEvReceive
rec = MSComm1.Input
Text1.Text = rec
MSComm1.InBufferCount = 0
End Select
End Sub
其中单片机发送的是16进制数 展开
Private Sub Form_Load()
MSComm1.Settings = "9600,N,8,1"
MSComm1.CommPort = 4
MSComm1.InBufferSize = 8
MSComm1.OutBufferSize = 2
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.RThreshold = 4
MSComm1.SThreshold = 1
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeText
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
MSComm1.InBufferCount = 0
Me.Caption = "温度"
End Sub
Private Sub MSComm1_OnComm()
Dim rec As String
Select Case MSComm1.CommEvent
Case comEvReceive
rec = MSComm1.Input
Text1.Text = rec
MSComm1.InBufferCount = 0
End Select
End Sub
其中单片机发送的是16进制数 展开
2个回答
展开全部
首先要确定单片机发送的是什么样的数据,比如温度的原始码还是转换后的温度值,是十六进制值还是ASCII码值,只有在这些都弄清楚了之后,才能在PC端显示出相应的温度值。
更多追问追答
追问
我发送的是16进制值
追答
采用接收缓冲区,然后直接使用就行了。如果有小数点,则需另外处理。
Private Sub MSComm1_OnComm()
Dim rec As String
Select Case MSComm1.CommEvent
Case comEvReceive
rec = MSComm1.Input
Dim BytReceived() As Byte
BytReceived() = rec
Text1.Text = BytReceived(0) '这里根据需要处理。。。
MSComm1.InBufferCount = 0
End Select
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询