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.开发者_如何学Python..");
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)
精彩评论