Binary data output produces junk

I cannot find a way of writing data in binary that works. I am not trying to do anything complicated just write out 4 numbers. This is simplified sample of my code:

#include <Arduino.h>
#define SQUARK 1000  //The Squark rate must be much bigger than Update rate

struct myData {
  long A;
  long B;
  double S;
  double D;
};

void setup() {
  //Start the serial
  Serial.begin(9600);
}

void loop() {

  Printit();
  delay(SQUARK);
}

void Printit() {

  struct myData Data;

  Data.A = 1;    //countA;
  Data.B = 2;    //countB;
  Data.S = 3.0;  //Speed;
  Data.D = 4.0;  //countA*Cal;

  Serial.write((char *) &Data, sizeof(Data));
}

I log the data in Tera Term and the display it in Hex Editor (neither if which I know how to use properly) and all I get is junk. Can anyone tell me what's going on?

How are you expecting it to be displayed ?

Sure, you are writing data in binary. I don't have Hexeditor, but in my editor is looks like this

Which is what I would expect to see.

The first 4 bytes "01 00 00 00" are the value of Data.A (1). AVR stores data LSB, so the bytes are "backwards". As a value it reads 0x00000001.

Do these values print any better?

  Data.A = 0x0A313030;    //countA;
  Data.B = 0x0A323030;    //countB;
  Data.S = 1.31545142219689917925e-259;  //Speed;
  Data.D = 1.31545266271702834126e-259;  //countA*Cal;

Some of those won't work on platforms like AVR. Here's one clue and another to how it works.

You need to use print instead of write. And you can't do the whole struct in a single call: in order to work, the bytes must be interpreted as the proper type, to call the appropriate overload of the print method.

output

 00000000 00000000 00000000 00000001  A
 00000000 00000000 00000000 00000010  B
 01000000 01000000 00000000 00000000  S
 01000000 10000000 00000000 00000000  D
struct myData {
    long   A;
    long   B;
    double S;
    double D;
}
data = { 1, 2, 3.0, 4.0 };

// -----------------------------------------------------------------------------
void prBin (
    byte        *p,
    int         nByte,
    const char *lbl )
{
    for (int n = nByte-1; n >= 0; n--) {
        Serial.print (" ");
        for (int i = 7; i >= 0; i--)
            Serial.print ((p [n] >> i) & 1);
    }

    Serial.print   ("  ");
    Serial.println (lbl);
}

void setup() {
    Serial.begin(9600);
    Serial.println ();

    prBin ((byte*) & data.A, sizeof(data.A), "A");
    prBin ((byte*) & data.B, sizeof(data.B), "B");
    prBin ((byte*) & data.S, sizeof(data.S), "S");
    prBin ((byte*) & data.D, sizeof(data.D), "D");
}

void loop () { }
1 Like

I was expecting something like post 3.

Yes, this is about what i was expecting. What i get is lots of blanks, and random charaters like +@£%÷=

because you're printing the binary value, not an ASCII 0 or 1

1 Like

Sorry, I cant follow this.

I can sort of follow this but cant understand why i would need to do it. Are you suggesting that the write method does not work and i need to make my own?

the serial monitor displays ASCII characters. not all ASCII characters are printable such as escape, linefeed or carrage return and ASCII characters are limited to 7-bit.

it displays odd symbols for these non-ASCII character values if use write() instead of print().

i provided code to extract the individual bits of the variables and print either a '1' or '0'

I logged the data in Teraterm using File |Log and setting mode to binary. Then I opened the file teraterm.log in PsPad in hex mode, and set column size to 1 byte.

I did also find a macro to put Teraterm into debug mode so it displays hex:

app_name = "Debug Mode Selector"

strdim options 4
options[0] = "Off (normal)"
options[1] = "How non-printable characters"
options[2] = "All bytes as hex"
options[3] = "Disable output completely"

listbox "Select Debug Mode" app_name options
debug_mode = result

setdebug debug_mode

Here's you data displayed in CoolTerm's HEX format, exactly as expected. Just change the view to Hex, no need to log to a file, it prints the Hex representation directly.

1 Like

Thanks everyone for your help. It looks like it is working, its just my poor understanding of what I am actually writing, and how to view it that is causing the problem.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.