Thanks for the feedback Roverfan and cburgess. My apologies for not replying earlier; I wanted to try out your suggestions before asking more questions. cburgess, I gave select() a try but couldn’t seem to get the time taken to examine the serial port below about 2 milliseconds (I need to get it below 1ms). When I set the wait time to 0 it seems to completely skip reading the serial port.
RoverFan, I tried your suggestion and have been partially successful. I am encountering two problems. The first is that if I set the readTimeout value to 0 then the reading of the serial port returns a value of -1 for the number of bytes read (so no bytes were read). This is not the case when I increase the value of readTimeout to 1 or more (and have a device connected that is sending in data on the serial port). Next, it seems that if serial data is available on the port and I try to read it using the following code my measured value for elapsed time becomes erratic (gets negative, etc.). It’s very strange. Any ideas as to how I could go about fixing both of these problems?
thanks!!
James
Example calls and what the program responds with:
- No hardware connected to the serial port, readTimeout = 0;
./serial
Serial Port 1 opened.
Time to initialise the serial port: 0.001279 sec
Elapsed Time: 0.000043 sec; Bytes read: -1
- No hardware connected to the serial port, readTimeout = 1;
./serial
Serial Port 1 opened.
Time to initialise the serial port: 0.001218 sec
Elapsed Time: 0.000351 sec; Bytes read: -1
- Hardware connected to the serial port, readTimeout = 0;
./serial
Serial Port 1 opened.
Time to initialise the serial port: 0.001252 sec
Elapsed Time: 0.000035 sec; Bytes read: -1
- Hardware connected to the serial port, readTimeout = 1;
(Note: the ACTUAL elapsed time isn’t actually 123 seconds)
./serial
Serial Port 1 opened.
Time to initialise the serial port: 0.001334 sec
Elapsed Time: 123.386620 sec; Bytes read: 4
--------- source code ----------
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/select.h> /* for the select() function /
#include <time.h>
#include <sys/syspage.h> / qnx.com/developers/docs/qnx_ … entry.html $
* for call $
* cycles_per_second = SYSPAGE_ENTRY(qtime)->cycles_per_sec;
/
#include <inttypes.h> / for uint64_t /
#include <sys/neutrino.h> / For ClockCycles() /
#include <errno.h> / For Error Codes. */
int main( void )
{
int console, serial;
struct timeval tv;
fd_set rfd;
int n;
double initial_time=0.0;
double elapsed_time=0.0;
uint64_t cycles_per_second;
unsigned char buf[1];
unsigned char *p=buf, *q=buf;
uint64_t readTimeout = 0;
int bytesRead = 0;
p=buf;
/* - - - - - - - - - - - - - - - - - - -
* Initialise time-related stuff.
* Then record the current time.
* - - - - - - - - - - - - - - - - - - - */
/* see QNX Syspage_entry:
* qnx.com/developer/docs/qnx_6 … entry.html for details */
cycles_per_second = SYSPAGE_ENTRY(qtime)->cycles_per_sec;
/* Finding the time that has elapsed and record it as initial time */
initial_time = ((double)ClockCycles())/((double)cycles_per_second);
/* - - - - - - - - - - - - - - - - - - -
* Open the serial port and console
* - - - - - - - - - - - - - - - - - - - */
if(( serial = open( “/dev/ser1”, O_RDONLY| O_NOCTTY | O_NONBLOCK ) ) == -1)
{
perror( “open” );
return EXIT_FAILURE;
}
else
{
printf(“Serial Port 1 opened.\n”);
}
/*
* Clear the set of read file descriptors, and
* add the two we just got from the open calls.
* */
FD_ZERO( &rfd );
FD_SET( serial, &rfd );
elapsed_time =((double)ClockCycles())/((double)cycles_per_second)-initial_time;
printf(“Time to initialise the serial port: %f sec\n”, (float)elapsed_time);
initial_time = ((double)ClockCycles())/((double)cycles_per_second);
readTimeout = 1; /* */
TimerTimeout( CLOCK_MONOTONIC,
_NTO_TIMEOUT_REPLY,
NULL,
&readTimeout,
NULL);
bytesRead=read( serial, p, sizeof(p));
/* Tell the user how much time has elapsed and how many bytes were read */
elapsed_time =((double)ClockCycles())/((double)cycles_per_second)-initial_time;
printf(“Elapsed Time: %f sec; Bytes read: %d \n”, (float)elapsed_time, bytesRead);
close(serial); /* close the port. */
return EXIT_SUCCESS;
}