I am attempting to communicate using MAX485 TTL to RS-485 Modules I purchased with an Arduino MEGA and NANO.
I wrote the sketches for the Mega to poll the Nano and the Nano to reply if the data poll was for it.
I am sending 2 numbers with start and end markers ie, <11,1>, the first number is the number associated with the Nano, an ID tag(trgetNumer), the second is a Query tag, If the Nano ID matches the TargetNumber value Defined in the Sketch, then it replies with the data determined by the Query The data will be scores of 4 teams, 1 current scores, 2 high scores, eventually this data will be retrieved from an array.
The issue I am having is that it is not working.
When I connect tx1,rx1 of the Mega to pins 10,11 of the Nano it works fine
When I try to use the rs485 boards I get nothing.
can someone please explain why this is not working? I have a connected to A and B to B, 5 v and ground, common grounds on boards
I have Rx going to R0 and TX going to DI on both boards
Mega code
/*
arduino Mega
sends poll to Nanos and waits for response
Reads a serial input string with start end Markers.
Parses the string to get the data, verifies that the data is correct
created 22 Jun 2018
by Mark Noll
*/
#define SerialTxControl 8 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
const byte numChars = 250;
/*-----( Declare Variables )-----*/
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
boolean newData = false;
int Team1Score;
int Team2Score;
int Team3Score;
int Team4Score;
int TargetNumber;
int nValidate;
int j = 1;
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (!Serial1) {
// wait for serial port to connect. Needed for native USB port only
}
while ((Serial1.available() > 0) && (newData == false)) {
rc = Serial1.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
if (rc == endMarker) {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
Serial1.flush();
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData() {
char * strtokIndx;
strtokIndx = strtok(tempChars, ",");
nValidate = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
TargetNumber = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Team1Score = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Team2Score = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Team3Score = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
Team4Score = atoi(strtokIndx);
}
void setup()
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("Arduino Mega RS485 Serial Communication");
pinMode(Pin13LED, OUTPUT);
pinMode(SerialTxControl, OUTPUT);
digitalWrite(SerialTxControl, RS485Receive); // Init Transceiver
}
void loop()
{
digitalWrite(Pin13LED, HIGH);
digitalWrite(SerialTxControl, RS485Transmit);
if(j==1){
Serial1.println("<11,1>");
}
if(j==-1){
Serial1.println("<11,2>");
}
j=j*-1;
digitalWrite(Pin13LED, LOW);
digitalWrite(SerialTxControl, RS485Receive);
delay(20);// pause to allow Nano to reply
// get the data
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
newData = false;
//Display for now and later VALIDATE and add to array
Serial.print("Validation: ");
Serial.println(nValidate);
Serial.print("Team1: ");
Serial.println(Team1Score);
Serial.print("Team2: ");
Serial.println(Team2Score);
Serial.print("Team3: ");
Serial.println(Team3Score);
Serial.print("Team4: ");
Serial.println(Team4Score);
//delay(60);
}
}
nano code
/*
Reads a serial input string with start end Markers.
Parses the string to get the data, verifies that it is for this Module,
if it is, it Sends the Data requested by the poll
created 22 Jun 2018
by Mark Noll
*/
//Arduino Nano
#include <SoftwareSerial.h>
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define TargetNumber 11 // number of this target, each target has unique ID 11-27
#define SSerialTxControl 8 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define LEDPin 13
int nTarget; // target number (this trget)
int nQuery; // Query 1 = team scores, 2= possibly use for highest scores saved to array
const byte numChars = 250;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
boolean newData = false;
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while ((RS485Serial.available() > 0) && (newData == false)) {
rc = RS485Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
if (rc == endMarker) {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
nTarget = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
;
nQuery = atoi(strtokIndx); // convert this part to an integer
}
void setup() {
pinMode(SSerialTxControl, OUTPUT);//Control pin for RS485
pinMode(LEDPin, OUTPUT);// LED
// Open serial communications and wait for port to open:
RS485Serial.begin(9600);
Serial.begin(9600);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
while (!RS485Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("RS485 serial established");
}
void loop() {
// get the data
digitalWrite(LEDPin,LOW);
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
//test data
Serial.print("received :");
Serial.println(tempChars);
parseData();
newData = false;
if (nTarget == TargetNumber){
if (nQuery == 1){// polled for team scores
digitalWrite(LEDPin,HIGH);
// debugging making sure we hve serial
digitalWrite(SSerialTxControl, RS485Transmit);
RS485Serial.println("<99,11,11,22,33,44>"); // Send the byte back test data
Serial.println("sent: <99,11,11,22,33,44>" );
delay(10);
digitalWrite(SSerialTxControl, RS485Receive);
}
if (nQuery == 2){// polled for team scores
digitalWrite(LEDPin,HIGH); // flash when transmit
// debugging making sure we hve serial
digitalWrite(SSerialTxControl, RS485Transmit);
RS485Serial.println("<99,11,1,2,3,4>"); // Send the byte back test data 99 validation, 11, 22,33,44 test data for teams
Serial.println("sent: <99,11,1,2,3,4>" );
delay(10);
digitalWrite(SSerialTxControl, RS485Receive);
}
}
newData = false;
}
}