I’ve succeeded in pairing an HC-05 bluetooth module to a Socket Mobile S700 bluetooth handheld scanner, and I’ve then connected the HC-05 to the Photon’s Serial1 port.
and scan a barcode, I see the correct barcode appear in the serial terminal. It is bookended with some special/junk characters on each scan (example: 758497113849�r or �22038550001530046��), but in my testing with different barcodes the full barcode string is present each time.
String readString;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
while (Serial1.available()) {
char c = Serial1.read(); //gets one byte from serial buffer
readString.concat(c); //makes the String readString
delay(5);
}
if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}
The result in terminal just shows up as a single special character �.
I also tried this: Serial.available() - Arduino Reference and am able to print each character of the barcode in the various formats (e.g., binary, hex) but I’m not sure how to store these in a buffer and then convert it to string.
Does anyone have any idea how I might achieve this? Thank you.
These extra characters are probably some non-printable control bytes. In order to interpret them correctly you may want to print them as numeric values.
e.g.
Serial.printf("%02x ", Serial1.read());
This way you don't get '�' for all kinds of received bytes but can see what you actually receive.
Once you know why you get these extra bytes and what they may mean, you can create a parsing logic that gives you what you want.
Any idea on what to do next? I was trying to figure out a way to push the incoming bytes to an int array (?) and then convert that to a string, and later parse the barcode on the server side and strip out the junk, but I remain stuck!
So those 00 bytes are problematic to concat into a string since zero is the string terminator. Why don’t you filter the results to just the numeric ASCII character 0x30 to 0x39? Can the result ever be non-numeric?
@bko hmm what do you mean filter the results in that way? Unfortunately yes, sometimes barcodes have letters - so in this case I am dealing with what appears to be no consistent terminator character present, as well as variable length barcodes.
@bko when you say 00 is the string terminator, is that why in the example I posted above for readString I am only seeing the single special character, as the actual barcode shows up after the 00 and gets lopped off?
Yes to string terminator question. Putting a 0x00 into the string ends it at that position which in this case looks like it has two non-ASCII characters.
Have you put the scanner in a special mode or are you using the default HID mode? I wonder if the extra bytes are keycodes or something? They do not appear to be simple checksums, but they could be an error correcting code. You may need to contact the manufacturer to find out how to use their bluetooth protocol.
@bko you had me take a closer look at the manual again and the configuration barcodes I’d been scanning - I changed it from Packet Mode to Raw Mode and now the special characters are gone and the code snippet above with readString works! Thank you!