matthew7047
I have several Arduino/ESP32 based devices that poll data using modbus RTU. Because Open PLC is visual and easier for technicians to learn I want to utilize the Arduino extension to pull the data and return it to OpenPlc ( which I was told could not do it without the USB to RS485 converter,  
 
How can I bring the data into OpenPLC?  I was thinking I could do it with a routine something like this in the Arduino extension.
the main routine sets up the communication and builds the modbus RTU string for ( slaveID, start address, number of registers. and calculates the CRC16 value. )   I would use similiar functions for coils, discrete inputs, and input registers.
 
 
        // Update OpenPLC input variables
        //IX[0] = node.getResponseBuffer(0); // First coil state to %IX0.0
        //IW[0] = node.getResponseBuffer(1); // First input register to %IW0
       // QX[0] = node.getResponseBuffer(2); // FIrst Holding register to %QX0
 
 
// Function to read holding registers
void readHoldingRegisters() {
  if (enable_FC03) {
    uint8_t result = node.readHoldingRegisters(offset_holdingRegisters, num_holdingRegisters);
    if (result == node.ku8MBSuccess) {
      for (uint16_t i = 0; i < num_holdingRegisters; i++) {
        uint16_t registerValue = node.getResponseBuffer(i);
        OpenPLC_updateHoldingRegister(i + offset_holdingRegisters, registerValue);
      }
    }
  }
}
 
I was unable to find documentation or a sample program on using modbus RTU with microcontrollers.
 
Reply 0 0
kortenbach
Try to find a simple Arduino example and get it working using the Arduino IDE. Once you have a working demo transfer the code to the Arduino extension in OpenPLC. It should be able to run you Arduino example without major problems. Once that's running build your application from there. You may need to install some libraries using the command line version of the Arduino compiler that's included in OpenPLC.
Reply 0 0
thiagoralves
Sharing variables between the Arduino extension and the PLC side is very easy. Just declare the variables you want shared in the variables table on top of the Arduino extension code. Then on the PLC program side, declare the same variables with the same name and type, just with class “external”.
Reply 0 0
matthew7047
Thank you!
Reply 0 0
matthew7047
If using the PI Pico or RP2040-zero would I be able to use micro python code to replace the Arduino code?  
Reply 0 0
thiagoralves
No. Arduino Extension is C/C++ only
Reply 0 0
matthew7047
thank you again for the quick response.  
Reply 1 0
Reply