Dimitri Herr Dimitri Herr - 1 year ago 92
C Question

vxworks serial port read timeout

I am using vxworks installed on a UEIPAC 600 1-G box. I am trying to read data from a serial port. This is my code.

void read_serial(int n){
/* store number of bytes read = int n*/
int num=0;
/* hold the file descriptor */
int fd = 0;
/* dev name from iosDevShow output */
char dev_name[] = "/tyCo/0";
/* buffer to receive read */
char re[n];
/* length of string to read */
int re_len = n;
/* open the device for reading. */
fd = open( "/tyCo/0", O_RDWR, 0);
num = read( fd, re, re_len); /* read */
close( fd ); /* close */
printf("number of bytes read %d\n",num); /* display bytes read*/
printf("displaying the bytes read: %s\n",re);

when I run it it just times out until I hit keyboard input then outputs like so

number of bytes read 1
displaying the bytes read:

How do i fix this to properly read from the serial port.

jxh jxh
Answer Source

You do not check to see if you have opened the serial port successfully.

fd = open( "/tyCo/0", O_RDWR, 0); 
if (fd < 0) {
    /* handle the error */

You do not check if your read from the serial port succeeded.

num = read( fd, re, re_len); /* read */ 
if (num < 0) {
    /* handle the error */

You are assuming reading from the serial port will result in a printable string, which is probably not correct. When you print out the data read from the serial port, you should probably dump the data in hexadecimal so you can ascertain what byte values were extracted.

for (int i = 0; i < num; ++i) {
    printf(" %02x", re[i]);