Re: Reading Serial Port
"clinisbut" <clinisbut@gmail.com> wrote in message
news:5edcd8e2-2a6c-4276-aade-844e41317c6f@s12g2000prg.googlegroups.com...
If you are wondering why I'm reading justa a byte each time when all
frames are 16 bytes length it's because this is just a test, in the
final version the device will send variable frame lengths.
I do remember having to set the timeouts appropriately or would get bad
data. Before writing data, I set a 0 timeout, and set a 1 second timeout
for readind data. Here's an example code snippet using the Naughter
library:
try
{
// Ask device to write back its current time
m_port.Set0Timeout(); // don't ever block, just return any chars already
arrived at port when port.Read() is called
BYTE cmd = 0x27; // READ_TIME
if ( m_port.Write (&cmd, 1) == 1 )
{
// Read data that device sent back. Wait up to 1 second for the data to
be received before timing out
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 1000; // 1000 ms
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
m_port.SetTimeouts(timeouts);
// Reads 4 bytes
BYTE bytesTime[4];
ZeroMemory(bytesTime, sizeof(bytesTime));
if ( m_port.Read (bytesTime, _countof(bytesTime)) ==
_countof(bytesTime) )
{
...
}
}
}
catch (CSerialException *)
{
bShowError = TRUE;
}
-- David