Click to See Complete Forum and Search --> : serial Buffer issue with VB.net 2005


Robbo99
January 13th, 2009, 03:52 PM
Just wondering how to solve this problem i have a stream of data coming in to my serial port which is the following at the moment however in will be a random number 0 - 512 and it repeats:


190
120
150
110


i'm reading it with:


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
current_time.Text = DateTime.Now.ToLocalTime

SerialPort1.Open()

Dim data As String
data = SerialPort1.ReadLine()
SerialPort1.Close()

Dim Setup_A_data As New StreamWriter("serial_data.text", True)
Setup_A_data.WriteLine(data)


Setup_A_data.Close()

end sub


the output im getting is


0
120
150
1110
190
120
1
120
150
110
150
110
190
10
190
120
1
120
150
110
150

the output should be equal to the input from the serial line but its not
do i need to be using a request to send or something else and if so how would i archive this?
Thanks Michael

dglienna
January 14th, 2009, 02:31 PM
Check the value of the buffer. Perhaps send a ENDLINE or some other character instead.

DataMiser
January 15th, 2009, 02:29 AM
Checking the value of the buffer is usually a good idea.
Also if you are using the readline method to get data then the data should be comming in as a line with the proper CRLF terminators.

In any case it would not seem to be a good idea to open the port, read a line from it then close it all within a tick of the timer. There are many possible issues here.

The port should be opened outside the timer routine and left open as long as data is expected. The code in the timer should first check to see if any data is there and then read what is there, either one line at a time or a block of data or all the data currently present. The port should remain open to continue receiving data into the buffer which will be there to read on the next timer cycle.

Opening an closing the port in such a way as shown in the op pretty much does away with any chance of receiving data at any time other than the fraction of a second where the port is actually open. On a fast pc odds are this routine would never receive data without using a breakpoint on the data= line. It would also discard any data that may come after a end line sequence and clear it from the buffer effectively loosing that data should it exist.