Daisy chain between RS-485's connected to arduino mega's

adwsystems:
The difference is that both the send and receive lines can be active at the same time in full-duplex.

the send lines become receive lines for all other nodes, while on the receive line for this node, there can only be one other node sending at the same time. So either way there has to be some kind of discipline about who sends when and what, as in a full duplex there can only be 2 nodes sending at the same time.

Unless you actually do start daisy-chaining by letting every node receive and echo. for any node half-way this would mean 2 ports, but since these are Mega's that is possible.

TomGeorge:
Hi,
Can you tell us the actual application you are going to use this comms system in?
You talk about breadboard, but what is the final aim?
What are the three controllers doing?
How far apart are they?

Thanks.. Tom.. :slight_smile:

The goal of the project for now is to have a network of micro-controllers by using the mentioned chips, MAX3490. And to accomplish this the main thing I am trying to get right is a functional master-slave setup, for which i came to the realization that i need to implement Modbus RTU protocol for the proper master-slave addressing, however, I am having new issues with the protocol I juts mentioned. So, a new and different question will be, what's recommended to implement Modbus in my current setup of Mega's and MAX chips. T

Thanks.. Samuel :slight_smile:

If you are going to use the MAX3490, then you can begin testing two nodes without the chips.

Step 1. Get two nodes communicating with the modbus protocol WITHOUT using the MAX3490/RS-485 chips. Just cross connect the TX/RX lines between two Arduinos.

Step 2. When you have step 1 working, it will be a simple matter to insert the MAX3490 chips in place of the direct connection.

Step 3. Add your third node.

Let us know when you have step 1 completed.

adwsystems:
If you are going to use the MAX3490, then you can begin testing two nodes without the chips.

Step 1. Get two nodes communicating with the modbus protocol WITHOUT using the MAX3490/RS-485 chips. Just cross connect the TX/RX lines between two Arduinos.

Step 2. When you have step 1 working, it will be a simple matter to insert the MAX3490 chips in place of the direct connection.

Step 3. Add your third node.

Let us know when you have step 1 completed.

Thank you for the suggestion. I just did what you asked me with a program that's setup to do master slave communication based on RS485, and it worked. The following are the codes for the 2 programs. What I am just wondering now is if this pair of code is appropriate for what I am trying to accomplish? I am asking because I run into any pairs of code as I am trying to learn this and they all seem very similar yet do they have a slighly different application.

Thank you,
Samuel

---------------------------------------------------MASTER CODE---------------------------------------------------------

#include <RS485.h>
#include <SimpleComm.h>

// Create SimplePacket for sending and receiving data
SimplePacket packet;
SimplePacket packet2;

// Define master address
uint8_t masterAddress = 0;

// Define slave address to communicate with
uint8_t slaveAddress = 1;
uint8_t slaveAddress2 = 2;
// Value to send as packet data
int value = 0;
int value2 = 0;

//int hold;
//int hold2;

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600L);

// Start RS485
Serial1.begin(19200L);
Serial1.setTimeout(20);

// Start SimpleComm
SimpleComm.begin(masterAddress);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
static unsigned long lastSent = millis();
// Send packet periodically: once per second
if (millis() - lastSent >= 10000) {

// Set request packet data
packet.setData(value);
packet2.setData(value2);

// Send request to slave 1
if (SimpleComm.send(Serial1, packet, slaveAddress)) {
lastSent = millis();

Serial.print("Sent value (To Slave 1): ");
Serial.println(value);
Serial.println(packet.getDestination());

}

// Send request to slave 2
// if (SimpleComm.send(Serial1, packet2, slaveAddress2))
// {
// lastSent = millis();
//
// Serial.print("Sent value (to Slave 2): ");
// Serial.println(value2);
// }
}

/************** Get responses *****************/

/* if (SimpleComm.receive(Serial1, packet)|| SimpleComm.receive(Serial1, packet2)) {
hold = packet.getInt();
hold2 = packet2.getInt();
switch(packet.getSource() || packet2.getSource()){
case 1:
value = hold;
Serial.print("Received value (From Slave 1): ");
Serial.println(value);
break;

case 2:
value2 = hold2;
Serial.print("Received value (From Slave 2): ");
Serial.println(value2);
break;
}

}
*/
// Slave-1 responses
if (SimpleComm.receive(Serial1, packet)) {
// Update value from the response
value = packet.getInt();

Serial.print("Received value (From Slave 1): ");
Serial.println(value);
}
// Slave-2 responses
// if (SimpleComm.receive(Serial1, packet2)) {
// // Update value from the response
// value2 = packet2.getInt();
//
// Serial.print("Received value (From Slave 2): ");
// Serial.println(value2);
// }
}

-----------------------------------------------------SLAVE CODE---------------------------------------------------------

#include <RS485.h>
#include <SimpleComm.h>

// Create SimplePacket for sending and receiving data
SimplePacket request;
SimplePacket response;

// Define slave address to communicate with
uint8_t slaveAddress = 1;
uint8_t master_address = 0;

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600L);

// Start RS485
Serial1.begin(19200L);
Serial1.setTimeout(30);

// Start SimpleComm
SimpleComm.begin(slaveAddress);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
Serial1.flush();
// Get requests
if (SimpleComm.receive(Serial1, request)) {
int value = request.getInt();

Serial.print("Received value (From Master): ");
Serial.println(value);
Serial.println(response.getSource());

delay(1000);
// Process value
value+=2;

// Send response to the request packet source
response.setData(value);
if (SimpleComm.send(Serial1, response, master_address)) {
Serial.print("Sent value: ");
Serial.println(value);
}
}
delay(5000);
}

samuel1010:
Thank you for the suggestion. I just did what you asked me with a program that's setup to do master slave communication based on RS485, and it worked. The following are the codes for the 2 programs. What I am just wondering now is if this pair of code is appropriate for what I am trying to accomplish? I am asking because I run into any pairs of code as I am trying to learn this and they all seem very similar yet do they have a slighly different application.

Thank you,
Samuel

Not sure. You have told us anything more than you want to communicate between three arduinos using RS-485.

I can tell you the use of delay() and asynchronous communications are not compatible. The delay() statement holds off all other program execution until it is complete. Look for the sticky thread at the top of one of the groups here on the proper use of millis(). (or if you are lucky someone will post a link for you).