Discussion:
Reading from a serial port
Roger Clary
2012-01-03 20:54:42 UTC
Permalink
I am attempting to read from a UART device connected to a USB port (Mac and Win). To communicate with the device, I write a short string (5-8 chars) and then read a string returned from the device up to 127 chars. but of varying lengths. The problem I am having is that the DataAvailable event of the serial control, running asynchronously, may not contain the complete return string when it fires. I am currently jumping through many hoops trying to check the validity of the returned string, but hoping there is a way I can delay by even half a second data appearing in the dataAvailable event so that I can be assured that the complete string is there when I parse it.
If I can figure out how to wait even 500 m/s to check the dataAvailable event after doing a "write" to the serial control, I think the whole string will be there. So far, I can't get this to work.

Suggestions gratefully accepted.


Roger Clary
Class One Software
http://www.classonesoftware.com
Educational Software for Lifelong Learning





_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
Jon Ogden
2012-01-03 21:05:16 UTC
Permalink
Create a timer. Set the timer's period to 500 mSec. When you send the data to the serial port, start the timer. In the action event of the timer, Do a serial.readall and put that into a property or text box or whatever you want to do with it. Then set the timer to off.

The way I do it for my stuff is have a timer that runs continuously doing a readall and appending it to a text area control. I think this period is 10 mS. In order to type in the window control and interact with the device, I put some blocking code in that basically turns my read off in the timer (I leave the timer firing, just set a boolean called NoRead) as I am typing. I then capture the received data in the key down event and throw it away. Once I'm done typing, I set NoRead to False and let things continue. This works very well and I never get duplicate characters and I see everything in real time (well, w/in 10 mS). My only problem is that most serial or telnet connections contain specific control characters that are part of the VT-100 or ANSI control set and I've not figured o
ut how to properly interpret or deal with those yet.

Jon
Post by Roger Clary
I am attempting to read from a UART device connected to a USB port (Mac and Win). To communicate with the device, I write a short string (5-8 chars) and then read a string returned from the device up to 127 chars. but of varying lengths. The problem I am having is that the DataAvailable event of the serial control, running asynchronously, may not contain the complete return string when it fires. I am currently jumping through many hoops trying to check the validity of the returned string, but hoping there is a way I can delay by even half a second data appearing in the dataAvailable event so that I can be assured that the complete string is there when I parse it.
If I can figure out how to wait even 500 m/s to check the dataAvailable event after doing a "write" to the serial control, I think the whole string will be there. So far, I can't get this to work.
Suggestions gratefully accepted.
Roger Clary
Class One Software
http://www.classonesoftware.com
Educational Software for Lifelong Learning
_______________________________________________
<http://www.realsoftware.com/support/listmanager/>
<http://support.realsoftware.com/listarchives/lists.html>
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
Norman Palardy
2012-01-03 21:06:53 UTC
Permalink
Post by Roger Clary
I am attempting to read from a UART device connected to a USB port
(Mac and Win). To communicate with the device, I write a short
string (5-8 chars) and then read a string returned from the device
up to 127 chars. but of varying lengths. The problem I am having is
that the DataAvailable event of the serial control, running
asynchronously, may not contain the complete return string when it
fires. I am currently jumping through many hoops trying to check the
validity of the returned string, but hoping there is a way I can
delay by even half a second data appearing in the dataAvailable
event so that I can be assured that the complete string is there
when I parse it.
If I can figure out how to wait even 500 m/s to check the
dataAvailable event after doing a "write" to the serial control, I
think the whole string will be there. So far, I can't get this to
work.
Suggestions gratefully accepted.
Read the data
Add it to a buffer
Check and see if the buffer has a complete response
If not wait some more
If it does then process it


Norman Palardy

Real World 2012, THE Real Studio Event of the year!
http://realsoftware.com/community/realworld.php


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
Roger Clary
2012-01-04 03:10:58 UTC
Permalink
Post by Norman Palardy
Post by Roger Clary
I am attempting to read from a UART device connected to a USB port (Mac and Win). To communicate with the device, I write a short string (5-8 chars) and then read a string returned from the device up to 127 chars. but of varying lengths. The problem I am having is that the DataAvailable event of the serial control, running asynchronously, may not contain the complete return string when it fires. I am currently jumping through many hoops trying to check the validity of the returned string, but hoping there is a way I can delay by even half a second data appearing in the dataAvailable event so that I can be assured that the complete string is there when I parse it.
If I can figure out how to wait even 500 m/s to check the dataAvailable event after doing a "write" to the serial control, I think the whole string will be there. So far, I can't get this to work.
Suggestions gratefully accepted.
Read the data
Add it to a buffer
Check and see if the buffer has a complete response
If not wait some more
If it does then process it
Create a timer. Set the timer's period to 500 mSec. When you send the data to the serial port, start the timer. In the action event of the timer, Do a serial.readall and put that into a property or text box or whatever you want to do with it. Then set the timer to off.
The way I do it for my stuff is have a timer that runs continuously doing a readall and appending it to a text area control. I think this period is 10 mS. In order to type in the window control and interact with the device, I put some blocking code in that basically turns my read off in the timer (I leave the timer firing, just set a boolean called NoRead) as I am typing. I then capture the received data in the key down event and throw it away. Once I'm done typing, I set NoRead to False and let things continue. This works very well and I never get duplicate characters and I see everything in real time (well, w/in 10 mS). My only problem is that most serial or telnet connections contain specific control characters that are part of the VT-100 or ANSI control set and I've not figured
out how to properly interpret or deal with those yet.


Thanks to both Jon and Norman. While my successful result became a synthesis of many ideas, your thoughts helped put me on the right track. I appreciate your suggestions.


Roger Clary
Class One Software
http://www.classonesoftware.com
Educational Software for Lifelong Learning





_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Loading...