0

I am trying to send packets of via bluetooth data that include 6 bytes read from a sensor and stored in a uint8_t array. Instead of sending over the data as numerical digital, I would like to send the ASCII character of each of the bytes. I am doing this to reduce the size of my data packets and make it easier to interpret the incoming data.

uint8_t* temp = readBufferData(FIFO);

for (int i = 0; i < 6; i++)
{
    Serial.write(temp[i]);

}

Read buffer data returns a pointer to a uint8_t array read from a FIFO buffer. When I use Serial.print, I get the following output, which makes sense. 22163226094255 One question is, when I use Serial.print on a single character, is it transmitted a single byte, or is it actually sending the decimal digital of the number, anywhere from 1-3 bytes?

When I use the Serial.write, however, I get something like the following DÿÂ?ÁÏ?ÌÆ?Ûµ?˽?ÔÀ?п?ÃÕÓÑËÐTÿÀ?Ï3ÿÉ?¹4ÿÐ?Ê<ÿ½?µfÿ¼?ìNÿÐ?×Â?ÊÚ?׿?Å Also, these data display much more slowly on the screen, than normal.

How can I send each uint8_t value by sending over the corresponding ASCII character for serial transmission?

Thanks!

4
  • 1
    try with this in the loop: Serial.print("Hex: 0x"); Serial.print(temp[i],HEX); Serial.print(" - Char: "); Serial.write(temp[i]); Serial.println(""); and then post some of the lines you get
    – frarugi87
    Apr 19, 2016 at 12:36
  • You could look at base64 encoding the data if you want to go hardcore, there are libraries out there. If you just want something you can read then the best solution is to convert each uint8_t to two hex numbers and send them, it will double the amount of data you transmit but I don't know if that's important to you. May 19, 2016 at 12:21
  • Why do you want to send your data as ASCII? Binary writes are much faster and of a vastly smaller size.
    – RubberDuck
    Aug 18, 2016 at 17:08

2 Answers 2

2

You don't "convert" anything. A character is just a human representation of an 8-bit value.

Instead you just need to change what the compiler thinks the data represents (from a human perspective). The simplest way of doing that is to either cast the uint8_t values to be char values, or to write each one individually as a raw value.

The latter way:

myBluetoothSerial.write(myData[0]);
myBluetoothSerial.write(myData[1]);
myBluetoothSerial.write(myData[2]);
myBluetoothSerial.write(myData[3]);
myBluetoothSerial.write(myData[4]);
myBluetoothSerial.write(myData[5]);

Or, with a loop:

for (int i = 0; i < 6; i++) {
    myBluetoothSerial.write(myData[i]);
}

or using a built-in variant of write that does the loop for you:

myBluetoothSerial.write(myData, 6);
7
  • Thanks for the comment. I just tried this and got the following characters out on my serial monitor. These characters should represent 6 bytes worth of data. ÆúÂÊàDÿÜ?²Nÿª?È{ÿ¹?×CÿŒ?ä`ÿà?Ïkÿð?ÿ
    – Alex K
    Apr 19, 2016 at 9:33
  • Are you sending the 6 bytes repeatedly over and over again?
    – Majenko
    Apr 19, 2016 at 9:33
  • Yes, but in between each transmission I have some other text printing to the serial monitor. E.G. a Count for ISR operations and Number of samples Below is a larger sample of the output @¾@ÀÂ?ǯ?¹ßßÂÄÑQÿ¿?Ë5ÿ¸?ÒGÿÐ?ÆYÿÉ?±CÿÊ?»#ÿÐ?ÔÊ?ßÑ?Øã?ÙÖ?ÜÜ?¸Û?OÿÍ?ISR Count: 84 Number of Samples: 1 ÙÎÜÍÂMÿê?¾GÿÔ?Òvÿ¤?ÍPÿÔ?æAÿÜ?áMÿÌ?:ÿISR Count: 117 Number of Samples: 1 Û?ѯ?ÄÄ?¹×?ÆÔ?ÑÁÄÍÇÁ¾aÿ·?ÝMÿÁ?ÔAÿË?àRÿÌ?ã0ÿÆ?ËÁ?ÃÄ?ÒÄ?ÇÄ?¸Å?ÅÅ?ÂÀÔÕÁÐAÿÂ?ÊGÿÐ?ÇEÿÛ?Ì8ÿÄ?Ç?ÿÌ?ÈUÿÕ?ÁÉ?ÐÄ?ÒÔ?ºÇ?ËÓ?ÝÌ?MÿË?ISR Count: 234 Number of Samples: 1 ÎÓæë†úþì? Cÿú?³<ÿÓ?¥'ÿÑ?Ø:ÿÚ?Ö,ÿÓ?EÿISR Count: 267 Number of Samples: 1
    – Alex K
    Apr 19, 2016 at 9:36
  • It is most likely correct. You won't be able to understand the data since it's no longer as numbers but ASCII characters. Without seeing your code I can't know what to expect from the output.
    – Majenko
    Apr 19, 2016 at 9:39
  • 1
    As I said, you won't be able to understand the output. It may be that the serial terminal is trying to interpret the seemlingly random data as UTF-8 and giving you all sorts of gibberish. Instead you are going to need to write the corresponding receiving software to interpret it and see if it is correct or not.
    – Majenko
    Apr 19, 2016 at 9:51
-1

Try changing Baud Rate. Most of the time garbage like this happens because of difference in baud rates of transmitter serial device and receiver serial device.

2
  • Both are set to 9600, I only get this when I use Serial.write instead of Serial.print.
    – Alex K
    Apr 19, 2016 at 10:14
  • 1
    oh, these two are different. Have a look at this question to understand HOW.
    – ARK
    Apr 19, 2016 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.