Pages: [1]   Go Down
Author Topic: Binary serial communication  (Read 2270 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a Diecimila and serial communication using ASCII is working fine. But string manipulation isn't really what the Arduino should be doing so I was wondering if there is a tutorial somewhere showing binary serial communication? I.e. with int and float values in binary format, not ASCII.

Which binary number representations does the Arduino use? I think the program (written in C++) on the PC side could construct the binary representation before sending it over the serial port so that the Arduino did not have to do any reformatting.

Has this been done?
Logged

Baja California and California
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Tururu!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well the only way i know to send raw bytes is doing this:

Serial.print(byte(0xFF)); //Where 0xFF is the byte in hex you want to send

or

byte Variable=0x00; //declaring a byte


Serial.print(Variable); //Print it... i Don't remember if arduino chage it, so in case of yes just put the Serial.print(byte(Variable));
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Well the only way i know to send raw bytes is doing this:
Serial.print(byte(0xFF)); //Where 0xFF is the byte in hex you want to send

Ok, thanks that will be useful. I was initially thinking more about receiving binary data from the PC however, so will the serial read allow raw bytes too? And the question regarding binary representation of floats on the Arduino remains, since the PC will have to construct them properly if binary floats are to be sent over.
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Apologies if this comes across as unkind. Truely: Not meant that way!

Across any serial (or other digital!) communication channel, the data passed is always really only numbers, i.e. "binary".

It is how the sending program selects which numbers to send, and how the receiving program uses them that determines whether you are "sending" or "receiving" raw numbers or ASCII or jpegs or characters or whatever.

If you want to send "five" from one computer to another, you can either send.....

00000101  (5 in binary)
or
011 0101 (the binary for the character "5"... the 0101 in the lower nibble is almost a coincidence)
or even....
110 0110, 110 1001, 111 0110, 110 0101 (The Ascii for "f" "i" "v" "e")

... and if you want to send messages that others can't eavesdrop on, you use a code that no one else is using.

===
Most programming languages (including the Arduino's) have built in things to help programmers with the converting of things to what goes across the comm channel, hence the confusion over what is being sent.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Apologies if this comes across as unkind. Truely: Not meant that way!
.

Thank you for answering and for your good intent. However, I think you don't quite understand what I mean  smiley-wink. Of course, everything on the computer is binary, including ASCII text. That is not the issue.

When computing with numbers, the native number representation is something else than ASCII. I would like to know what the native floating point representation is, is it for example IEEE 754  http://en.wikipedia.org/wiki/IEEE_floating-point_standard or something else? As the original question states, I think it would be a good idea if such numbers could be transported over the serial port after being prepared to the relevant native format for the Arduino on the PC. That way no text to binary conversion would be needed on the Arduino.


Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to know what the native floating point representation is, is it for example IEEE 754

I just discovered that another thread on this forum seems to confirm IEEE 754
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207242838

So I guess the union method might work....  smiley
Logged

Central Florida, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know what you are saying / asking for. If you do

Code:
Serial.print( 5 );

What arrives at the terminal is the ASCII code for "5", which is a byte value of, like 53, if I recall. The print function converts binary numbers to ASCII strings. I assume that is because it is called "print" not "SendThisByte"
But...You want the byte value of 5.


Do this:
Code:
Serial.print( 5, BYTE );

That'll do ya.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I know what you are saying / asking for. If you do
Do this:
Code:
Serial.print( 5, BYTE );

That'll do ya.

Thank you, I will have a look at this option. Combined with the union technique it may solve the problem.
Logged

Pages: [1]   Go Up
Jump to: