Master and slave at the same time

Hi,
I also trying to interpose between 2 devices one is slave (a smart meter) and one is master (invertor). They are from different manufactures therefore they can't communicate to each other.

I want to do this by using a Arduino board (I have a Nano) and use 2 TTL RS485 modules (MAX485). One to connect to one device and one to connect to the second one. In the loop, I want to read from one serial port (the one connected to slave) and write to the other (the one connected to the master).

Problem I am facing, is that the modbus class (and I tried few) works ok with the standard hardware serial port.. but not with any soft serial ports that i created. Thus I need to connect the MAX 485 module to RX and TX pins.. I one case it could work.. but not if I want to use 2 modules.. I can't connect them both to RX TX pins...

Is this setup (one board with 2 RS485 modules) a viable setup? Or just theoretically?
Any suggestions?

Thanks
Radu

@

Topic split from another topic. Please do not add your own questions to the end of other people's topics.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Please show your code.

yes so long as the microcontroller has two hardware serial ports, e.g. a Mega

I'm convinced that Modbus Master Library from Doc Walker (installed with the library manager) works with HW Serial AND SoftSerial.

Similar my Modbus Server Simple Library will work with HW Serial AND SoftSerial.
https://werner.rothschopf.net/microcontroller/202112_arduino_modbus_server.htm

unfortunately , I can't upload attachments (new user)...

Here is the code:

#include <ModbusMaster.h>
#include <SoftwareSerial.h>


#define RS485_DE_RE 2
ModbusMaster node;
SoftwareSerial mySerial (5, 6);
void preTransmission()
{
  digitalWrite(RS485_DE_RE, HIGH);
}
void postTransmission()
{
  digitalWrite(RS485_DE_RE, LOW);
}
// the setup function runs once when you press reset or power the board
void setup() {
  pinMode(RS485_DE_RE,OUTPUT);
  postTransmission();
 
  Serial.begin(19200);
  while (!Serial);

  mySerial.begin(19200);
  node.begin(1,mySerial);

  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

// the loop function runs over and over again forever
void loop() {
  uint8_t resultMain;
  resultMain = node.readHoldingRegisters(0x0115, 3);
  if (resultMain == node.ku8MBSuccess){
    Serial.print("Read 0x0115");
  }
  else {
    Serial.print("error 0x0115:");
    Serial.println(resultMain);
  }

  // resultMain = node.readHoldingRegisters(0x0109, 3);
  // if (resultMain == node.ku8MBSuccess){
  //   Serial.print("Read 0x0109");
  // }
  // else {
  //   Serial.print("error 0x0109:");
  //   Serial.println(resultMain);
  // }

  // resultMain = node.readHoldingRegisters(0x0025, 3);
  // if (resultMain == node.ku8MBSuccess){
  //   Serial.print("Read 0x0025");
  // }
  // else {
  //   Serial.print("error 0x0025:");
  //   Serial.println(resultMain);
  // }

  delay(5000);
}


@noiasca as you can see, ModbusMaster is exactly what I use. If I replace mySerial by Serial.. (and ofcourse I switch the pins to RX, TX) it reads properly.

that's not how it works.
you must create the SoftSerial in global scope - not in setup()

further more, don't call digitalWrite with 0 nor 1. Use LOW and HIGH

Yes.. I was afraid that this is the only solution.. I still hope there is one where I can use Nano instead...

are you sure it is a master?
can you post a link to the datasheet?

rs-485 devices are typically slaves on the same bus and the controller (arduino) is the master

Thanks @noiasca for the suggestions. The code above is updated. Still, the result is the same

try altSoftSerial

On the photovoltaic setup, the invertor is master (client) and is querying the slave(server) which is the smart meter. If I intervene between, on one side my Arduino needs to act as master to interrogate the smart meter, and on the other side , needs to act as a slave so it can reply when the invertor is querying.

This setup works fine on a PC.. but I want to go next level and replace my pc by an Arduino

Please, NEVER update the code that ALREADY sent. Insert it as a new message.

1 Like

I did that already... This library has also a downside... the pins for RX and TX are fixed based on the board.. thus I can not have 2 instances of an AltSerial port...

I am not sure what you were intending to attach, but if it is your code then it is much better to post it in a reply in code tags

To post images etc you need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

Users at trust level 1 can…

  • Use all core Discourse functions; all new user restrictions are removed
  • Send PMs
  • Upload images and attachments

indeed I thought initially to attach the code. Afterwards, I discovered the tags :slight_smile: . Thank you!

could you switch to a Arduino Nano 33 IoT which has a SAMD21 processor which supports multiple hardware ports

Why are you stuck on a Nano? Make your life easier and switch to a more appropriate platform. There are plenty of boards in the Arduino Ecosystem that support multiple Hardware UART ports.
I'd recommend a Teensy or one of the Adafruit Feathers.

Thanks @horace @gfvalvo ! I am not stuck to Nano.. just wanted to be sure that I am not doing something wrong before starting to spend more money on different boards.. Nano was a choice because of the size.. which allows us to put it in a fuse box afterwards... but I see your suggestions are comparable in size . so probably this will be the next step.. (unless someone will point a solution in the current setup :slight_smile: ).