Serial-Port

如何將串口設置為 RS-485 模式?

  • October 31, 2015

我正在使用從串口到 USB 的轉換器,在 Windows 中,可以打開串口屬性並設置一個複選框 RS-485,只有在那之後我才能從設備接收數據。如何在 Linux 中做同樣的事情?因為預設情況下,我得到的結果與在未選中 RS-485 的 Windows 中相同:

Port name - /dev/ttyACM0; Method name - readBytes(); Serial port operation timeout (500 ms).
execute try 2 error: I/O exception - failed to read

我的設備是:

Bus 001 Device 008: ID 04e2:1411 Exar Corp.

setserial -a /dev/ttyACM0
/dev/ttyACM0, Line 0, UART: unknown, Port: 0x0000, IRQ: 0
       Baud_base: 115200, close_delay: 5, divisor: 0
       closing_wait: 300
       Flags: spd_normal low_latency

您將不得不編寫一些 C 程式碼,如核心文件中所述。

#include <linux/serial.h>
struct serial_rs485 rs485conf = {0};

int fd = open ("/dev/ttyACM0", O_RDWR);
if (fd < 0)...
rs485conf.flags |= SER_RS485_ENABLED;
if (ioctl (fd, TIOCSRS485, &rs485conf) < 0)...

引用自:https://unix.stackexchange.com/questions/239839