简体   繁体   English

了解Linux中的RS-485

[英]Understanding RS-485 in Linux

I'm trying to learn the RS-485 interface by sending data between two Linux computers. 我正在尝试通过在两台Linux计算机之间发送数据来学习RS-485接口。 The main computer is running ubuntu16.04 and the other is running a debian based system on kernel 4.19. 主计算机正在运行ubuntu16.04,另一台计算机正在内核4.19上运行基于debian的系统。

The main PC has a USB to RS-485 adapter and the other PC has RS-485 interface. 主PC具有USB转RS-485适配器,另一PC具有RS-485接口。

Initially I assumed that i could simply echo data on the interface but I could not. 最初,我以为我可以直接在接口上回显数据,但我做不到。 If i connected with picocom I could type data but not send it. 如果我连接了picocom,则可以输入数据但不能发送。 By looking around i realized that this is because both PCs are waiting for RTS that somehow needs to be toggled. 通过环顾四周,我意识到这是因为两台PC都在等待RTS,因此需要进行某种切换。

I then concluded that I need some sort of C program to initialize the interface(please correct me if I'm wrong). 然后我得出结论,我需要某种C程序来初始化接口(如果我错了,请更正我)。

I went https://www.kernel.org/doc/Documentation/serial/serial-rs485.txt Where the is an example code for setting up RS-485. 我去了https://www.kernel.org/doc/Documentation/serial/serial-rs485.txt,其中是用于设置RS-485的示例代码。

However, when I try to run it always fails at: 但是,当我尝试运行时,它总是在以下位置失败:

status = ioctl(fd,TIOCSRS485, &ctrl485);
  if (status) {
    printf("ERROR PORT 1! TIOCSERSETRS485 failed %i", status);
    return -1;
  }

I have googled every combination of RS-485 that i can think of but most information is about modbus and sensors. 我用谷歌搜索了我能想到的RS-485的每种组合,但是大多数信息都是关于Modbus和传感器的。 I also read about ioctls but I'm still stuck. 我也读过有关ioctl的文章,但我仍然陷于困境。

If anyone has experience or tips I would be really thankful. 如果有人有经验或技巧,我将非常感激。

Best Regards, W 最好的问候,W

I cannot be completely sure but I don't think you need to play with the RTS toggling at all since your devices seem to support hardware half-duplex by default. 我不能完全确定,但我认为您根本不需要使用RTS切换,因为默认情况下,您的设备似乎支持硬件半双工。

Check the following to make sure your hardware setup is correct: 检查以下内容以确保您的硬件设置正确:

According to the manual of your Cincoze you have to select each port's operating mode at BIOS level: 根据您的Cincoze手册,您必须在BIOS级别上选择每个端口的操作模式:

COM2的BIOS设置

Then make sure your USB cable is also working half-duplex with the microswitches (it should be fine by default): 然后确保您的USB电缆也与微动开关半双工(默认情况下应该没问题):

USB电缆设置

And finally, make sure you're wiring your devices correctly, following these diagrams: 最后,请确保按照以下图表正确连接设备:

接线图

If your cable is not too long (less than 50 meters, maybe?) you should not have to worry about termination or impedance that much, otherwise you might need to add a resistance on the computer DB9 and move the switches on the USB cable. 如果电缆不太长(也许不到50米?),则不必担心端接或阻抗太大,否则,您可能需要在计算机DB9上加一个电阻并移动USB电缆上的开关。

As a final comment, be aware that your devices support full-duplex too; 最后,请注意您的设备也支持全双工。 if you change the settings as per the instructions above but select RS422 instead and make the 4-wire setup you should be able to open a terminal on both computers and send and receive at the same time, in case you need that. 如果您按照上述说明更改设置,但选择RS422并进行4线设置,则您应该能够在两台计算机上打开一个终端,并在需要时同时发送和接收。

EDIT 1: For completeness, I'm adding here a couple of references for those who are working with devices that don't support hardware direction control: 编辑1:为完整起见 ,我在这里添加了一些参考,供正在使用不支持硬件方向控制的设备的用户参考:

RS485: Inappropriate ioctl for device RS485:设备的不适当的ioctl

Pymodbus - Read input register of Energy meter over rs485 on uart of raspberry pi3 Pymodbus-在Raspberry Pi3的uart上通过rs485读取电能表的输入寄存器

EDIT 2: As discussed in the comments below, for this particular hardware where you can choose different operating modes in the BIOS, an additional thing to keep in mind is that the naming of the ports (at OS level) might change when you select a new operating mode. 编辑2:如以下注释中所讨论,对于可以在BIOS中选择不同操作模式的特定硬件,要记住的另一件事是,当您选择一个端口时,端口的命名(在OS级别)可能会改变。新的操作模式。 Use $cat /proc/tty/driver/serial to find out details about you hardware. 使用$cat /proc/tty/driver/serial查找有关硬件的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM