Modbus_Arduino, Sometimes can read arduino sensor with Modbus,sometimes cannot

Hi guys,I’m just new to this part, please forgive me

As the title mentioned above, my project is using some python code to read the value which is stored in the Modbus’s register(CP2102)

I am using a sensor(waterflow sensor) and calculate the output in the arduino and save the output to arduino EEPROM and Modbus’s register

After that, i will use Pycharm and some python code to read the register value.

I use 2 arduino uno board , that mean 2 sensor work together and save the output to Modbus’s register 30001 with unit=1 and unit=2 (different unit address)

But the problem is, Sometimes can read both output(arduinoboard1, arduinoboard2) together, and sometimes only can read on of them, and at last both output cannot read…

But if I press the reset button of the arduino board or replugin the usb connection between arduino and computer,then I can read it again~

If there have any solutions about this problems? Looking for your suggestion .

Thanks

from pymodbus.client.sync import ModbusSerialClient as ModbusClient
import time

scale = ModbusClient(method='rtu', port='/dev/ttyUSB0', parity='N', baudrate=9600, bytesize=8, stopbits=2, timeout=1)
connection = scale.connect()
count = 0

while connection:
    
    count = count + 1
    print(count)

    try:
        a = scale.read_input_registers(0, 1, unit=1)
        print(a.registers)
        time.sleep(0.1)
    except:
        print("Unit1 occur error")
        print(a)
        scale.close()
        scale.connect()

    try:
        b = scale.read_input_registers(0, 1, unit=2)
        print(b.registers)
        time.sleep(0.1)
    except:
        print("Unit2 occur error")
        print(b)
        scale.close()
        scale.connect()

    time.sleep(1)
#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>
#include <EEPROM.h>
#include <EEPROMAnything.h>

#define RS485TxEnablePin 8
#define RS485Baud 9600
#define RS485Format SERIAL_8N2
modbusDevice regBank;
modbusSlave slave;

byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin       = 2;
//EEPROM
int addr = 0;

float calibrationFactor = 4.5;
volatile byte pulseCount;  
float flowRate;
float flowMilliLitres;
float totalMilliLitres;
float oldTime;

struct config_t
{
  float total_Mil;
} data;


//######################################
void setup()
{   

  Serial.begin(9600);
  
  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0.0;
  totalMilliLitres  = 0.0;
  oldTime           = 0;

  EEPROM_readAnything(addr,data);
  totalMilliLitres = data.total_Mil; 

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  
  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 

Analog values are 16 bit unsigned words stored with a range of 0-32767
Digital values are stored as bytes, a zero value is OFF and any nonzer value is ON
*/

//Add Analog Input registers to the register bank
  regBank.add(30001);   
  
//Add Analog Output registers to the register bank
  regBank.add(40001);  

  slave._device = &regBank;  
  slave.setBaud(&Serial,RS485Baud,RS485Format,RS485TxEnablePin); 

  setP32( ); // Prescaler = 32
  Serial.flush();
  
}

void loop()
{
  if((millis() - oldTime) > 2000)    // Only process counters once per second
  { 
    detachInterrupt(sensorInterrupt);
        
    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    
    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();
    
    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    flowMilliLitres = (flowRate / 60) * 1000 / 1.378;
    
    //1.38 is Calibration Factor_2 to get the correct output of the Luquid quantity
    
    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;

    // Print the flow rate for this second in litres / minute
    Serial.print("\t\t");       // Print tab space
    Serial.print("Flow rate: ");
    Serial.print(flowRate);  // Print the integer part of the variable
    Serial.print("L/min");
    Serial.print("\t");       // Print tab space

    // Print the cumulative total of litres flowed since starting
    Serial.print("Output Liquid flowMilliLitres: ");        
    Serial.print(flowMilliLitres);
    Serial.print("mL\t"); 
    
    Serial.print("Output Liquid totalMilliLitres: ");        
    Serial.print(totalMilliLitres);
    Serial.println("mL");
    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;
    
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
  data.total_Mil = totalMilliLitres;
  
  EEPROM_writeAnything(addr,data);

  regBank.set(30001, data.total_Mil); //from 0 - 1023

  slave.run();  
  delay(10);
}

void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

void setP32( ) {
  ADCSRA |=  (1 << ADPS2);   // 1
  ADCSRA &=  ~(1 << ADPS1);  // 0
  ADCSRA |=  (1 << ADPS0);   // 1
}
  slave.setBaud(&Serial,RS485Baud,RS485Format,RS485TxEnablePin);

but

    // Print the flow rate for this second in litres / minute
    Serial.print("\t\t");       // Print tab space
    Serial.print("Flow rate: ");
    Serial.print(flowRate);  // Print the integer part of the variable
    Serial.print("L/min");
    Serial.print("\t");       // Print tab space

Do you see the problem? If you define the serial interface to be a Modbus you must not use it otherwise (e.g. to print any debugging values).