当前位置:编程学习 > VB >>

VB串口接收问题

我使用VB串口接收仪器传送出来的数据不能正常在textbox里头正确显示出来。
比例:正确的数据是23.111 在textbox显示出来的会是 | 23.111 ,在数据的前面就出现了| 这样的字段。


Private Sub Form_Load()

MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,2"
MSComm1.RThreshold = 5 '2
MSComm1.InputMode = comInputModeText
MSComm1.InputLen = 0
MSComm1.PortOpen = True

Private Sub MSComm1_OnComm()
Dim Inbuffer As String


Inbuffer = MSComm1.Input
If txtdata1.Text = "" Then
txtdata1.Text = Inbuffer
Else
If txtdata2.Text = "" Then
txtdata2.Text = Inbuffer
Else
If txtdata3.Text = "" Then
txtdata3.Text = Inbuffer
Else
If txtdata4.Text = "" Then
txtdata4.Text = Inbuffer
Else
If txtdata5.Text = "" Then
txtdata5.Text = Inbuffer
End If
End If
End If
End If
End If


End Sub

有用过debug.print Asc 显示出来的东西就很奇怪了:
Dim I as Integer
For I = 1 to Len(Inbuffer)
Debug.Print Asc(Mid$(Inbuffer, I, 1))
Next I

debug.print text1.text = 32 32 32 48 46 48 48
debug.print text2.text = 55 13 32 32 32 32 48 46 48 48 48
debug.print text3.text = 49 13 32 32 32 48 46 48 48 48
debug.print text4.text = 49 13 32 32 32 48 46 48 48 48 53

请帮帮忙。谢谢。

--------------------编程问答-------------------- 1)这很正常,有些控制字符在文本框中显示为特殊字符.
例:



LZ可以依据通信协议约定的数据帧结构取出正确数据.
2)按LZ须接收的数据至少是6字节长,加上数据帧首尾字符不止6字节长.
你设置的
MSComm1.RThreshold = 5
是无法正确接收数据的. --------------------编程问答-------------------- 感觉最近很多人都在问串口操作问题,都没见有人问 USB 通讯问题,看来 MAX232 是越来越好卖了,
难怪 MAX232 才8毛钱, D12最少都要8块钱, 这就是有产量的效果,呵呵 --------------------编程问答-------------------- 楼主的可以不要使用文本方式接受,使用二进制方式来通信,这样接收到的就是字符的ASCII编码,参考一下这个:
http://download.csdn.net/source/1262066http://download.csdn.net/source/1421180
补充:VB ,  控件
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,