Header Datalength with Arduino

Asked

Viewed 76 times

1

I need help sending a HEADER, produced in Arduino (C++), and sent to a server Zabbix.

Header documentation

https://www.zabbix.com/documentation/devel/manual/appendix/protocols/header_datalen

My Code in C++

Serial.println(json);
             int tamanho = json.length();

             String pacote = "";
             pacote = "ZBXD\1" ;
             String stringtres = String(pacote, BIN);
             String stringOne = String(, BIN);

             String separador = "\0\0\0\0";

             pacote += stringOne;
             pacote += separador;
             pacote += json;
             String stringtwo = String(pacote.length(), BIN);
             Serial.println(stringtwo);


           //Aqui conecto no servidor zabbix porta 10051
            if(client.connect(server, porta)){

              Serial.println(pacote);
              client.print(pacote);

              contador++;
              delay(5000);
     }

Example of how the code should be done in JAVA

byte[] header = new byte[] {
    'Z', 'B', 'X', 'D', '\1',
    (byte)(data.length & 0xFF),
    (byte)((data.length >> 8) & 0xFF),
    (byte)((data.length >> 16) & 0xFF),
    (byte)((data.length >> 24) & 0xFF),
    '\0', '\0', '\0', '\0'};

byte[] packet = new byte[header.length + data.length];
System.arraycopy(header, 0, packet, 0, header.length);
System.arraycopy(data, 0, packet, header.length, data.length);

I think I’m making this headline wrong, if anyone can help me I’m grateful!

1 answer

0

The implementation of this C header is very similar to that of Java. You will need to create a char array with the length of 13 characters, the first 4 being the characters 'Z', 'B', 'X', ’D', a control character 0x01, 4 characters to represent a number of 32 bits of length of the data to be transmitted, 4 empty characters in case 0x00, 0x00, 0x00, 0x00.

Example:

char header[13] = {
    'Z', 'B', 'X', 'D', 0x01,         
    (strlen(data) & 0xFF),
    ((strlen(data) >> 8) & 0xFF),
    ((strlen(data) >> 16) & 0xFF),
    ((strlen(data) >> 24) & 0xFF),
    0x00, 0x00, 0x00, 0x00 };
  • 1

    Correct I managed to implement Header, you know how I do this concatenation, I tried to do here but without success. code 
byte[] packet = new byte[header.length + data.length];

System.arraycopy(header, 0, packet, 0, header.length);

System.arraycopy(data, 0, packet, header.length, data.length);


  • You will need to Instantiate an array of char called packet with the length of [len(data) + len(header)] and use the method memcpy to copy into packet the array header and data.

  • If you accept the answer, the Zabbix header will work better and better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.