微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在程序中间等待来自串口的input

我正在编写一个程序来控制与卫星通信的铱星调制解调器。 在发送每个AT命令后,调制解调器发回一个回复(取决于命令),以指示命令成功。

现在我已经实现了这个function,以便程序在每个命令传输到调制解调器之间等待10秒钟,但是这样做有点危险,因为在命令没有被成功解释的情况下它不允许error handling。 我知道如何读取串行input的唯一方法是使用while(fgets(,)) ,所以我想知道如何让程序通过串口等待来自调制解调器的回复,并检查它是什么在下一个命令发送之前,而不是有一个统一的延迟。

我正在使用一个Linux操作系统。

FILE *out = fopen(portName.c_str(),"w");//sets the serial port for(int i =0; i<(sizeof(messageArray)/sizeof(messageArray[0])); i++) { //creates a string with the AT command that writes to the module std::string line1("AT+SBDWT="); line1+=convertInt( messageArray[i].numChar); line1+=" "; line1+=convertInt(messageArray[i].packetNumber); line1+=" "; line1+=messageArray[i].data; line1+=std::string("rn"); //creates a string with the AT command that initiates the SBD session std::string line2("AT+SBDI"); line2+=std::string("rn"); fputs(line1.c_str(),out); //sends to serial port usleep(10000000); //Pauses between the addition of each packet. fputs(line2.c_str(),out); //sends to serial port usleep(10000000); }

我如何知道在Linux上使用哪个串口?

编译FTDI内核模块

Socatus伪terminal:你可以使用数据线(DTR,RTS等)?

从串口读取原始字节

串口通信C ++ Linux

Python串行模块无法configuration端口

在C中获取Windows串行端口的input缓冲区长度

在linux的串口描述符块上的'close'函数

在Linux中阻塞UART串行端口的I / O写操作

从Windows上的串行端口读取的时间

使用对串口对应的文件描述符进行选择或轮询 ,当描述符准备好读取时返回。

像这样的东西:

int fd = fileno(stdin); /* If the serial port is on stdin */ fd_set fds; FD_ZERO(&fds); FD_SET(fd,&fds); struct timeval timeout = { 10,0 }; /* 10 seconds */ int ret = select(fd+1,&fds,NULL,&timeout); /* ret == 0 means timeout,ret == 1 means descriptor is ready for reading,ret == -1 means error (check errno) */

为了保持与你的模型接近,你可以做一些事情:

1)使用纯文件句柄(通过打开())。 也可以使用read()和write()与串口通信。

2)使用上面的1然后让你使用select来查看是否有任何东西可以被写入或读取。

这也可以让你移动与调制解调器的通信到另一个线程,如果还有其他的东西你的程序必须做的…

我最近刚做了一些与HF Radio调制解调器非常类似的东西,并使用Boost ASIO库进行串口通信。

这可以帮助你,如果这是一个选项。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐