de:project:modbus_rtu_uno_master_sw_serial

Modbus RTU Master mit SW-Serial

Das Programm läuft an Arduino UNO. UNO hat nur einen seriellen Port, daher ist SW-Serial eine sehr nützliche Funktion, damit mehrere Quasi-UARTs öffnen können. Problem ist, dass es nicht mehreren Kommunikationslösungen funktioniert mit. In diesem Fall fungiert der Arduino Modbus als RTU-Master und führt gleichzeitig den seriellen Monitor aus.

Modbus RTU Master mit SW-Serial

// Arduino UNO
// Modbus RTU - Master
// comm over sw serial
 
#include <ModbusRtu.h>
#include <SoftwareSerial.h>
 
// data array for modbus network sharing
uint16_t au16data[16];
uint8_t u8state;
#define SSerialTxControl 10
/**
 *  Modbus object declaration
 *  u8id : node id = 0 for master, = 1..247 for slave
 *  u8serno : serial port (use 0 for Serial)
 */
 
// this is master USB-FTDI via software serial 
Modbus master(0); 
/**
 * This is an structe which contains a query to an slave device
 */
modbus_t telegram;
 
unsigned long u32wait;
 
SoftwareSerial mySerial(2, 3);//Create a SoftwareSerial object so that we can use software serial. Search "software serial" on Arduino.cc to find out more details.
 
void setup() {
  //use the hardware serial if you want to connect to your computer via usb cable 
  Serial.begin(9600);
  // begin the ModBus object. The first parameter is the address of your SoftwareSerial address. 
  // Do not forget the "&". 9600 means baud-rate at 9600
  master.begin( &mySerial, 9600, SSerialTxControl ); 
  // if there is no answer in 2000 ms, roll over
  master.setTimeOut( 2000 ); 
  u32wait = millis() + 1000;
  u8state = 0; 
}
 
void loop() {
  switch( u8state ) {
  case 0: 
    if (millis() > u32wait) u8state++; // wait state
    break;
  case 1: 
    telegram.u8id = 0x01;         // slave address
    // master functionen: 
    // 03: read holding reg., 06: write holdong reg., 10: write more holding reg.
    telegram.u8fct = 0x03;        // function code
    telegram.u16RegAdd = 0x00;    // start address in slave
    telegram.u16CoilsNo = 0x10;   // number of elements (coils or registers) to read
    telegram.au16reg = au16data;  // pointer to a memory array in the Arduino
 
    master.query( telegram );     // send query (only once)
    u8state++;
    break;
  case 2:
    master.poll();                // check incoming messages
    if (master.getState() == COM_IDLE) {
      u8state = 0;
      u32wait = millis() + 2000;  // wait for answer
        // print it to serial monitor 
        Serial.print("Register 0x00 :");
        Serial.println(au16data[0]); 
        Serial.print("Register 0x01 :");
        Serial.println(au16data[1]); 
        Serial.print("Register 0x02 :");
        Serial.println(au16data[2]); 
        Serial.print("Register 0x03 :");
        Serial.println(au16data[3]); 
        Serial.print("Register 0x04 :");
        Serial.println(au16data[4]); 
        Serial.print("Register 0x05 :");
        Serial.println(au16data[5]); 
        Serial.print("Register 0x06 :");
        Serial.println(au16data[6]); 
        Serial.print("Register 0x07 :");
        Serial.println(au16data[7]); 
        Serial.print("Register 0x08 :");
        Serial.println(au16data[8]); 
        Serial.print("Register 0x09 :");
        Serial.println(au16data[9]); 
 
    }
    break;
  }
}

  • de/project/modbus_rtu_uno_master_sw_serial.txt
  • 2022/04/21 15:01
  • ()