Author Topic: Receiving Modbus data from Arduino to Raspberry Pi  (Read 869 times)

mdhill

  • Newbie
  • *
  • Posts: 5
    • View Profile
Receiving Modbus data from Arduino to Raspberry Pi
« on: May 27, 2022, 10:36:41 AM »
Good morning,

I'm hoping someone within the community can assist me as I am almost out of hair to pull.  I have created a program with my Arduino Mega to send Modbus data to my Advanced HMI program.  This is to verify various harness' are connected to a motor and also monitor temp and humidity with the Arduino and raspberry Pi 4.  During the trial testing, everything works fine with Windows 10 but when I try using the Advanced HMI program using Mono in my Raspberry Pi (command: mono AdvancedHMI.exe) the displays appear but no data.  I have made changes in the ModbusRTUCom1 setting to use /dev/ttyACM0 as my Port name, and confirmed the baud rate (115200, 8, N, 1).  My PLC address range is from 40000 - 40022.

I have found on some forums that Mono can only read from TCP/IP and not from RTU via Serial.  Is this true and if so is there an alternative to using Mono in Raspberry Pi to run the Advanced HMI application?  I have also tried creating a program in python to act as a translator between the Arduino and the Pi but to no avail.  I hope there is a solution that someone has come across. 

Arduino Code:

#include "DEV_Config.h"
#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>
#include <SoftwareSerial.h>
#include <DFRobot_DHT11.h>
DFRobot_DHT11 DHT;

//All of the data accumulated will be stored here
modbusDevice regBank;

//Create the modbus slave protocol handler
modbusSlave slave;

// Analog inputs
const int analogPin1 = 0;
const int analogPin2 = 1;
const int analogPin3 = 2;
const int analogPin4 = 3;
const int analogPin5 = 4;
const int analogPin6 = 5;

// Digital outputs
const int outPin1 = 3;
const int outPin2 = 5;
const int outPin3 = 6;
const int outPin4 = 4;

// Temp sensor parameters for Arduino and Pi F & C
#define DHT11_PIN_A 28
#define DHT11_PIN_R 29
float TempF_Ard = 0;
float TempC_Ard = 0;
float TempF_Pi = 0;
float TempC_Pi = 0;
int CabFan;

void setup()
{
  //Assign the modbus device ID. 
  regBank.setId(1);
  /*
  modbus registers follow the following format
  00001-09999  Digital Outputs, A master device can read and write to these registers
  10001-19999  Digital Inputs, A master device can only read the values from these registers
  30001-39999  Analog Inputs, A master device can only read the values from these registers
  40001-49999  Analog Outputs, A master device can read and write to these registers
  Assign the modbus device object to the protocol handler
  This is where the protocol handler will look to read and write
  register data.  Currently, a modbus slave protocol handler may
  only have one device assigned to it.
  */
  slave._device = &regBank; 

  // Initialize the serial port for coms at 9600 baud 
  slave.setBaud(9600);
  delay(250); 
 
  //Add Analog Output registers 40001-40020 to the register bank
  regBank.add(40001); // Connector, Connect LED on Display
  regBank.add(40002); // Connector
  regBank.add(40003); // Connector
  regBank.add(40004); // Connector
  regBank.add(40005); // Connector (Not used for testing 
  regBank.add(40006); // Connector (Not used for testing
  regBank.add(40007); // Not Used
  regBank.add(40008); // Pass Display Indicator
  regBank.add(40009); // Fail Display Indicator
  regBank.add(40010); // Power LED on Display
  regBank.add(40011); // Arduino temp F
  regBank.add(40012); // Arduino humidity
  regBank.add(40013); // Arduino temp C
  regBank.add(40014); // Rasp Pi temp F
  regBank.add(40015); // Rasp Pi humidity
  regBank.add(40016); // Rasp Pi temp C
  regBank.add(40017); // Connector, Raw data
  regBank.add(40018); // Connector, Raw data
  regBank.add(40019); // Connector, Raw data
  regBank.add(40020); // Connector, Raw data
  regBank.add(40021); // Connector, Raw data
  regBank.add(40022); // Connector, Raw data

  System_Init();
  if(USE_IIC)
{
 // Serial.print("Only USE_SPI_4W, Please revise DEV_config.h !!!");
   return;
  Serial.begin(9600);
}
 // Digital Outputs for LED lights
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  // Digital Inputs for reading continunity
  pinMode(22, INPUT);
  pinMode(23, INPUT);
  pinMode(24, INPUT);
  pinMode(25, INPUT);
  pinMode(26, INPUT);
  pinMode(27, INPUT);
 
  digitalWrite(22, HIGH);
  digitalWrite(23, HIGH);
  digitalWrite(24, HIGH);
  digitalWrite(25, HIGH);
  digitalWrite(26, HIGH);
  digitalWrite(27, HIGH);
  digitalWrite(7, HIGH);
 
}

void loop()
{
  regBank.set(40001,1);  // Connector, Connect LED on Display
  regBank.set(40002,2);  // Connector
  regBank.set(40003,3);  // Connector
  regBank.set(40004,4);  // Connector
  regBank.set(40005,5);  // Connector (Not used. No direct connect)
  regBank.set(40006,6);  // Connector (Not used. No direct connect)
  regBank.set(40007,7);  // Not used. Spare
  regBank.set(40008,8);  // Pass Display Indicator
  regBank.set(40009,9);  // Fail Display Indicator
  regBank.set(40010,10); // Power On Display Indicator
  regBank.set(40011,11); // Arduino temp F
  regBank.set(40012,12); // Arduino humidity
  regBank.set(40013,13); // Arduino temp C
  regBank.set(40014,14); // Rasp Pi temp F
  regBank.set(40015,15); // Rasp Pi humidity
  regBank.set(40016,16); // Arduino temp C
  regBank.set(40017,17); // Connector, Raw Data
  regBank.set(40018,18); // Connector, Raw data
  regBank.set(40019,19); // Connector, Raw data
  regBank.set(40020,20); // Connector, Raw data
  regBank.set(40021,21); // Connector, Raw data
  regBank.set(40022,22); // Connector, Raw data
 
 while(1)
  {
    regBank.set(40010, 1);  // Power LED on Display
   
    // Temp and Humidity sensors for Arduino and Pi
    DHT.read(DHT11_PIN_A);
    TempF_Ard = ((DHT.temperature*1.8)+32); // Arduinio conversion from C to F  "This is a math formula (DHT.Temperature x 1.8+32) not emoji"
    TempC_Ard = DHT.temperature;            // Arduino Temp C
    regBank.set(40011, TempF_Ard);          // Arduino temp F
    regBank.set(40012, DHT.humidity);       // Arduino humidity
    regBank.set(40013, TempC_Ard);          // Arduino Temp C
    DHT.read(DHT11_PIN_R);
    TempF_Pi = ((DHT.temperature*1.8)+32);  // Rasp conversion from C to F  "This is a math formula DHT.Temperature x 1.8+32) not emoji"
    TempC_Pi = DHT.temperature;
    regBank.set(40014, TempF_Pi);           // Arduino temp F   
    regBank.set(40015, DHT.humidity);       // Arduino humidity
    regBank.set(40016, TempC_Pi);           // Arduino Temp C
     
     // C1YE01-EVC Connector, Connect LED on Display
     int reading1 = analogRead(analogPin1);
     float voltage1 = reading1 / 204.6;
     regBank.set(40017, voltage1);
     if (voltage1 < 1)
       {
        regBank.set(40001, 0);
       }
     else
       {
        regBank.set(40001, 1);
       }

     // C1YT04-EV Connector
     int reading2 = analogRead(analogPin2);
     float voltage2 = reading2 / 204.6;
     regBank.set(40018, voltage2);
     if (voltage2 < 1)
       { 
        regBank.set(40002, 0);
       }
     else
       {
        regBank.set(40002, 1);
       } 

     // C44-EV2 Connector
     int reading3 = analogRead(analogPin3);
     float voltage3 = reading3 / 204.6;
     regBank.set(40019, voltage3);
     if (voltage3 < 1)
       { 
        regBank.set(40003, 0);
       }
     else
       {
        regBank.set(40003, 1);
       }

     // C1YT17-EV  Connector
     int reading4 = analogRead(analogPin4);
     float voltage4 = reading4 / 204.6;
     regBank.set(40020, voltage4);
     if (voltage4 < 1)
       { 
        regBank.set(40004, 0);
       }
     else
       {
        regBank.set(40004, 1);
       }
   
    // Actuation of Pass or Fail relays
    if ((voltage1 > 4.7) && (voltage2 > 4.7) && (voltage3 > 4.7) && (voltage4 > 4.7)) // && (voltage5 > 4.7) && (voltage6 > 4.7))
      {
        digitalWrite(outPin1, LOW);  // Fail LED
        regBank.set(40009, 1);       // Fail Display Indicator
        digitalWrite(outPin2, LOW);  // Pass LED
        regBank.set(40008, 1);       // Pass Display Indicator
        digitalWrite(outPin3, HIGH); // Connect to Harness LED
      }
    else if ((voltage1 < 1) && (voltage2 < 1) && (voltage3 < 1) && (voltage4 < 1)) // && (voltage5 < 1) && (voltage6 < 1))
      {
        digitalWrite(outPin1, LOW);  // Fail LED
        regBank.set(40009, 1);       // Fail Display Indicator
        digitalWrite(outPin2, HIGH); // Pass LED
        regBank.set(40008, 0);       // Pass Display Indicator
        digitalWrite(outPin3, LOW);  // Connect to Harness LED
      }
    else
      {
        digitalWrite(outPin1, HIGH); // Fail LED
        regBank.set(40009, 0);       // Fail Display Indicator
        digitalWrite(outPin2, LOW);  // Pass LED
        regBank.set(40008, 1);       // Pass Display Indicator
        digitalWrite(outPin3, LOW);  // Connect to Harness LED
      }
    slave.run();   
   // Driver_Delay_ms(250);   
   }   
}

Thanks in advance.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Receiving Modbus data from Arduino to Raspberry Pi
« Reply #1 on: May 27, 2022, 12:56:29 PM »
Are you using Version 3.99y Beta that you can download from this forum? That version uses some modified serial drivers that have been modified to better support Linux.

mdhill

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Receiving Modbus data from Arduino to Raspberry Pi
« Reply #2 on: June 23, 2022, 10:40:21 AM »
Thank you for the reply.  I am now running 399yBeta 38 and the program is working well on the Pi.