Go Down

Topic: Need Help with Reading Modbus Output using Simple Modbus Master (Read 526 times) previous topic - next topic

AlvaroAmos1

Hi guys,

I'm currently working on a project involving reading data from a Schneider Acti 9 Power Meter and sending the data through Arduino to the internet. I'm having a problem with reading data from the power meter to the Arduino. The main library I'm using is the Simple Modbus Master library. The code I have right now is as the following. The output of this code, for some reason, is 769.



#include <SimpleModbusMaster.h>
#include <SoftwareSerial.h>

//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate
#define retry_count 10

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2

#define LED 13
#define RX 8
#define TX 7

// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 1

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
 PACKET1,
 //PACKET2,
 TOTAL_NO_OF_PACKETS // leave this last entry
};

SoftwareSerial Modbus(RX,TX);

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];

void setup()
{
 // Initialize each packet
 modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 1845, 2, 0);
 //modbus_construct(&packets[PACKET2], 1, PRESET_MULTIPLE_REGISTERS, 1, 1, 0);
 
 // Initialize the Modbus Finite State Machine
 modbus_configure(&Serial, baud, SERIAL_8N1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
 
 pinMode(LED, OUTPUT);
}

void loop()
{
 modbus_update();
 long temp = (long)regs[0]<<16|regs[1]; //?
 Serial.println(readRegs[0]);  //?
}



I'm still very new to Modbus. I've haven't fully understood it. Please help :smiley-confuse:

pylon

Quote
The main library I'm using is the Simple Modbus Master library.
Post a link to it, there are several different version of libraries with that name on the Internet.

Quote
I'm currently working on a project involving reading data from a Schneider Acti 9 Power Meter and sending the data through Arduino to the internet.
This is your second thread about the connection to that device and you still failed to provide a link to it's manual!

Code: [Select]
modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 1845, 2, 0);

It's a bad idea to request two registers if you provide only space for one of them.

Quote
The output of this code, for some reason, is 769.
It outputs the content of register 1845. The register seems to contain 769. Why are your surprised?

AlvaroAmos1

Here's the link to the library I'm using.
I'm using the SimpleModbusMasterV2rev2 files.

I just found the manual for the device. I attached it to this reply.

Code: [Select]
modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 1845, 2, 0);

It's a bad idea to request two registers if you provide only space for one of them.
I don't really understand this part. How do I add more space so that I can read more than 1 register in each packet?

It outputs the content of register 1845. The register seems to contain 769. Why are your surprised?
The problem is that I already tried to change the register address to different registers, but the output is always the same: 769.

pylon

Quote
I don't really understand this part. How do I add more space so that I can read more than 1 register in each packet?
Like that:

Code: [Select]
// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 2


Quote
I just found the manual for the device. I attached it to this reply.
That's not a manual, that's a fact sheet. The manual would declare which register holds what value. Where did you get the register number 1845 from?

AlvaroAmos1

Like that:

Code: [Select]
// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 2


Oh, I see.

That's not a manual, that's a fact sheet. The manual would declare which register holds what value. Where did you get the register number 1845 from?
Ohh, thanks for pointing that out. Here's the Modbus register manual. Here's the rest of the manual.

pylon

The Google Photos link doesn't work for me. If it's an image file, please post it directly to the forum as external links tend to get unpopulated quite soon.

AlvaroAmos1

This should work. . Just in case it doesn't, I've also attached it

pylon

According to that sheet register 1845 holds the year. This isn't a value that I would expect to change that much. If you never adjusted the value a year 2001 seems to make sense to me. So again: why are you surprised to read that value?

If you used other registers, which one did you try?

AlvaroAmos1

Actually, I've already changed the year to match this year, 2019. That's why I'm surprised to read that the value is 769. Also, I've tried reading other registers, specifically register 3000 and still it  gave the value 769.

Update:
I've tried to change the number of registers, reading the same register, 1845, but instead of the usual 769, the serial monitor displays 0.

pylon

Quote
I've tried to change the number of registers, reading the same register, 1845, but instead of the usual 769, the serial monitor displays 0.
I get the impression that your hardware wiring is not correct. Post a complete wiring diagram!

AlvaroAmos1

This is my configuration. I connect the power line and load of the power meter according to the configuration in the manual. The RS485 communication port in the power meter is then connected to the MAX485 chip. The serial output of the MAX485 chip I connect to the Arduino according to this format:

RO -> RX
DO -> TX
DE  -> pin 3
RE -> pin 2

pylon

I completely overlooked that:

Code: [Select]
void loop()
{
 modbus_update();
 long temp = (long)regs[0]<<16|regs[1]; //?
 Serial.println(readRegs[0]);  //?
}


You must not use the Serial interface for debugging output while it's used for the Modbus connection! Everything you print will be sent to the Modbus and there the bytes make no sense.

Quote
DO -> TX
I doubt the pin is called DO.

Quote
DE  -> pin 3
RE -> pin 2
Both (DE and RE) are expected to be connected to pin 2 (at least that's in your code).

What is that setup expected to do in the end? Is the UNO used to control anything or just as a forwarder of data from or to the PC?

Go Up