Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please help to test my device ( project ) #346

Open
proasnet opened this issue Apr 25, 2024 · 1 comment
Open

Please help to test my device ( project ) #346

proasnet opened this issue Apr 25, 2024 · 1 comment

Comments

@proasnet
Copy link

Dear @emelianov, I had my first project with Modbus RTU. I need only help to test a my hardware, custom board. Programs is only for HW testing, from your examples.
My device project is as a slave. CPU is ESP32 WROOM, with modbus converter MAX3485 with DE.
Some is bad, communication between this CPUs not working.

obrázok

Slave program

#include <HardwareSerial.h>
//#include <SoftwareSerial.h>
#include <ModbusRTU.h>
#define TXenableRS485 21
#define M_RX 34
#define M_TX 32


HardwareSerial RS485(1);
//EspSoftwareSerial::UART RS485;
ModbusRTU mb;

void setup() {
  Serial.begin( 115200 );
  

//  pinMode( TXenableRS485 , OUTPUT );
//  digitalWrite( TXenableRS485 , LOW );

  RS485.begin( 9600, SERIAL_8N1 , M_RX , M_TX );
  
  //RS485.begin( 9600 , SWSERIAL_8N1 , M_RX , M_TX , false );

  mb.begin( &RS485 , TXenableRS485 );
  mb.setBaudrate( 9600 );
  mb.slave( 1 );

  // RTU SERVER
  mb.addIreg( 100 ); // analog input
  mb.addIreg( 101 ); // analog input
  mb.addIreg( 102 ); // analog input
  mb.addIreg( 103 ); // analog input
  mb.addIreg( 104 ); // analog input

  // RTU SERVER
  mb.addIsts( 100 ); // digital input
  mb.addIsts( 101 ); // digital input
  mb.addIsts( 102 ); // digital input
  mb.addIsts( 103 ); // digital input
  mb.addIsts( 104 ); // digital input

  // RTU SERVER
  mb.addHreg( 100 ); // holding
  mb.addHreg( 101 ); // holding
  mb.addHreg( 102 ); // holding
  mb.addHreg( 103 ); // holding
  mb.addHreg( 104 ); // holding

  // RTU SERVER
  mb.addCoil( 100 ); // coil
  mb.addCoil( 101 ); // coil
  mb.addCoil( 102 ); // coil
  mb.addCoil( 103 ); // coil
  mb.addCoil( 104 ); // coil
}

void loop() {
  mb.task();                // Server Modbus TCP queries

  mb.Ireg( 100 , 400 ); // analog in
  mb.Ireg( 101 , 401 ); // analog in
  mb.Ireg( 102 , 402 ); // analog in
  mb.Ireg( 103 , 403 ); // analog in
  mb.Ireg( 104 , 404 ); // analog in

  mb.Ists( 100 , true ); // digital input
  mb.Ists( 101 , false ); // digital input
  mb.Ists( 102 , true ); // digital input
  mb.Ists( 103 , false ); // digital input
  mb.Ists( 104 , true ); // digital input

  mb.Hreg( 100 , 511 ); // holding
  mb.Hreg( 101 , 501 ); // holding
  mb.Hreg( 102 , 502 ); // holding
  mb.Hreg( 103 , 503 ); // holding
  mb.Hreg( 104 , 504 ); // holding

  Serial.println ( "coil 0: " + String( mb.Coil( 0 )) + " coil 2: " + String( mb.Coil( 1 )) + " coil 2: " + String( mb.Coil( 2 )) + " coil 3: " + String( mb.Coil( 3 )));

  yield();
}

For testing this my hardware project, I mount a master RTU with the same converter, EPS8266 Wemos D1 mini.
Master program

#include <ModbusRTU.h>

#include <SoftwareSerial.h>

#define SLAVE_ID 1
#define FIRST_REG 100
#define REG_COUNT 5

SoftwareSerial S(7, 5);


ModbusRTU mb;

bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { // Callback to monitor errors
  if (event != Modbus::EX_SUCCESS) {
    Serial.print("Request result: 0x");
    Serial.print(event, HEX);
  }
  return true;
}

void setup() {
  Serial.begin(115200);
  S.begin(9600, SWSERIAL_8N1);
  mb.begin(&S);
  mb.master();
}

uint16_t res[REG_COUNT];

void loop() {
  
  if (!mb.slave()) {    // Check if no transaction in progress
    mb.readHreg(SLAVE_ID, FIRST_REG, res, REG_COUNT, cb); // Send Read Hreg from Modbus Server
    while (mb.slave()) { // Check if transaction is active
      mb.task();
      delay(10);
    }
    Serial.println(res[0]);
    Serial.println(res[1]);
    Serial.println(res[2]);
    Serial.println(res[3]);
    Serial.println(res[4]);
  }
  delay(1000);
}

Thank you very much for help.

@emelianov
Copy link
Owner

If both rs-485 converters are the same server also needed to have RE/DE pins control from code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants