Bluetooth sending garbage characters

I’m sending short delimited text strings to an Arduino using a HC-05 card. The information was getting garbled, so I examined it character-by-character. All the desired text was there, as well as the delimiters, but every so often the string includes some ‘-1’ characters (i.e. byte 0xFF). Sometimes it’s just a few (< 10), but sometimes there are dozens of them. I don’t understand where they are coming from.
Has anyone else ever experienced this?
Any helpful suggestions would be welcome. The only things I can think of are:

  • EMF interference
  • Faulty HC-05 card (although it seems to work otherwise)

If you upload your Downloaded Blocks Image file we can look at it.

Here’s more information.
The blocks image shows the AI blocks that send the text. The arguments ‘cmd’ and ‘value’ are numbers. ‘cmd’ is an integer, and ‘value’ may have decimal digits.
The ‘C code.txt’ file is the Arduino code that reads the data from a serial port. In this example _SERIAL is a SoftwareSerial port, and DIAGNOSTIC is the Arduino’s built-in serial port that sends the results to my PC via USB. In my case, I am using the ‘|’ character as the delimiter. It works better than NL or CR because it is printable.
The ‘Example1.txt’ file shows what the C code reads. It shows how the characters are read and how the strings are assembled. It shows some input that is read the way it should be when there is not garbage, as well as some examples of the junk that often comes along.
blocks
C code.txt (976 Bytes)
Example1.txt (2.6 KB)

I’ll add some additional information. The Arduino also sends information to my smartphone using the same delimited text strings. It also sends messages as pairs of numbers, and uses ‘|’ as the delimiter. I’ve had no problems with the communication in this direction. The only trouble is when the information flow is FROM the phone TO the Arduino.

The sending procedure is not enough.
We can’t see what is being passed to it.
Export the project .aia file and post it here.

What happens if you use one of the freely available BlueTooth terminal apps to send to your Arduino?

I don’t know the specific type of _SERIAL in your sketch code, but if it is the Arduino Serial object, or something compatible, read() will return -1 if there isn’t any data available. You may want to update your code to test for this because likely your sketch is running faster than it can read data from the Bluetooth controller (or that the controller receives data from the phone).

This code sample uses the Serial.Available value to avoid the -1’s …
https://www.arduino.cc/reference/en/language/functions/communication/serial/read

I have tried this, and have never experienced the same issue.

Here is the .aia file. Good luck. I was learning how to use a graphic interface instead of a text interface, so I don’t know that I was very efficient here.RosieII.aia (61.1 KB)

I will try testing to make sure I don't read if there is nothing in the buffer. I can used the delimiter to make sure the message is complete.

You are using a bar char to separate values, but do not appear to be sending an end-of-data char (e.g. \n) so that the Arduino knows when to stop reading?

We could suggest other things but without your Blocks and the Sketch, it would take a month of Sundays to guess.

Chris,
This is not a problem. The Arduino programming looks for ‘|’ and uses it to separate the character strings. It works very well. The problem I am having is extra characters or misread characters.
I have modified the string reading function to work this way:

String getStringDelim(char delimiter) {
String s = String(" "); // up to 16 char max
char c;
int i = 0;

do {
if (_SERIAL.available()) {
c = _SERIAL.read();
s.setCharAt(i++, c);
}
} while (c != delimiter);
s.remove(i - 1); // remove the delimiter and excess white space
return (s);
}

This works very well, and no longer creates excess ‘-1’ characters. The only problem now is when there is a transmission error. Some characters I’ve seen include ‘&’ and some non-ASCII characters like 241 and 255. I don’t know how to eliminate these kinds of errors, except to have the program recognize the garbage and throw it away.

I believe it could well be the problem - but if you don’t want my help, that’s fine, I have a lot of work to do.

Chris,
You could help by answering a question I could not find the answer to in the Guide.
When you use the Bluetooth SendText() function to send some characters, does it just send only those characters? Does it add a terminator, such as ‘\0’ or ‘\n’? Or does it just send the characters and leave them hanging with no way for the receiver to know the string is complete? The AI Guide offers pretty minimal information on the function.
It seems there needs to be some sort of terminator. Is there a default terminator, or must you explicitly add one? And if you need to add one, what difference does it make, whether it is a ‘\n’ or some other character, as long as the receiving program knows what to expect?
If you know the answer, it could save me some time.

Hi @mrwags,

The string is converted to the UTF-8 representation of the string as bytes. There isn't any terminator sent. If you want to send a \n you should include it in the string.