1

I am writing a vxworks task involving sending data thru serial port. Opening the serial port is successful. But when I am trying to set the baud rate of the port using ioctl() system call, it fails. I am giving the code below. can anyone please shed some light on it? The second function is failing always...

int f, status;

if (f = open("/tyCo/1", O_RDWR, 0) == ERROR)
{
    printf("error opening serial port; exiting...");
    return 1;
}

if (status = ioctl(f, FIOBAUDRATE, 2400) == ERROR)
{
    printf("ioctl error; exiting...");
    return 1;
}
Mike
  • 47,263
  • 29
  • 113
  • 177
Likhin M
  • 21
  • 2
  • 4
  • 2
    Just to be clear. Are you saying that you open the port and then set the baud rate? If that is correct try setting the baud rate and then open the port. – dbasnett Jun 29 '11 at 12:59
  • @dbasbett -- ioctl() needs the file descriptor returned from the open() call -- how do you propose to set the baud rate first? – Jay Elston Jun 29 '11 at 18:56
  • m -- Check the documentation on the device you are using to see what the valid baud rates are. Also check any configuration settings on the device carefully. – Jay Elston Jun 29 '11 at 18:58

1 Answers1

6

Maybe is a little bit too late, but the code above looks like erroneous. Assignment operator has lower priority as comparing operator, so you should write the code like:

if((f = open("/tyCo/1", O_RDWR, 0)) == ERROR)
{
    printf("error opening serial port; exiting...");
    return 1;
}

if((status = ioctl(f, FIOBAUDRATE, 2400)) == ERROR)
{
    printf("ioctl error; exiting...");
    return 1;
}

This way it works perfect in VxWorks. The way you wrote the code was to assign f either 0 or 1 (0 in this case, because you could open the serial port), and then tried to set baud rate for file descriptor 0 (i guess is the stdout id). The same you assigned status either 0 or 1 (1 in this case, because you couldn't set the baud rate, so ioctl returned -1)

Mircea
  • 61
  • 1
  • 2