是否有可能以编程方式将串行端口 Tx 线保持在 C 中的高电平或低电平?

Is it possible to programatically hold the serial port Tx line high or low in C?

这是我的 C 程序的相关部分(在 Linux 中):

while (input != ' '){
 write(serial_port, msg, sizeof(msg));
 //1. here I would like to wait at least 100 us with the Tx line high
 //2. followed by at least 8us with the Tx line low
 //3. and then repeat the message
 input = mygetch();
}

如您所见,每次通过串口发送 msg 后,我想将 Tx 线设置为高电平 100 微秒,然后设置为低电平 8 微秒,然后重复消息,直到用户按下 space-bar.

我在Linux中使用C在串口上有这种控制吗?

TIA 为您提供帮助。

是的。不发送任何数据时串行线路的默认状态是“标记”(高,对于 single-ended 线路)。要发送 space(低),请设置中断位。清除中断位以恢复空闲状态。

正如评论中所指出的,tty 设备 ioctls TIOCSBRK 和 TIOCCBRK 可用于发送和清除中断。

https://linux.die.net/man/4/tty_ioctl