简体   繁体   中英

Setting baud rate of serial port fails on vxworks

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;
}

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM