linux下恢复串口
Restoring serial port in linux
我试图通过串口与PLC进行通信,PLC控制着工业中的机械门。由于对这个主题没有很丰富的经验,而且很匆忙,我没有意识到存储旧设置并在程序退出时恢复它们的重要性。在更改术语结构中的一些字段后,即使在使用与我用于其他端口(ttyD0)的端口函数完全相同的端口打开后,我也不再能够从端口读取任何内容,该端口适用于这些设置。有什么建议我可以恢复ttyD1回到工作状态吗?
用于打开端口的代码如下:
int OpenPort()
{
fd = open("/dev/ttyD0", O_RDWR | O_NOCTTY);
if (fd < 0)
{
cerr << "open error " << errno << strerror(errno) << endl;
}
else
{
struct termios my_termios;
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &my_termios);
//bzero(&my_termios, sizeof(my_termios));
tcflush(fd, TCIFLUSH);
my_termios.c_cflag = B115200 | CS8 | CREAD | CLOCAL | HUPCL;
//my_termios.c_lflag = ICANON;
//cfsetospeed(&my_termios, B115200);
tcsetattr(fd, TCSANOW, &my_termios);
}
return fd;
}
只需复制您从第一个tcgetattr
收到的结构体,并在退出时将其交给tcsetattr
。
相关文章:
- 为什么我要从串口读取垃圾?
- Boost Asio SerialPort。无法创建并打开串口服务
- 如何在QtWidget中使用c++串口发送数据
- 如何插入十六进制值并发送到串口?
- 串口打开返回 EAGAIN
- QT 5.10.1 串口不发送,为什么?
- 通过串口发送后无法获取原始值
- C++ / RS232C 串口 / 事件回调异步读取缓冲区 / 不使用线程
- 串口读取功能的通信超时和线程不超时
- QT 5.7 串口读取速度很慢
- 串口通信
- 为什么Qt(4.8)串口(qextserialport)分两部分从设备读取数据
- 将C++串口奇偶校验更改代码从 Linux 移植到 Windows
- qt串口未打开
- 有没有一个跨平台的c++串口库可以枚举端口
- Qt串口,当我写的时候,我看不懂我写的东西
- 是否可以检查串口是空闲还是占用
- 无法将简单的读/写串口代码从 boost::asio::serial_port 迁移到 QSerialPort
- 通过c++中的串口发送数据
- linux下恢复串口