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!

Alex K
  • 219
  • 2
  • 8

2 Answers2

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);
Majenko
  • 105,095
  • 5
  • 79
  • 137
  • 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 '16 at 09:33
  • Are you sending the 6 bytes repeatedly over and over again? – Majenko Apr 19 '16 at 09: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 '16 at 09: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 '16 at 09:39
  • I added some code and details to my question above. The readBufferData function works as expected by returning a pointer to a uint8_t array. It's just the Serial output that is behaving strange. I used the software Serial output for testing purposes, but I wouldn't think that matters. – Alex K Apr 19 '16 at 09:47
  • 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 '16 at 09: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.

ARK
  • 514
  • 2
  • 15