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?
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.
00000000 00000000 00000000 00000001 A
00000000 00000000 00000000 00000010 B
01000000 01000000 00000000 00000000 S
01000000 10000000 00000000 00000000 D
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:
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.
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.