I need to setup bidirectional communication between 2 devices, other one is using normal RS-232 and other one is using half-duplex RS-485.
Basically RS-232 is sending commands which are 1 character long and RS-485 is responding with messages which are normally few characters long, usually less than 50 characters.
I tried to find some ready code which does this or atleast something which could be easily modified to do this, but I didn't really find anything that could be quickly taken into use, so I decided to ask if anyone knows if there is some similar project.
I have some Nano boards and RS-232 and RS-485 half-duplex tranceivers so I could easily make PCB for this if there is some code available.
So data flow would be some thing like this.
Nano has configured uart to rs-232 and one I/O pin let's call it "tranceiver select" to '0' and listen uart until it receives command from rs-232 and stores it.
Nano will configure uart to RS-485 and set "tranceiver select" to '1' which will physically connect uart to RS-485 tranceiver and disconnect it from RS-232 tranceiver.
Nano will set DE (RS-485 directional control" to 1 and send command out of uart to RS-485.
4.Nano will set DE to '0' and listen for response and store it.
Nano will set "tranceiver select" to '0' and configure uart to rs-232 and send data to RS-232
Nano will go back to step 1. and listen new command from RS-232.
few notes:
Main purpose to configure to rs-232 and to rs-485 is to get HW control for RS-485 DE pin. other serial port parameter could probably be mathed between devices.
From rs-232 there wont be carriage return, but that is not a problem since command is always 1 character.
from RS-485 side there will be carriage return to signal end of string.
If anyone could point to some project which could be modified to do his with as little effort as possible it would be appreciated
Well the first thing you need to do is buy an RS232 to RS485 converter so you can connect both devices together using RS485
Then you can start with this library
What you are trying to do is possible with an Arduino Nano, BUT there some limitations.
I think the connecting and disconnecting is maybe not quite the way to describe it - or I'm not quite getting what you are describing.
Let's assume that you can't use the built-in hardware serial port on the Nano - maybe because you are going to use that for debugging the code etc.
The big assumption will be that the RS-232 device will pause between sending commands to either wait for the response from the other device or time out and send the command again.
If that is the case, then you could have 2 software serial ports. There are limitations such that only 1 software serial port can listen for (i.e. receive) data at once, and that the baud rate is fairly slow - less than 38400 baud.
Your code would likely do something like this:
configure software serial port #1 to listen for the 1 character command
on reception of that command you would tell software serial port #2 to listen
configure the RS485 module for transmit mode
send the command out on software serial port #2
once transmission completes, switch the RS485 module back to receive mode
wait a period of time for the RS485 device to respond on software serial port #2
send the response back on serial port #1
back to step 1
A lot depends on the baud rates involved. At higher baud rates (i.e. greater than 38400), you would be better with an Arduino MEGA2560 based board with 4 hardware serial ports.
Thank you for the reply and let me try to explain things little better.
This device would be stand alone passing data between 2 devices so there would not be need to connect example PC to HW serial port, ofcourse it would be nice have that connection during softaware development.
I was planning to use HW serial port, so what I mean with connect and disconnect was to use digital switch to connect only one tranceiver at any given time to HW serial port, like below:
receive command from RS-232 -> connect rs-232 tranceiver to HW serial port and disconnect RS-485 tranceiver from HW serial port.
send command to RS-485 -> connect rs-485 tranceiver to HW serial port and disconnect RS-232 tranceiver from HW serial port.
receive response from RS-485 -> connect rs-485 tranceiver to HW serial port and disconnect RS-232 tranceiver from HW serial port.
send response to RS-232 -> connect rs-232 tranceiver to HW serial port and disconnect RS-485 tranceiver from HW serial port.
After RS-232 device has sent command it will wait for reply or time out before sending new command so there is no need to worry about timing or syncronization
Baud rate will be quite slow probably 9600 and 38400 max, so it probably will better idea to use that SW serial port method, that way there is no need to switch between tranceivers.
You might have reasons to use a Nano but I would like to point out that the Nano Every has 4 UARTs. Two are part of the Arduino core (Serial and Serial1), the other two are a bit trickier. If you're not set on using a classic Nano but have the freedom to change to a Nano Every you can read e.g. Arduino Nano Every Serial Ports – Kevin's Blog.
You can use a CD74HCT126 or 125 or SN74HCT126 or 125 to do the switching.
You will need 3 control signals, two for the 125/126 part and one for the RS485 driver.
You can also disconnect everything using this set-up so you can download code to the Arduino.