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);
}