2

I'm trying to send heximal values from arduino to PZEM-04 power module (it shows voltage, current and ect.) over serial communication in heximal format and get values in decimal format for further actions with them. Any help with the code please? One of the commands: Send command: B4 C0 A8 01 01 00 1E Reply data: A4 00 00 00 00 00 A4

Edit: I am connecting wires from PZEM module TTL to arduino "serial port": TX-RX, RX-TX, VCC-VCC, GND-GND, so it's serial communication.. PZEM module: http://www.aliexpress.com/store/product/PEACEFAIR-AC-100A-Electric-power-monitoring-and-communication-module-power-meter-power-energy-Volt-Ammeter/1773456_32373508101.html

4
  • Please add link to communication format/protocol. Commented Mar 4, 2016 at 17:36
  • I just added more information.
    – Eimis
    Commented Mar 4, 2016 at 18:01
  • What code have you tried?
    – uint128_t
    Commented Mar 4, 2016 at 18:12
  • That for the extra info. I read this differently. You should send binary data with the hexadecimal values. Commented Mar 4, 2016 at 18:41

1 Answer 1

2

A possible implementation of the protocol using serial read/write of binary data (command and response).

void send(Stream& ios, uint8_t cmd[6])
{
  uint8_t sum = 0;
  for (int i = 0; i < 6; i++) {
    uint8_t c = cmd[i];
    sum += c;
    ios.print((char) c);
  }
  ios.print((char) sum);
}

bool recv(Stream& ios, uint8_t res[6])
{
  uint8_t sum = 0;
  uint8_t c;
  for (int i = 0; i < 6; i++) {
    while (!ios.available());
    c = ios.read();
    sum += c;
    res[i] = c;
  }
  while (!ios.available());
  c = ios.read();
  return (sum == c);
}

void setup()
{
  Serial.begin(57600);
  while (!Serial);

  uint8_t cmd[] = { 0xB4, 0xC0, 0xA8, 0x01, 0x01, 0x00 };
  uint8_t res[6];
  send(Serial, cmd);
  recv(Serial, res);
}

void loop()
{
}

Cheers!

1
  • Thank you very much Mikael Patel, you cleared it a lot to me. Now it's only needed to analize code, and insert other commands. After that I'm going to connect bluetooth module and send values to smartphone somehow, haha. Thanks again!
    – Eimis
    Commented Mar 6, 2016 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.