Go Down

Topic: Help with ModBus RTU Master-Slave: SimpleModbus [SOLVED] (Read 201461 times) previous topic - next topic


On the Leo you can only use the Serial1 object. Although you have done this you cannot access the Serial1 object methods as this will conflict with the library. The information should be stored in the array you initialized the packet to, in this case readRegs[4]. This array contains the unsigned int values read from the slave. Since modbus only specifies unit16 values you will have to read 2 registers per float value. You will need to shift the two consecutive unsigned integers together to form the float32 value. You can then use Serial (USB) to transmit the two registers to the PC.


So the  readRegs[4] contains the 4 words read from the slave.
readRegs[0] 1st word
readRegs[1] 2nd word
readRegs[2] 3rd word
readRegs[3] 4th word

Actually i want to read the registers and store the data on SD Card.
Arduino will read the data periodically and update the file by over writing the entries in the file.
The file will act as a sort of database.
As of now just for debugging i am printing the output to Serial1.
Gabriel Jeremy Jeldo


Yes, readRegs[] contains the requested registers information.

According to the arduino product page you must use Serial for USB transmission and Serial1 for UART (modbus) on the Leo.



I tried with the schematics from simple modbus. Didn't Work.

Tried a breakout board for RS-485. Didn't work.

Serial port hangs never enters the if condition where it checks if hardware serial is available using breakout board.

Reading output of hardware serial. Giving junk values without even connecting wires.

Please help. Thanks
Gabriel Jeremy Jeldo


Hi meetjeremy,

Again, you can not access the Serial1's objects methods directly. You must transmit the contents of readRegs[] using Serial.


If you see the third attachment in my previous post. I have done that.

Still no output  :(
Gabriel Jeremy Jeldo


The third attachment still uses Serail1.available() and the other one uses Serial1.read(). This wont work.

Use Serial.println(readRegs[]) or something similar like you already did to transmit the received values from Serial1 (the modbus connection). Again you cannot use Serial1.available() since it will most of the time be empty depending when you read it. The library reads the received information and stores it in the assigned register array, in this case readRegs[]. This is the only way of interfacing with the information received.

If you want to know if the request packet is successful than access the packets following information counters like this:

Also try not to use delay() with values larger than 100ms. It will affect the polling and timeout values.

If you want to periodically send the received information from readRegs[] to Serial (USB) than rather use millis() in a timely manner or check if the value linked to the regdRegs[] index has changed.


Aug 27, 2013, 11:18 am Last Edit: Aug 27, 2013, 11:23 am by meetjeremy Reason: 1
Hello JuanB,

Thanks for the reply.

I printed this
Code: [Select]
void loop()
  //if (Serial1.available()){
    /// Write the received data from Serial 1 to Serial
  analogWrite(LED, readRegs[0]>>2);

but i'm getting


that is requests made 50 but all failed requests.

Even the comm. light on the meter is not blinking.
Gabriel Jeremy Jeldo


Hi meetjeremy, since the information is scattered could you please mail me your complete sketch to bester.juan@gmail.com.

Maybe I have asked this but did you connect the driver ic (or break out board) according to the RS485 connection schematic on the SimpleModbus site?

What arduino are you using?


Hello JaunB,

Thanks for the reply.

I'm using Leonardo for this project.

Yes i have connected the circuit according to the schematics.

I have sent you the code in your mail.
Gabriel Jeremy Jeldo


Hi Jeremy,

Assuming that you are referring to the ElMeasure meter LG119:

Try using the address 159 directly as some master implementations include an offset automatically.


Hello JuanB,

i tried reading for register 40159 and tried 159 also. It didn't read. I read it via rs485 usb converter also it didn't send anything. So now i'm not reading from Elmeasure.


I tried the same with Fineco (China) meter also and the readReg[] gives different values. The communication led on the meter is blinking. And when addressed on a Daisy chain only that particular meter is read. But the values are not proper.

Code: [Select]

    //Serial.println(readRegs[0]);  0
    //Serial.println(readRegs[1]);  0
    //Serial.println(readRegs[2]);  12801
    //Serial.println(readRegs[3]);  2
    //Serial.println(readRegs[4]);  259
    //Serial.println(readRegs[5]);  29
    //Serial.println(readRegs[5]);  21762
    //Serial.println(readRegs[6]);  241
    //Serial.println(readRegs[7]);  0
    //Serial.println(readRegs[8]);  0
    //Serial.println(readRegs[9]);  0

Another thing.. If i remove the 10k resistor then the communication light blinks for every 1 second properly. Else if i have the 10k resistor then the light blinks very fast continuously with very less gap.

Best thing i'm now able to see the communication light blinking, but now i don't know how to get the proper data.
Gabriel Jeremy Jeldo


Hi jeremy,

This is strange indeed, although if the code is so short and depending on the response of the slave than the led flash rate should theoretically be very quick not 1 second intervals as you have described. If I connect an led (with 220ohm resistor)on the TxRxEnable output the led will flash according to the polling rate of 200ms which is editable in the code. So rather leave the 10k in place as the 1 second interval you are perceiving is most likely the timeout of 1000ms expiring due to false reception.

Your statement: "I read it via rs485 usb converter also it didn't send anything." Are you referring that you have used another modbus implementation via an USB-RS485 converter?

Maybe the ElMeasure meter LG119 is dead?


Mail me the code for the Fineco (China) meter and the complete data sheet so I can have a look at the address space.


Hello JuanB,

Ya i think Elmeasure is dead. Let me check with other modbus readers.

Let me try to connect a 220ohm resistor wit led at txrxenable pin.

Ok i'll mail you the data sheet.

Gabriel Jeremy Jeldo


Hello JaunB,

I tried the LED. Yes its flashing for every 200 ms.
Gabriel Jeremy Jeldo

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131