Help understanding this RS485 code

Hi to all,

I'm having problems to understand how this code works.

It basically receives data from the RS232 and then forward them to the RS485.
If it receives nothing, it outputs 0 in all the registers.

I need to modify it in order to output -1 if it received nothing.
I cannot understand the line where this instruction is set.
I was expecting to see something like:

"if I receive nothing, then set everything to 0"

But I cannot find this instruction in the code.
If I upload the code into Arduino, I actually receive zero on the RS485 even if I have nothing from the Serial port. I need to set it at -1 when I receive nothing.

Can you help me, please?

#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>
modbusDevice regBank;
modbusSlave slave;
#define RS485TxEnablePin 2
#define RS485Baud 9600
#define RS485Format SERIAL_8E1

void setup() {
 
  Serial1.begin(115200); //dati in ingresso da ESP01
  Serial2.begin(115200); //dati in uscita da ATMEGA
  //Serial.begin(9600);
  regBank.setId(1);
  //Add Analog Input registers to the register bank
  regBank.add(30001);
  regBank.add(30002);
  regBank.add(30003);
  regBank.add(30004);
  regBank.add(30005);
  regBank.add(30006);
  regBank.add(30007);
  regBank.add(30008);
  regBank.add(30009);
  regBank.add(30010);
  regBank.add(30011);
  regBank.add(30012);
  regBank.add(30013);
  regBank.add(30014);
  regBank.add(30015);
  slave._device = &regBank;
  slave.setBaud(&Serial, RS485Baud, RS485Format, RS485TxEnablePin);

}

void loop() {
//myStringC = builds(random(1,100));
  if (Serial1.available() > 0) {
   String rawData = Serial1.readStringUntil('\n'); //Reads data from ESP8266
    int data = rawData.toInt();  //Turns data to integer
    if(data>31){  //Filters none needed data
      char c = data;  //Converts data to readable string
      myString+=c; //Combines string to form one command
    }
    if(data==13){  //When done recieving data
      //Serial.println(myString); //Print received datamyString.remove(0, 1);
      Serial2.println(myString); //Print received data
      myString.remove(0, 1);
      lun = myString.length();
      if (lun>0){
        for (i = 0; i < lun; i++) {
          if (myString[i] == ',') {
            stp = myString.substring(dd, i);
            //Serial.print("substprima ");
            //Serial.println(stp);
            val = stp.toInt();
            regBank.set(StartReg, val); //from 0 - 1023
            //myval[ind] = val; //from 0 - 1023
            //Serial.print("val ");
            //Serial.println(val);
            sta = myString.substring(i + 1);
            //Serial.print("substdopo ");
            //Serial.println(sta);
            ind = ind + 1;
            dd = i + 1;
            StartReg = StartReg + 1;
          }
          if (myString[i] == ';') {
            val = sta.toInt();
            //Serial.print("ultimo ");
            //Serial.println(val);
            regBank.set(StartReg, val); //from 0 - 1023
           //myval[ind] = val; //from 0 - 1023
          }
  }
      }
  //slave.run();
      //delay(1000);
      myString="";
    }

}

slave.run();
StartReg = 30001;
dd = 0;
ind = 0;
//i = 0;
//myStringC[15] = 0;

}

Please tell us what adapters you are using for RS232 and for RS485.

I'm using the MAX485 for the RS485.
To keep things simple, I'm not using RS232, but directly another Arduino connected to the Serial1 on the main Arduino.

So, Serial1 for incoming data and Serial2 to output the data to the MAX485.

Your understanding will be helped by using serial.Print() to print what is being received on the input side.

1 Like

That's true, I used multiple Serial.println() to check what happened at each step.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.