I want to control an Arduino down a long cable in a noisy RF enviroment, so I figured RS485 is best (balanced and long cables).
So I'll used an RS232 to 485 converter on the PC, then as far as I can tell, I connect my cable to a 120? terminating resistor and MAX485 chip, then connect the TTL side of this to my arduino serial pins. I'll then use MS hyperterminal (until I write some better software) to send it a multi-character string. The arduino then acknowledges the command, works out the command and performs it, then replies to say it's complete.
Will that work? Do the hyperterminal serial messages via RS232, then converted to RS485 signal make sense to the Arduino or does it have other characters/handshaking stuff that'll confuse my Arduino sketch, or does it "just work!"?
If I do need data interpretation are there any C/Arduino libraries etc I can use (been randomly looking at modbus libraries and anything that mentions 485 in the description, without it making much sense)? Still a bit confused about the interfacing from a data point of view! In at the deep end with this project at the mo'!
You can use a terminal soft to test your comms. It´s not a problem.
The RS485 only defines a physical layer (voltages), because of that, you need to create a protocol (rules, messages, etc). The code which you can follow in my blog is an example based on a professional product. It´s just an example, but you can create your own. I mean, if you send an "a", you are going to receive an "a" in your Arduino. You don´t need to do anything special... But for sure, create a protocol is the best thing to do.
Another example of protocol is modbus over rs485.(http://www.modbus.org/docs/Modbus_over_serial_line_V1.pdf)
If you use CAN bus in stead of RS485, the controller is going to do all the "dirty" job (messages, rules, CRC, ...) for you. You can follow some examples here (http://secuduino.blogspot.com/). I´m afraid, at the moment it´s only in spanish.
The hardware you've drawn should work. As Igor says you really should design a protocol to make the comms reliable. The complexity of said protocol depends on the nature of the application and mostly boils down to how critical the Arduino's job is.
The example usually put forward here is something like
where the <> are delimiters that allow the Arduino to sync on the data. That's about all you need if the Arduino is just doing a LED display for fun. If it's controlling a heart lung machine I'd add a checksum
//Mini protocolo serie
//Igor Real
int luz1= 13;
int el1;
int estado=0;
byte dato_recibido;
void setup() {
pinMode(luz1, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(12,LOW);
Serial.begin(9600);
}
void loop()
{
//trama a recibir --> S1F o S0F
//S inicio
//1 ENCENDIDO Y 0 APAGADO
//F fin
while (Serial.available() > 0)
{
//Significa que has recibido algo y lees el primer byte
dato_recibido=Serial.read();
if (dato_recibido=='S' && estado==0){
estado=1;
}
if (((dato_recibido=='1')||(dato_recibido=='0')) && estado==1){
estado=2;
if (dato_recibido=='1') el1=1;
if (dato_recibido=='0') el1=0;
}
if (dato_recibido=='F' && estado==2){
estado=0;
digitalWrite(luz1,el1);
if (el1==0) Serial.println("APAGADO");
if (el1==1) Serial.println("ENCENDIDO");
}
}
}
You can continue adding more features to your messages, for example, the slave address, timeouts if you don´t receive the next char of the message, checksum,....
As I said before, if you use CAN bus, you are going to have a very sophisticated messages and rules that it´s "transparent" for the user (the CAN controller do everything for you).
Ah, great news! I'm putting plenty of error checking into my code already. If it goes wrong then it'll probably lose 2.5kW of RF into the wrong place and set fire to things! I'll make sure it's damn good before then though, and it's more likely that the thing I'm controlling I haven't build yet will catch fire through no fault of the arduino!
Just didn't know if there was other stuff going on in the data, so it would work perfectly fine using the on-board USB and its associated chip, but once trying RS485 via the pins on the chip and a MAX485 if it would seem to be gobbledegook! If that was the case suicide might be one of my better options at this point! XD