Understanding RS-485 in Linux
Asked Answered
J

1

6

I'm trying to learn the RS-485 interface by sending data between two Linux computers. The main computer is running ubuntu16.04 and the other is running a debian based system on kernel 4.19.

The main PC has a USB to RS-485 adapter and the other PC has RS-485 interface.

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. By looking around i realized that this is because both PCs are waiting for RTS that somehow needs to be toggled.

I then concluded that I need some sort of C program to initialize the interface(please correct me if I'm wrong).

I went https://www.kernel.org/doc/Documentation/serial/serial-rs485.txt Where the is an example code for setting up 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. I also read about ioctls but I'm still stuck.

If anyone has experience or tips I would be really thankful.

Best Regards, W

Juni answered 24/5, 2019 at 8:55 Comment(8)
Show ctrl485. Show how you open fd. What type is status?Zip
printed values:status:-1,fd:3, content of ctrl485:0,1,0,1.Juni
What is the value of errno?Zip
printf("error %s\n", strerror(errno));error Inappropriate ioctl for deviceJuni
Can you give us a link or the full name of your devices (both) and the drivers you are using?Shop
PC1: [cincoze.com/goods_info.php?id=24] with Debian 4.19.28-2. PC2 is a normal PC with Ubuntu 4.10.0-28 to which I'm using [elfa.se/en/converter-usb-1x-rs422-485-exsys-ex-1303/p/…Juni
I see, with your setup you should not need to do anything with RTS. Your USB cable can handle the toggling automatically. The problem must be on your Cincoze PC. According to the datasheet you can setup its port as full-duplex or half-duplex. Can you edit your post to show the output of "dmesg | grep serial"?Shop
FWIW, the sample from the kernel docs you link to is broken pseudocode, as it doesn't initialize the struct serial_rs485 rs485conf;, if it's an automatic variable, its flags field may contain garbage (do the initialization with struct .. rs485conf = {0}). Also, some ioctls may return a positive int on success -- better to check with if(status < 0) ...`Exuviate
S
4

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.

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:

BIOS settings for COM2

Then make sure your USB cable is also working half-duplex with the microswitches (it should be fine by default):

USB Cable settings

And finally, make sure you're wiring your devices correctly, following these diagrams:

Wiring 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.

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.

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:

RS485: Inappropriate ioctl for device

Pymodbus - Read input register of Energy meter over rs485 on uart of raspberry pi3

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. Use $cat /proc/tty/driver/serial to find out details about you hardware.

Shop answered 24/5, 2019 at 17:16 Comment(11)
Hello, thank you for such a thoroughly written answer! I have everything set up as you written. Since I use a Dsub female cable between the adapter and Cincoze PC the wiring should natively correct. However, I'm still not able to send data between the computers. If I use picocom from my PC with echo enabled(on adapter) I can see what I'm typing but not hit enter, not sure what that means atm but it feels like the problem is still on the Cincoze side.Juni
You're welcome. Are you sure about the internal wiring of your cable? If what you have is a standard RS232 cable male to female I don't think it will work (these cable have a cross connection from TX on one side to RX on the other). If you have one available I would check it with a DMM to make sure. I'm not even sure what would happen with a 1 to 1 DB9 (GND is on different pins).Shop
Just to make it clear: if you have changed the bios settings and microswitches on your cable (both RS485 or both RS422) and your cable is correct (2 wire for RS485 and 4 wire for RS422) you should be able to open picocom or a similar terminal utility with the same settings (baud rate, data bits, and stop bits) and send and receive from both sides. You don't have to hit enter, each character should appear as you type it, I believe. Maybe the only thing to check is if you have flow control off (that's the default too). And make sure you have the right port, you have a lot of them!Shop
Another thought: if you want to be sure your cable is not a null modem ( en.wikipedia.org/wiki/Null_modem female to female sub D9) you can easily test it setting two of your ports on the Cincoze computer as RS232 and connecting them. Then, open two picocom terminals (both on the Cincoze) and type in something. If the link works it means you have a null modem cable and you cannot use it for RS485 (or RS422).Shop
Hello, GND is pin 5 on both connectors. I found out something rather curious. If i change to RS-422 in BIOS it works as expected(and on the adapter). But when i switch back to RS-485 half-duplex nothing gets through. That should also imply that the cable is not null modem?Juni
I deleted my last comment, take a look at this: exsys.it/wp-content/uploads/ex-1303.pdf Obviously, if your cable works for RS422 it will never work for RS485.Shop
It works!!! It is jibberish, but data was sent! For some reason that I cannot explain the port mapping changes name when you switch from RS-232/422 to RS-485. Kinda hard to figure that out, so COM2 became ttyS1. I was able to echo from the Cincoze PC but not the other way around, atleast something is happening.Juni
Regarding the cable, why would that be so? The adapter is male, from the cincoze is also male connector and then i use a female to female in between them. The wiring would not affect the cable since it just send whatever comes to the other side?Juni
great! It's always harder to take the first steps, then it's just smoothing things out. With so many ports, no wonder it's difficult to keep track of them. Jibberish might mean that something is wrong with the connection (maybe the cable as we said above) or you have different settings (baud rate, data bits, stop bits). Good luck with it, and post if you have other issues.Shop
Indeed! Thank you a lot for your help, I will mark this as solved:)Juni
The cable does matter, you have to set it up correctly for the standard you're using: RS232, RS485 or RS422. If you look at the link I posted above the wiring is different for RS485 vs. RS422. And if you're using a male to female adapter, make sure it's not adding any crossings (sometimes you have null modem male to female converters!)Shop

© 2022 - 2024 — McMap. All rights reserved.