Page 1 of 1

Read energy meter with Z-uno shield via modbus

Posted: 05 Sep 2020 12:43
by kivi
Hi all,
I need to read data from an energy meter via modbus with Z-Uno shield. I can't find any useful examples for a sketch, I tried made a one, but it isn' working, maybe no answer from meter. Wired to shield's 7-8 pin, jumpers set to RS485. My sketch is below (reads voltage only), is anyone a good idea or a solution? Thanks.

Code: Select all

// Global variables

int Buffer[9];
byte x;
int VoltReadCommand[] ={0x08, 0x04, 0x00, 0x00, 0x00, 0x02, 0x71, 0x52}; // energy meter is no. 008 on modbus, voltage is in 00 00 register

ZUNO_SETUP_CHANNELS(
  ZUNO_METER(ZUNO_METER_TYPE_ELECTRIC, METER_RESET_DISABLE, ZUNO_METER_ELECTRIC_SCALE_VOLTS, METER_SIZE_FOUR_BYTES, METER_PRECISION_TWO_DECIMALS, getterVOLTS, NULL)
);


void setup() {
  Serial1.begin(9600);
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
}

void loop() {
 delay(20);
 
 // Set pin to HIGH > Transmit mode
 digitalWrite(2, HIGH); 
 delay(5);
 // Send command to ModBus Meter
for (int i=0; i < 8; i++) Serial1.write(VoltReadCommand[i]);
 delay(5);

// Set pin to LOW > Receive mode
 digitalWrite(2, LOW);
 delay(5);

// Read reply in Buffer[];
for (int i=0; i < 9; i++) Buffer[i] = Serial1.read();  

// Convert to Float

 ((byte*)&x)[3]= Buffer[3];
 ((byte*)&x)[2]= Buffer[4];
 ((byte*)&x)[1]= Buffer[5];
 ((byte*)&x)[0]= Buffer[6];

 delay(1500);
}

byte getterVOLTS() {
 return (x);
}

Re: Read energy meter with Z-uno shield via modbus

Posted: 05 Sep 2020 12:47
by PoltoS
Your approach is correct I general except that you allocate one byte for x and write 3 bytes behind it. This results in corruption.

Also getter should be dword (and x too) since you define four bytes meter value

Re: Read energy meter with Z-uno shield via modbus

Posted: 05 Sep 2020 12:53
by kivi
Thanks, but should be an other bug too, it is reporting now -1.00V.

Re: Read energy meter with Z-uno shield via modbus

Posted: 05 Sep 2020 12:55
by PoltoS
Put a static number in getter and check the getter is defined correctly.

Then revert back to x and make sure x is correct after reading

Re: Read energy meter with Z-uno shield via modbus

Posted: 05 Sep 2020 13:00
by kivi
Hmm, If i put: return (55.33); in the controller says 0.55 V
or if x = x*100;
return (x);
result is -1.00V
Interesting.

Re: Read energy meter with Z-uno shield via modbus

Posted: 05 Sep 2020 15:34
by kivi
The bug is in the modbus communication, Serial1.available() > 0 is false when it wants to write or read.

Re: Read energy meter with Z-uno shield via modbus

Posted: 11 Nov 2020 07:23
by zwaFisher
I would appreciate if you post your fina sketch.
Cheets

Re: Read energy meter with Z-uno shield via modbus

Posted: 11 Nov 2020 13:20
by kivi
Hi, not working yet. I am waiting for arrive a RS485 module for Arduino, that I can test without z-uno.

Re: Read energy meter with Z-uno shield via modbus

Posted: 16 Jan 2021 15:54
by zwaFisher
Has it arrived?

Re: Read energy meter with Z-uno shield via modbus

Posted: 05 Feb 2021 00:57
by kivi
zwaFisher wrote:
16 Jan 2021 15:54
Has it arrived?
Yes, but unfortunately I have no time to play with it... :?