Sorry about the long delay, I have been hijacked by other things.
In desperation I have totally rebuilt the test rig for this project, using Veroboard and soldering all joints possible to avoid issues of poor connectivity.
I have also tried various different schemes in the software, but right now if I am plugged into the Master Uno, the Serial output shows just "Sending data of 51". There is no apparent response from the Slave Uno.
I have lowered the comms speed to 9600, and attach hereto a screenshot of a Fritzing wiring diagram plus the outputs from both serial monitor sessions and finally the pretty short code for both sketches are included.
In this setup I am assuming that relevant line resistors are supplied by the boards themselves. The documentation is unclear about this. but my previous system did have additional resistpors soldered into it. so there does not appear to be any difference..
The twisted pair go A-A and B-B as specified, although I have tried reversing one end to no avail.
Master "sender" code
// Send
#include <RS485_protocol.h>
#include <SoftwareSerial.h>
#define RS485RECEIVEPIN 10
#define RS485TRANSMITPIN 11
#define RS485ENABLE_PIN 3
#define RS485OUTPUTPIN 7
#define MASTERID '1'
#define SLAVEID1 5
byte Message[] = {SLAVEID1,1};
int address = 0;
int command = 0;
bool rs485Data = false;
char gbuff[128]="";
SoftwareSerial rs485 (RS485RECEIVEPIN, RS485TRANSMITPIN); // receive pin, transmit pin
void fWrite (const byte what){
rs485.write (what); }
int fAvailable (){
return rs485.available (); }
int fRead (){
return rs485.read (); }
void setup()
{
Serial.begin(9600);
Serial.println("System Startup - Sender");
rs485.begin(9600);
pinMode (RS485ENABLE_PIN, OUTPUT); // driver output enable
Serial.println("Setup completed...");
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loop()
{
// start:
delay (150);
Message[0] = SLAVEID1;
Message[1] = 1;
Serial.print("Sending: ");
Serial.print(Message[0]);
Serial.println(Message[1]);
digitalWrite (RS485ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, (const byte*)Message, sizeof Message);
delayMicroseconds (660);
digitalWrite (RS485ENABLE_PIN, LOW); // disable sending
// receive any available message
byte buf []={2,0};
// while (fAvailable()){
if(recvMsg (fAvailable, fRead, buf, sizeof buf)){
// rs485Data = true;
Serial.print(buf[0]);
Serial.println(buf[1]);
sprintf(gbuff, "Message [%d%d] received...", buf[0], buf[1]);
Serial.println(gbuff);
address = buf[0];
command = buf[1];
buf[0] = '\0';
// if (rs485Data){
if (address >= 0 && command < 2){
sprintf(gbuff, "Message received = %d%d", address, command);
Serial.println(gbuff);
if (address != MASTERID){ // SHOULD BE MASTERID
Serial.println("Message is NOT for us...");
Serial.println(gbuff);
return; // unknown command
}
if (command > 1){ // command = 0/1
sprintf(gbuff, "Command [%d%d] is NOT recognised...", address, command);
Serial.println(gbuff);
return; // unknown command
}
if (digitalRead(13) == HIGH){
digitalWrite (13, LOW); // set LED to command value of 0 or 1 (On or Off)
Serial.println("Setting LED OFF");
}
else{
digitalWrite (13, HIGH); // set LED to command value of 0 or 1 (On or Off)
Serial.println("Setting LED ON");
}
// }
// rs485Data = false;
}
}
}
Slave "Receiver" code
#include "RS485_protocol.h"
#include <SoftwareSerial.h>
// required for RS485 communications
// I AM USING NICK GAMMONS LIBRARY FOR THIS, AS IT WORKS GREAT
#define RS485RECEIVEPIN 10
#define RS485TRANSMITPIN 11
#define RS485ENABLE_PIN 3
#define RS485OUTPUTPIN 7
#define SERIALID '5' // this is this Arduinos ID for 2 way serial communications, and/or RS485
#define MASTERID 1
#define SLAVEID_1 5
char gbuff[128] = "";
SoftwareSerial rs485 (RS485RECEIVEPIN, RS485TRANSMITPIN); // receive pin, transmit pin
byte buf[] = {0,0};
int address = 0;
int command = 0;
void fWrite (const byte what){
rs485.write (what); }
int fAvailable (){
return rs485.available (); }
int fRead (){
return rs485.read (); }
void setup(){
Serial.begin(9600);
rs485.begin (9600);
pinMode (RS485ENABLE_PIN, OUTPUT); // driver output enable
Serial.println("Setup completed...");
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loop(){
// delay(100);
Serial.println("Waiting for RS485 data");
if(recvMsg (fAvailable, fRead, buf, sizeof buf)){
sprintf(gbuff, "buf[0] = [%c], buf[1] = [%c]..", buf[0], buf[1]);
Serial.println(gbuff);
address = buf[0];
command = buf[1];
sprintf(gbuff, "Message received = %d%d", address, command);
Serial.println(gbuff);
if (address >= 0 && command < 2){
sprintf(gbuff, "Message received = %d%d", address, command);
Serial.println(gbuff);
if (address != 5){ // ID = 5
Serial.println("but message is NOT for us...");
Serial.println(gbuff);
buf[0] = '\0';
return; // unknown command
}
if (command > 1){ // command = 0/1
sprintf(gbuff, "Command [%d%d] is NOT recognised...", address, command);
Serial.println(gbuff);
buf[0] = '\0';
return; // unknown command
}
if (digitalRead(13) == HIGH){
digitalWrite (13, LOW); // set LED to command value of 0 or 1 (On or Off)
Serial.println("Setting LED OFF");
byte Message[] = {MASTERID,1};
digitalWrite (RS485ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, (const byte*)Message, sizeof Message);
delayMicroseconds (660);
digitalWrite (RS485ENABLE_PIN, LOW); // disable sending
sprintf(gbuff,"Sending: [%d%d]", Message[0], Message[1]);
Serial.println(gbuff);
buf[0] = '\0';
}
else{
digitalWrite (13, HIGH); // set LED to command value of 0 or 1 (On or Off)
Serial.println("Setting LED ON");
byte Message[] = {MASTERID,1};
delay (1); // give the master a moment to prepare to receive
digitalWrite (RS485ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, (const byte*)Message, sizeof Message);
delayMicroseconds (660);
digitalWrite (RS485ENABLE_PIN, LOW); // disable sending
sprintf(gbuff,"Sending: [%d%d]", Message[0], Message[1]);
Serial.println(gbuff);
buf[0] = '\0';
return; // unknown command
}
}else{ // end if something received
Serial.println("No message available...");
}
}
} // end of loop
I sure hope someone can help me out here, as I desperately need this functionality on my model railway project.
Thanks in advance
Ian