简体   繁体   English

在 vxworks 上设置串口波特率失败

[英]Setting baud rate of serial port fails on vxworks

I am writing a vxworks task involving sending data thru serial port.我正在编写一个涉及通过串行端口发送数据的 vxworks 任务。 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.但是当我尝试使用 ioctl() 系统调用设置端口的波特率时,它会失败。 I am giving the code below.我在下面给出代码。 can anyone please shed some light on it?谁能解释一下? The second function is failing always...第二个 function 总是失败......

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.这样它就可以在 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).您编写代码的方式是将 f 分配为 0 或 1(在这种情况下为 0,因为您可以打开串行端口),然后尝试为文件描述符 0 设置波特率(我猜是标准输出 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)与您分配状态相同的 0 或 1(在这种情况下为 1,因为您无法设置波特率,所以 ioctl 返回 -1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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