arduino serial communication with HEX code. How to read HEX stream?

Hello forum,

I want to communicate with a serial glucose monitor device that runs with a 3.3V coin cell battery.

Purpose:

  1. send HEX (array) commands to the device
  2. receive the response(HEX array)
  3. Store the data on inbuilt EEPROM of arduino or transmit it via bluetooth or any wireless device.

Progress:
Successfully sent command to the gluco monitor (acknowledgement message “PC” shown on the glucometer screen).

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
bool x = 1; //to make sure the loop runs only once
int led = 13; //indication LED
void setup()
{

  Serial.begin(19200);   // Open serial communications and wait for port to open:
  mySerial.begin(9600);
  pinMode (led, OUTPUT);
  delay(500);
}

void loop() // run over and over
{
  if (x == 1) { //to make sure the loop runs only once
    digitalWrite(led, HIGH);
    byte message[] = {0x02, 0x12, 0x00, 0x05, 0x0B, 0x02, 0x00, 0x00, 0x00, 0x00, 0x84, 0x6A, 0xE8, 0x73, 0x00, 0x03, 0x9B, 0xEA, }; // HEX code to check device serial number
    mySerial.write(message, sizeof(message));
    digitalWrite(led, LOW);
    x = 0;
  }
  else;
}

the device’s response is clearly screen in its LCD. (“PC” is written in its LCD).

Problem:
The device is supposed to send an acknowledgement & information to the arduino in turn. I am stuck at this stage.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
bool x = 1; //to make sure the loop runs only once
int led = 13; //indication LED
void setup()
{

  Serial.begin(19200);   // Open serial communications and wait for port to open:
  mySerial.begin(9600);
  pinMode (led, OUTPUT);
  delay(500);
}

void loop() // run over and over
{
  if (x == 1) { //to make sure the loop runs only once
    digitalWrite(led, HIGH);
    byte message[] = {0x02, 0x12, 0x00, 0x05, 0x0B, 0x02, 0x00, 0x00, 0x00, 0x00, 0x84, 0x6A, 0xE8, 0x73, 0x00, 0x03, 0x9B, 0xEA, };
    mySerial.write(message, sizeof(message));
    while(!mySerial.available()); // wait for the serial port to senr data
    int incomingByte = mySerial.read();
    Serial.println(incomingByte,HEX);
    digitalWrite(led, LOW);
    x = 0;
  }
  else;
}

The output shows this

instead of this

 02 06 06 03 cd 41 02 11 02 05 06 47 58 47 34 34 35 44 45 52 03 be 25

Any thoughts on this issue shall be appreciated.
Thank you.

PS- I tried to sniff the serial port using serial monitor(communication between glucometer & the propitiatory software via desktop computer) & the information is enclosed in the attached .txt file.
All I am trying to do is to communicate with the device with arduino without using the proprietary software.

Sniff_Dump.txt.txt (32.1 KB)

    while(!mySerial.available()); // wait for the serial port to senr data
    int incomingByte = mySerial.read();
    Serial.println(incomingByte,HEX);

You are only displaying the first character you receive. I don’t know why it shows up as “:ø” and not 1 or 2 hexadecimal digits.

If you want to display the whole message you need a way to detect the end of the message. Are all messages exactly 23 characters?

    for (int i=0; i<23; i++) {
      while(!mySerial.available()); // wait for a character
    int incomingByte = mySerial.read();
    Serial.print(incomingByte,HEX);
    Serial.print(' ');
    }
    Serial.println();

If the messages are not always 23 characters, is the an End of Message marker for the information packet?

Thanks for the support. The program runs ok, but is there any way to terminate arrays of variable length?

/*
  Software serial multple serial test

 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.

 The circuit:
 * RX is digital pin 10 (connect to TX of other device)
 * TX is digital pin 11 (connect to RX of other device)
 */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
bool x = 1;
int led = 13;
void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(19200);
  mySerial.begin(9600);
  pinMode (led, OUTPUT);
  delay(500);
}

void loop() // run over and over
{
  if (x == 1) { //run the loop once
    digitalWrite(13, 1); //LED for debug
    byte message[] = {0x02, 0x09, 0x00, 0x05, 0x0D, 0x02, 0x03, 0xDA, 0x71};
    mySerial.write(message, sizeof(message));
    for (int i = 0; i < 40; i++) { 
      while (!mySerial.available()); // wait for a character

      int incomingByte = mySerial.read();
      Serial.print(incomingByte, HEX);
      Serial.print(' ');
    }
    digitalWrite(13, 0);
    Serial.println();
    digitalWrite(led, LOW);
    x = 0;
  }
}

The output of the reply message is 32 characters long, some of the replies from the device is 6 characters long. But the common thing is that every HEX message ends with 0x03 as the third last character. Any clue on the variable length arrays? How to terminate them?

I suggest you use one of the examples in serial input basics to receive the data and, as a first step, display it on the Serial Monitor. When you know it is receiving reliably you can consider parsing the data.

...R

But the common thing is that every HEX message ends with 0x03 as the third last character.

There is another on going discussion of similar nature concerning end of packet marker and such below. In my ascii table hex 03 is an end of text marker. The other two trailing hex bytes might be a check sum.

http://forum.arduino.cc/index.php?topic=343806.0