Need Tutorial on how to communicate with MOD-RS485

Started by mikenycz, March 20, 2014, 12:48:36 AM

Previous topic - Next topic

mikenycz

I need to send some data via RS485. I have purchased the MOD-RS485 extender board. Can someone either provide or point me to a tutorial on what I need to do to send and receive data via the RS485? For right now I am looking for something simple to send a byte of data at a time. If any can provide an explanation or point to an example it would be greatly appreciated.

Thank you,
Mike Nycz

dave-at-axon

It's the same as doing it over RS232 but with 1 caveat! If you are using 2 wire RS485 then the sending and receiving is half duplex. RS232 is what is called full duplex, mean it can send and receive at the same time. With half duplex, you have to send and then switch to receive etc.

With RS485 drivers, unless they are auto change (and the Olimex one is not), you need to use a GPIO pin to control the TRANSMIT and RECEIVE on the RS485 driver IC. With Olimex board there are 2 GPIO lines and they are connected to the SDA and SCL I2C lines by default so you need to make sure that I2C is not enabled on your board. You can link to use #SS and SCK of the mostly unused SPI.

To send data, set both GPIO lines HIGH. By the way, setting both high ensures that the receiver is disabled so that you don't see your own transmission although in some cases this can be handy to detect bus collisions but that's just complicating matters for you just now so set both HIGH.

Transmit your data and (this is the important bit) check that the data has been transmitted fully. This is important because in normal use, your data you send goes into a buffer and is transmitted by an interrupt handler. You need to know when the last byte has been transmitted fully. Switching the GPIO lines back to receive before your data is fully transmitted will mean that the receiving end will be missing the data.

Once you confirm it has been transmitted, set the GPIO lines to LOW. This will disable the transmitter and enable the receiver. You are now ready to receive your data and this works exactly as it would with RS232.

When you are ready to send again, repeat the above.

I am not sure which OS you are going use but Linux has issues with this transmission detection depending on the processor used. Android has no support for serial as such and requires a JNI to do this and again, there is no direct support for checking the transmission is complete.

mikenycz

Hi Dave,
    Thank you for your response. I understand the procedure that you provided. The problem is, I need more fundamental help. The real question is, how do I set up the GPIO pins (as you described) and what tools can I use to send data? I am working in the Debian Linux environment. Can I use the I2C tools (I2c_put and I2c_get) to send data to the I2C buss or is more setup required to make this work? I have never done this before. If you can provide more details or a pointer to where I can read up on it that would be great.

Thank you,
Mike Nycz

dave-at-axon

No, you don't use any I2C calls. You need to configure the lines as GPIO and then set them high or low.

Sorry, I have no experience of Linux for development but I believe the GPIO works like Android JNI where you open a file to the dev/value and then write 0 or 1 to it. There should be a tutorial for GPIO out there somewhere.


mikenycz

Dave,
     I have looked at the schematic for the MOD-RS485 and I see what you mean. In many of the webpages it is implied that the MOD-RS485 is an I2C device but in reality it looks like the I2C GPIO's are merely used to enable/disable the drivers in the RS485 level translator chip (ADM3483ARZ). Then you send data via the uart TX and RX pins that are also part of the UEXT. I don't know why there is any mention about it being an I2C device. Thank you for the pointers.

Mike Nycz

dave-at-axon

As is the case with a lot of hardware they sell, the documentation is a little on the poor side. It's left to experimenters and engineers to figure things out themselves. Great for the learning but a hassle for implementation on a short time :)

vinifr

#6
It is not necessary to use GPIO, "This module can be used to convert RS232 signals into RS485 signals". Use a RS232 software to send and receive data. https://dl.dropboxusercontent.com/u/22273442/example-uart.c

I'm assuming that the pins RS232 and RS485 are connected!

dave-at-axon

Yes it is necessary to use GPIO as you need to control the RS485 driver between transmit and receive as the IC is a 2-wire driver using half duplex operation.

Only when you have 4 wire and two end to end devices do you not need GPIO control.

With 2-wire RS484 you need GPIO control to enable and disable the transmitter unless the RS485 driver IC has auto transmit enable and the MOD-RS485 does not.

mikenycz

#8
I can attest to this. I tried running the program that vinifir provided and this is what was output....

root@a13-OLinuXino:~/nycz# ./serial_port3
hello world
hi again
wrote      // Up to here the program was sending data from the UART which it seemed to do without errors.
number of bytes read is -1 // This is the result of the read section...
read error:: Resource temporarily unavailable
163  135  0  0  128  43  251  182  113  135
should of put something out

Is all I need to do to put the UART in a receive mode is change the /RE and DE from '1's to '0's? Or are there port settings as well that need to change? I tried to change the /RE and DE values yesterday and I could see the sent data at the RX output of the RS485 chip on the MOD-RS485 board. However, I was unable to read the data sent in Linux. All I did was...

cat /dev/ttyS0

No data was seen. Any ideas what may be wrong or incorrectly set up?

Thanks,
Mike

One additional piece of information... I changed the permissions on the /dev/ttyS0 file... The permissions were set at crw-------   I changed the permissions to... crw-rw-rw-  There was no change to what is happening.

vinifr

Quote from: dave-at-axon on April 02, 2014, 03:57:56 PM
Yes it is necessary to use GPIO as you need to control the RS485 driver between transmit and receive as the IC is a 2-wire driver using half duplex operation.

Only when you have 4 wire and two end to end devices do you not need GPIO control.

With 2-wire RS484 you need GPIO control to enable and disable the transmitter unless the RS485 driver IC has auto transmit enable and the MOD-RS485 does not.
Yes, you have right. :P Only data transmission itself uses exclusively RX and TX pins. Sorry.