Click here to Skip to main content
15,032,024 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have an problem in reading data from serial port into text box using, 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
Dim Incoming As String = com1.ReadLine()
If Incoming Is Nothing Then
Exit Do
returnStr &= Incoming & vbCrLf
End If
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 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?
Updated 5-May-15 18:34pm
RK Samy 4-May-15 6:23am
Thank you for your valuable response sir,i just tried SerialPort.DataReceived event sir... but it is also shows me "The operation has timed out.".. sir is there any other way to solve this issue?
Ralf Meier 6-May-15 7:04am
Does the device, you have contacted to the SerialPort, communicates by itself ?
Or does it only give you an answer if you call it in the right way ?

What device (exactly !!!) do you have ?
RK Samy 6-May-15 7:50am
i am using weight scale to measure weight ! i don't how to make communication by itself? can you explain about that?
Ralf Meier 6-May-15 9:02am
No - I can't because I don't know which device you have.
Perhaps you post the Device-Name and some more Info about it.
I think, that this device need a kind of Communication-Protocol. You must send a specific command to the device and after that you will get an answer.
The device will not poll it's value by itself ...

1 solution

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900