gpt4 book ai didi

c - 自动更改 RTS 以进行 RS-485 通信

转载 作者:太空狗 更新时间:2023-10-29 11:44:35 28 4
gpt4 key购买 nike

我正在尝试在我的程序中设置半双工通信。我的 RS485 收发器使用 RTS 标志 (TIOCM_RTS) 在发送和接收之间来回切换。为了发送/接收数据,我需要手动更改 RTS 标志:

  1. 将 RTS 设置为高。

  2. 发送数据。

  3. 将 RTS 设置为低。

    int setRTS(int level) {
    int status;
    ioctl(ser_port, TIOCMGET, &status);
    if(level) {
    status |= TIOCM_RTS;
    } else {
    status &= ~TIOCM_RTS;
    }
    ioctl(ser_port, TIOCMSET, &status);
    return 1;
    }

我的问题是:linux内核不应该能够自动切换RTS吗?以及如何确保在调用 setRTS(0) 之前发送数据?

最佳答案

shouldn't the linux kernel be able to switch RTS automatically?

是的,从 Linux 3.0 开始就有内核框架。
include/uapi/asm-generic/ioctls.h中有两个ioctl:

#define TIOCGRS485      0x542E
#define TIOCSRS485 0x542F

在 RS-485 模式下检索和配置 tty 串行端口驱动程序。
这些 ioctl 使用 struct serial_rs485:

 /*
* Serial interface for controlling RS485 settings on chips with suitable
* support. Set with TIOCSRS485 and get with TIOCGRS485 if supported by your
* platform. The set function returns the new state, with any unsupported bits
* reverted appropriately.
*/

struct serial_rs485 {
__u32 flags; /* RS485 feature flags */
#define SER_RS485_ENABLED (1 << 0) /* If enabled */
#define SER_RS485_RTS_ON_SEND (1 << 1) /* Logical level for
RTS pin when
sending */
#define SER_RS485_RTS_AFTER_SEND (1 << 2) /* Logical level for
RTS pin after sent*/
#define SER_RS485_RX_DURING_TX (1 << 4)
__u32 delay_rts_before_send; /* Delay before send (milliseconds) */
__u32 delay_rts_after_send; /* Delay after send (milliseconds) */
__u32 padding[5]; /* Memory is cheap, new structs
are a royal PITA .. */
};

我已经在 Atmel 和 Etrax SoC 上使用了这种 RS-485 功能,但在其他方面,这些 ioctl 在 Linux UART/USART 驱动程序中的实现非常稀少。
如果您的驱动程序没有,请考虑自己实现。您可以使用 drivers/tty/serial/atmel_serial.c 中的实现作为指南。另请阅读 Linux kernel document for RS485 .

关于c - 自动更改 RTS 以进行 RS-485 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25250731/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com