Skip to content

Instantly share code, notes, and snippets.

@Rolias
Last active December 15, 2015 14:29
Show Gist options
  • Save Rolias/5274516 to your computer and use it in GitHub Desktop.
Save Rolias/5274516 to your computer and use it in GitHub Desktop.
Simple sample of using ReadWithTimeout on the NetBurner to read X bytes without any additional delays.
void Read(int port, int messageLength, int timeoutTicks)
{
const int MAX_DATA = 1024;
if (messageLength > MAX_DATA)
{
cout << "This demo only supports lengths up to" << MAX_DATA<<'\n';
return;
}
char read_buffer[MAX_DATA];
int read_status = 0;
int bytes_read = read_status;
while (bytes_read < messageLength)
{
read_status = ReadWithTimeout(port, &read_buffer[bytes_read], MAX_DATA - bytes_read, timeoutTicks);
if (read_status <= 0)
{
if (read_status < 0)
{
cout << "\n!!Error trying read from serial port: " << read_status << "!!!\n";
return;
}
cout << "Incomplete. No more data read from serial port after waiting " << timeoutTicks << " ticks\n";
return;
}
bytes_read += read_status;
}
//do something with read_buffer - no guarantee it ends with \0 if it filled the entire buffer
if (bytes_read < MAX_DATA)
{
read_buffer[bytes_read + 1] = '\0';
cout << read_buffer << '\n';
cout <<"Successfully read " <<bytes_read << '\n';
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment