I am trying to use Nick's http://www.gammon.com.au/forum/?id=11428 RS485 Protocol Library. I am sending data from one Arduino with sensors to another Arduino via RS-485. My plan is to have 2 remote Arduino's sending sensor data to a 3rd Arduino (Mega) with an LCD and touchscreen. I had this all working using i2c, but due to the length of the cables it had problems so I am switching it to RS485. I am doing a test right now with 1 remote sending data to another, which then sends that data to my serial monitor to confirm it is receiving the data. Here is the code for the remote;
// assemble message
byte msg [] = {
1, // device 1
2, // outside info
Whole, // out humidity
Whole1, // outdoor temp
pressure, // baro pressure
atm // atmospheric pressure
};
// send to slave
digitalWrite (EN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (EN, LOW); // disable sending
I have verified with my PC via usb>RS-485 adapter that the data is being sent, but I can't receive the data on the arduino using Nick's code;
#include <SoftwareSerial.h>
#include "RS485_protocol.h"
#define RxD 11
#define TxD 12
SoftwareSerial mySerial(RxD,TxD);
int EN = 13;
void fWrite (const byte what)
{
mySerial.print (what);
}
int fAvailable ()
{
return mySerial.available ();
}
int fRead ()
{
return mySerial.read ();
}
void setup()
{
mySerial.begin(57600);
Serial.begin (57600);
Serial.println("starting");
pinMode(EN, OUTPUT );
}
void loop()
{
byte buf [32];
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf) - 1);
if (received)
{
Serial.println("received");
if (buf [0] != 1)
return; // not my device
if (buf [1] != 2)
return; // unknown command
Serial.println(buf [2]);
} // end if something received
}
With the above code I should see "received" in the serial monitor if anything is received. If I use the following in my loop I receive the data no problem;
String content = "";
char character;
while(mySerial.available()) {
character = mySerial.read();
content.concat(character);
}
if (content != "") {
content.toCharArray (charBuf, 40);
Serial.println(charBuf);
}
so my hardware seems to be working fine - but I must be doing something wrong here?