Hi all,
first of all, I'm not an expert... I'm experimenting with RS485 about a Master/Slaves project.
With my tests, I'm able to create a Master/Slaves network where the Master is an Arduino Mega and the slaves are Arduino Nano Every.
Basically, I'm using the Serial1 on all devices. I would like to use an HW Serial instead of the Software Serial.
I did stupid examples, just to test the connections... Seems works fine.
Now, I would like to start implementing the real protocol.
I found this library: GitHub - IndustrialShields/arduino-SimpleComm
It's not clear for me if I can use it for the RS485 based on the Serial1 (or in general HW Serial).
Is it possible?
In general, do you have similar suggestions to have this?
on the Mega Serial1 is a hardware serial port on pins 18 and 19
for details of the Nano Every have a look at arduino-nano-every-serial-ports
looks like no need to use SoftwareSerial
The library includes an example of an RS485 master device and an RS485 slave device. However I could not figure out which RS485 library the author was using in their examples.
There is an ArduinoRS485 library that is specifically for the MKR485 board. You could use that with a bit of alteration.
There's also the RS485 library by Rob Tillaart that you could use. That library lets you specify the serial port you are going to use as well as the pin used to control the Tx/Rx state of the RS485 line driver chip, assuming that you are going to operate in half duplex mode.
The MAX485 modules that are available have separate pins to control the Tx and RX line driver/receiver - called RE & DE. You can control both RE & DE from 1 pin.
My question is not related to the connections schema. As I wrote, I’m able to use the RS485 using the Serial1. I’m find for a library in order to semplify the protocol implementation....
I tried the library by Rob Tillaart that you suggested.
I integrated it into the example (master/slave) provided with the SimpleComm library that I mentioned in my question.
Here the code about the RS485 integration that I integrated:
MASTER:
const uint8_t sendPin = 3;
const uint8_t deviceID = 0;
RS485 RS485(&Serial1, sendPin, deviceID);
void setup() {
Serial.begin(9600L);
// Start RS485
Serial1.begin(19200L);
RS485.setTimeout(20);
// Start SimpleComm
SimpleComm.begin(masterAddress);
}
void loop() {
static unsigned long lastSent = millis();
// Send packet periodically: once per second
if (millis() - lastSent >= 1000) {
// Set request packet data
packet.setData(value);
// Send request to slave
if (SimpleComm.send(RS485, packet, slaveAddress)) {
lastSent = millis();
Serial.print("Sent value: ");
Serial.println(value);
}
}
// Get responses
if (SimpleComm.receive(RS485, packet)) {
// Update value from the response
value = packet.getInt();
Serial.print("Received value: ");
Serial.println(value);
}
}
SLAVE:
const uint8_t sendPin = 3;
const uint8_t deviceID = 1;
RS485 RS485(&Serial1, sendPin, deviceID);
void setup() {
Serial.begin(9600L);
// Start RS485
Serial1.begin(19200L);
RS485.setTimeout(20);
// Start SimpleComm
SimpleComm.begin(slaveAddress);
}
void loop() {
// Get requests
if (SimpleComm.receive(RS485, request)) {
int value = request.getInt();
Serial.print("Received value: ");
Serial.println(value);
// Process value
value++;
// Send response to the request packet source
response.setData(value);
if (SimpleComm.send(RS485, response, request.getSource())) {
Serial.print("Sent value: ");
Serial.println(value);
}
}
}
In this way I'm able to send the messages from the Master to the Slave but the viceversa doesn't work: when the Slave will send back the changed value, it will not be received on the Master.
I think that the pins connection it's correct because I also tried a simple sketch (using the RS485 module + Serial1) where I send a message from the Master to the Salve a viceversa: this works fine.
I'm using:
- an Elegoo ATMega 280 as Master;
- an Arduino Nano Every as Slave;
- Two RS485 Module like this: YWBL-WH 5PCS TTL to RS-485 module /MAX485 / RS485 5V modulo TTL a RS-485 MCU Development Board : Amazon.it: Commercio, Industria e Scienza
Is there any HW limitation using the suggested library?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.