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);
}