如何使用vb.net从usb到串口转换器读取数据到文本框? [英] How to read data from usb to serial port converter into textbox using vb.net ?

查看:105
本文介绍了如何使用vb.net从usb到串口转换器读取数据到文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用vb.net从串口读取数据到文本框时遇到问题,我只是尝试了很多,直到现在我没有得到任何解决方案,请帮我解决这个问题!我有一个表单,按钮接收和文本框显示收到的数据!我使用了msdn网络的以下功能!



函数ReceiveSerialData()As String

'从串口接收字符串。

Dim returnStr As String =



Dim com1 As IO.Ports.SerialPort = Nothing

com1 = My.Computer.Ports.OpenSerialPort(COM3)

com1.ReadTimeout = 1000

Do

Dim Incoming As String = com1。 ReadLine()

如果输入什么都没有那么

退出运行

否则

returnStr&= Incoming&vbCrLf

结束如果

循环

返回returnStr

结束函数
运行此代码后
我的输出就像操作已超时。



i只想将体重秤连接到我的vb.net应用程序中!我不知道怎么办!我是这个问题的初学者!提前谢谢!

i刚试过的SerialPort.DataReceived事件也显示错误信息操作已超时。

如果我的秤没有发送该数据意味着如何解决它?我需要改变规模吗?或者有没有办法请求我的比例发送握手信号等数据?

I have an problem in reading data from serial port into text box using vb.net, i just tried a lot till now i didn't get any solutions for that please help me out of this problem! i have one form,button to receive and text box to show received data! i have used the below function from msdn networks!

Function ReceiveSerialData() As String
' Receive strings from a serial port.
Dim returnStr As String = ""

Dim com1 As IO.Ports.SerialPort = Nothing
com1 = My.Computer.Ports.OpenSerialPort("COM3")
com1.ReadTimeout = 1000
Do
Dim Incoming As String = com1.ReadLine()
If Incoming Is Nothing Then
Exit Do
Else
returnStr &= Incoming & vbCrLf
End If
Loop
Return returnStr
End Function
after running this code i got output like "The operation has timed out."

i just want to connect weight scale into my vb.net application! i don't know how to do ! i am beginner to this problem! thanks in advance!
i just tried SerialPort.DataReceived event also shows the error message "The operation has timed out."
if my scale doesn't send that data means how to solve it? am i need to change the scale? or is there any way to request my scale to send data like handshaking signals?

推荐答案

处理SerialPort.DataReceived事件并缓冲数据。这样,当比例实际提供时,你只处理来自串口的数据。

缓冲它的原因很简单:你可以在收到每个角色时触发一个事件,所以你可能无法一次性获得完整信息。因此检查字符,如果不是行结束,则将其添加到StringBuilder,直到EOL到达为止。然后使用Invoke将数据传输到文本框(因为DataRecieved总是在非UI线程上)。



这种方法意味着您的主代码不必等待消息 - 它只是启动串口来做它自己的事情并让它继续下去。
Handle the SerialPort.DataReceived event and buffer up the data. That way, you only process data from the serial port when the scale actually provides it.
The reason to buffer it is simple: you can get an event triggered for each individual character as it is received, so you may not get a "full" message in one go. So check the character and if it isn't the line end add it to a StringBuilder until the EOL does arrive. Then transfer the data to the textbox using Invoke (as DataRecieved is always on a non-UI thread).

This approach means that you main code doesn't have to "wait" for messages - it just kicks off the serial port to do it's own thing and lets it get on with it.


这篇关于如何使用vb.net从usb到串口转换器读取数据到文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆