gpt4 book ai didi

使用 Beaglebone Black Angstrom 通过半双工 RS-485 分线板实现自动 RTS 的 Python PySerial

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:34 25 4
gpt4 key购买 nike

我正在尝试使用运行 Angstrom(3.8 内核)的 Beaglebone Black 与位于 9600-N-8-1 的半双工 RS-485 网络上的设备进行通信。

我正在尝试使用与此类似的 RS-485 分线板:https://www.sparkfun.com/products/10124 ,除了芯片是MAX3485http://www.maximintegrated.com/datasheet/index.mvp/id/1079 .我购买了预先组装有引脚和端子条的电路板。我的一个 friend 用示波器对其进行了测试,并声称 RS-485 板确实可以工作。该板有五个连接到 BBB 的引脚。 3-5V(电源)、RX-I、TX-O、RTS 和 GND。

我在 BBB 上禁用了 HDMI 支持,以便 UART4_RTSnUART4_CTSn引脚将可用。

    mkdir /mnt/boot
mount /dev/mmcblk0p1 /mnt/boot
nano /mnt/boot/uEnv.txt
#change contents of uEnv.txt to the following:
optargs=quiet capemgr.disable_partno=BB-BONELT-HDMI,BB-BONELT-HDMIN

然后我找到了一个覆盖来启用带有 RTS/CTS 控制的 UART-4:
            /*
* Modified version of /lib/firmware/BB-UART4-00A0.dtbo to add RTS so we can reset Arduinos
*/
/dts-v1/;
/plugin/;

/ {
compatible = "ti,beaglebone", "ti,beaglebone-black";
part-number = "BB-UART4-RTS";
version = "00A0";
exclusive-use = "P9.13", "P9.11", "P9.15", "P8.33", "P8.35", "uart4";

fragment@0 {
target = <0xdeadbeef>;

__overlay__ {


pinmux_bb_uart4_pins {
pinctrl-single,pins = <
0x070 0x26 /* P9_11 = UART4_RXD = GPIO0_30, MODE6 */
0x074 0x06 /* P9_13 = UART4_TXD = GPIO0_31, MODE6 */
/* need to enable both RTS and CTS, if we only turn on RTS then driver gets confused */
0x0D0 0x26 /* P8_35 = UART4_CTSN = lcd_data12, MODE6 */
0x0D4 0x06 /* P8_33 = UART4_RTSN = lcd_data13, MODE6 */
/* 0x040 0x0F /* P9_15 = GPIO1_16 = GPIO48, MODE7 failed attempt to put DTR on gpio */
>;
linux,phandle = <0x1>;
phandle = <0x1>;
};
};
};

fragment@1 {
target = <0xdeadbeef>;

__overlay__ {
status = "okay";
pinctrl-names = "default";
pinctrl-0 = <0x1>;
};
};

__symbols__ {
bb_uart4_pins = "/fragment@0/__overlay__/pinmux_bb_uart4_pins";
};

__fixups__ {
am33xx_pinmux = "/fragment@0:target:0";
uart5 = "/fragment@1:target:0"; /* Not a mistake: UART4 is named uart5 */
};

__local_fixups__ {
fixup = "/fragment@1/__overlay__:pinctrl-0:0";
};
};

编译并启用覆盖:
    cd /lib/firmware
dtc -O dtb -o BB-UART4-RTS-00A0.dtbo -b 0 -@ BB-UART4-RTS-00A0.dts
echo BB-UART4-RTS:00A0 > /sys/devices/bone_capemgr.*/slots

像这样将485板连接到BB
    3-5V to P9_05 (VDD_5V)
RX-I to P9_13 (UART4_TXD)
TX-O to P9_11 (UART4_RXD)
RTS to P8_33 (UART4_RTSn)
GND to P9_01 (DGND)

在 python 中,我试图使用这样的串口:
    import serial
ser = serial.Serial('/dev/ttyO4', baudrate=9600, rtscts=True)
ser.write(list_of_byte_dat)

我知道该程序有效,因为当我在 /dev/ttyUSB0 上使用 USB 到 RS-485 转换器时并设置 rtscts=False双向通信都很好。但是我无法使用 RS-485 板使通信正常工作。

我的 RS-485 板有两个问题,都与 RTS 相关。
  • 板上的 RTS 与我期望的方式相反。当我在 rs485 板的 RTS 引脚上施加电压时,板上的 RTS 灯熄灭,板子不会传输。当我从 RTS 引脚上移除电压时,RTS LED 亮起,并且电路板将传输。如何反转 BBB 上 UART_RTSn 引脚的极性?
    Temporary solution: I've made a small bone script program that uses UART4_RTSn pin as input. It turns on a different GPIO when the UART4_RTSn pin is off and turns off that same GPIO pin when the UART4_RTSn pin is on. Then hooked up the RTS pin on the rs485 board to the GPIO pin instead of the UART4_RTSn pin.
    这似乎是一个糟糕的解决方案,但它确实使 RS485 板上的 RTS 在回显到 /dev/ttyO4 时在正确的时间开启。从命令行。

    如何更改 UART4_RTSn 的极性通过调整硬件配置或通过更改pyserial中的配置来固定?

    这让我想到了第二个问题
  • 正如我在问题 1 中所说的 UART4_RTSn当像这样向 tty 端口回显值时,pin 将自动(但向后)为我工作:
    echo -en '\x02\xFD\xCD......' > /dev/ttyO4

    这将使 UART4_RTSn正在传输数据时 LED 闪烁。如果我在没有上面提到的骨骼脚本的情况下设置它,那么它会正常打开并在传输时闪烁。如果我使用我的 bonescript hack 那么它会正常关闭并在传输时闪烁(这是我想要的)。但是,这仅在从命令行使用 echo 时有效。当我使用 python 并设置串口时 UART4_RTSn引脚变为非事件状态。传输时不会闪烁。一旦我在 python 中声明:
    ser = serial.Serial('/dev/ttyO4', baudrate=9600, rtscts=True)
    UART4_RTSn销关闭并保持关闭。使用ser.write(stuff)发送信息时不闪烁.因此,rs485 板无法进行传输。我如何获得 UART4_RTSn pin在pyserial中自动工作?我试过设置 rtscts=False它没有用。

    我可以使用 ser.setRTS(True)ser.setRTS(False)手动切换 pin 值,以便我知道我使用的是正确的 pin 并且它正在被识别。但我不想直接切换 UART4_RTSn 引脚。我希望它在串行端口传输数据时自动工作,并且在使用 echo 时自动工作,但在 Python 中不工作。

  • 任何帮助将不胜感激。

    最佳答案

    RTS 通常是低电平有效信号,我怀疑您看到数据通过 echo 传输的原因是它不使用 RTS/CTS(保持高位),因此只能传输数据。

    根据 http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=29408 上的帖子

    If you enable hardware flow control (CRTSCTS in "man termios", or "stty crtscts -F /dev/ttyAMA0", or pySerial rtscts=True), then sending will take place only when CTS is asserted. RTS will be asserted except when the kernel input buffer is full. The kernel input buffer is about one page or 4KB, so your application has to get well behind with its reads before RTS actually changes.



    因此,请检查 CTS 是否在您的电路板外被断言(拉到地)。但是,我认为这不会让您正确控制所需的 RTS。

    因此,对于您的应用程序,您应该禁用硬件流控制( rtscts=False )并使用 setRTS(1) 手动控制 RTS之前写和 setRTS(0)然后。

    如果您仍然没有看到数据通过设备,请尝试交换 A 和 B 线 - A/B 标签在 RS485 设备上(令人沮丧地)不一致。如果可能,最好在您自己的应用中使用 D+/D- 标签。

    关于使用 Beaglebone Black Angstrom 通过半双工 RS-485 分线板实现自动 RTS 的 Python PySerial,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21148655/

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