Click to See Complete Forum and Search --> : serial port and newline char


abinash_y
April 8th, 2003, 12:16 PM
is there a way to break data received on serial port in chunks of each time new line char is received?

my program is receiving enormous amount of data on serial port and i want to break incoming data at newline then process it and then display it on view window.

i am using VC++ and win32 API fns to read from and write to serial port on win xp platform.

thanks
Abi:(

alex_gusev
April 8th, 2003, 03:59 PM
you may use SetDCB function with appropriate XonChar/XoffChar defined and fInX set to true

abinash_y
April 14th, 2003, 03:36 PM
I did like this,

m_dcb.fInx = TRUE;
m_dcb.XonChar = '\n';

then i tried m_dcb.XonChar ='\r\n';

but it didn't seem to make any difference.

i don't know what to do with XoffChar, as my only concern is to read data from port line by line, instead of chunk.

:confused:
-Abi

NigelQ
April 14th, 2003, 09:13 PM
You can use break characters during transmission of data using the EscapeCommFunction with the SETBREAK value set.

Alternatively, as Alex suggested, you can enable XON/XOFF protocol at both transmitting and receiving ends of the data flow. This allows the receiver to send the transmitter an XOFF character to indicate it's buffer is almost full and the transmitter should stop sending until it receives an XON character

Although XON and XOFF can be changed, these characters are normally the default characters (something like 0x11 and 0x13 from memory). You really shouldn't use characters that may appear in the normal data flow.

One reason for this is that once the transmitter receives an XOFF character triggering the stoppage, it does not guarantee that transmission will stop immediately - several more characters may be sent depending on how the protocol was implemented.

Using this method, or the RTS/CTS (request to send/clear to send) control lines are by far the best methods of controlling data flow (with RTS/CTS being preferred - no software overhead).

Attempting to read each character as it arrives and respond accordingly is doomed to failure. Because both transmission and reception are buffered, there is no way in the world you can receive a character and then react fast enough to send the transmitter a 'stop immediately' signal.

Because of the way the data is transmitted, by the time you receive a single byte of data, the following byte has already left the other end and is on it's way.

This also means that reading one byte at a time is extremely time consuming, error prone, and inefficient.

I would try to deal with chunks of data at a time if you can with some optional handshaking as described above. You can change the size of the receive buffer if necessary.

Hope this helps,

- Nigel