Arduino from Modbus rtu to Modbus tcp

alverman
2017-07-25
2017-08-09
  • alverman - 2017-07-25

    Goodmorning everyone,
    So far I used the attached code that uses the modbus rtu protocol but now for various reasons I have to move on modbus tcp.
    The structure of the code I used would like to keep it as it is.
    Can you tell me a modbus tcp library for arduino and point me a method to apply it to the code?

    \#include <Controllino.h>  /* Usage of CONTROLLINO library allows you to use CONTROLLINO_xx aliases in your sketch. */
    \#include <ModbusRtu.h>
    \#define ID 3
    Modbus slave(ID, 0, 0); // this is slave ID and RS-232 or USB-FTDI
    int8_t state = 0;
    // data array for modbus network sharing
    uint16_t au16data[9];
    void setup() {
      io_setup(); // I/O settings
      slave.begin( 38400 );
    }
    void loop() {
      // poll messages
      state = slave.poll( au16data, 9 );
      io_poll();
    } 
    void io_setup() {
      //Set digital out
      pinMode(CONTROLLINO_R0, OUTPUT); // next to the digital output screw terminal 
      pinMode(CONTROLLINO_R1, OUTPUT); // next to the digital output screw terminal
      pinMode(CONTROLLINO_R2, OUTPUT); // next to the digital output screw terminal
      pinMode(CONTROLLINO_R3, OUTPUT); // next to the digital output screw terminal
      pinMode(CONTROLLINO_R4, OUTPUT); // next to the digital output screw terminal
      pinMode(CONTROLLINO_R5, OUTPUT); // next to the digital output screw terminal
      pinMode(CONTROLLINO_R6, OUTPUT); // next to the digital output screw terminal
      pinMode(CONTROLLINO_R7, OUTPUT); // next to the digital output screw terminal
      pinMode(CONTROLLINO_R8, OUTPUT); // next to the digital output screw terminal
      pinMode(CONTROLLINO_R9, OUTPUT); // next to the digital output screw terminal
      // Set analog in (used as digital)
      pinMode(CONTROLLINO_A0, INPUT);  // next to the analog input screw terminal
      pinMode(CONTROLLINO_A1, INPUT);  // next to the analog input screw terminal
      pinMode(CONTROLLINO_A2, INPUT);  // next to the analog input screw terminal
      pinMode(CONTROLLINO_A3, INPUT);  // next to the analog input screw terminal
      pinMode(CONTROLLINO_A4, INPUT);  // next to the analog input screw terminal
      pinMode(CONTROLLINO_A5, INPUT);  // next to the analog input screw terminal
      pinMode(CONTROLLINO_A6, INPUT);  // next to the analog input screw terminal
      pinMode(CONTROLLINO_A7, INPUT);  // next to the analog input screw terminal
      pinMode(CONTROLLINO_A8, INPUT);  // next to the analog input screw terminal
      pinMode(CONTROLLINO_A9, INPUT);  // next to the analog input screw terminal
    }
    void io_poll() {
      // get analog inputs -> au16data[0]
      bitWrite( au16data[0], 0, digitalRead(CONTROLLINO_A0));
      bitWrite( au16data[0], 1, digitalRead(CONTROLLINO_A1));
      bitWrite( au16data[0], 2, digitalRead(CONTROLLINO_A2));
      bitWrite( au16data[0], 3, digitalRead(CONTROLLINO_A3));
      bitWrite( au16data[0], 4, digitalRead(CONTROLLINO_A4));
      bitWrite( au16data[0], 5, digitalRead(CONTROLLINO_A5));
      bitWrite( au16data[0], 6, digitalRead(CONTROLLINO_A6));
      bitWrite( au16data[0], 7, digitalRead(CONTROLLINO_A7));
      bitWrite( au16data[0], 8, digitalRead(CONTROLLINO_A8));
      bitWrite( au16data[0], 9, digitalRead(CONTROLLINO_A9));
      //bitWrite( au16data[0], 10, digitalRead(CONTROLLINO_IN0));
      //bitWrite( au16data[0], 11, digitalRead(CONTROLLINO_IN1));
      // set digital outputs -> au16data[1]
      digitalWrite( CONTROLLINO_R0, bitRead( au16data[1], 0 ));
      digitalWrite( CONTROLLINO_R1, bitRead( au16data[1], 1 ));
      digitalWrite( CONTROLLINO_R2, bitRead( au16data[1], 2 ));
      digitalWrite( CONTROLLINO_R3, bitRead( au16data[1], 3 ));
      digitalWrite( CONTROLLINO_R4, bitRead( au16data[1], 4 ));
      digitalWrite( CONTROLLINO_R5, bitRead( au16data[1], 5 ));
      digitalWrite( CONTROLLINO_R6, bitRead( au16data[1], 6 ));
      digitalWrite( CONTROLLINO_R7, bitRead( au16data[1], 7 ));
      digitalWrite( CONTROLLINO_R8, bitRead( au16data[1], 8 ));
      digitalWrite( CONTROLLINO_R9, bitRead( au16data[1], 9 ));
      // diagnose communication
      au16data[6] = slave.getInCnt();
      au16data[7] = slave.getOutCnt();
      au16data[8] = slave.getErrCnt();
    }
    
     

    Related

    Talk.ru: 1
    Talk.ru: 7
    Talk.ru: 8


Log in to post a comment.