Reputation: 950
I'm programming with arduino 1.0.5 in lunix/windows
With this piece of code:
void readSerialString () {
char buffer[8];
if(Serial.available()) {
while (Serial.available()){
sb = Serial.read();
buffer[indexB] = sb;
indexB++;
}
}
Serial.println(buffer);
}
I'm trying to send (by serial terminal) a message that can be seen in hexadecimal.
For exemple if I write: "\xaa\x22\xa1" It will not read as hexadecimal, will it?
How can I let the program read the string in input as hexadecimal?
Upvotes: 1
Views: 4592
Reputation: 5874
No your mistaking the data and it s format.
do you have access to printf ? If so use printf("%x",char)
to see a char as hexadecimal.
Arduino solution Serial.print(78, HEX) gives "4E"
see http://arduino.cc/en/Serial/Print
[edit]
I need the contrary of print. I need that the string that has been token from the serial terminal is interpreted as hex.
To do this use read(), but you will have to implement the convertion function from ascii HEX to data, as an HEX data for a byte hols on 2 chars, my function take two chars as input)
char hex_ascii_to_databyte(char c1, char c2){
char res=0;
if(c1>=48 && c1<=57) res = c1-48;
else if(c1>=65&& c1<=70) res = c1 - 65 + 0xa;
else if(c1>=97&& c1<=102) res = c1 - 97 + 0xa;
else{//error
}
//idem c2 in res2
res=res<<4;
res+=res2;
return res;
}
for each hex read, call twice read (to read the 2 ascii chars) then call this func
Upvotes: 1