Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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!

share|improve this answer
    
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 Mar 6 at 21:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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