Trouble converting Serial.write(Serial1.read()) to String

Hi all,

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.

When I do this:

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop()
{
  while (Serial1.available()) {      
    Serial.write(Serial1.read());
  }      
}

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.

Now, for the life of me I can’t seem to grab this as a string so that I can pass it to the Particle Cloud. I believe I tried every solution here: Convert serial.read() into a useable string using Arduino? - Stack Overflow

For example, when I try this:

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.

Thanks @ScruffR - this is the result when I read the incoming data in that format:

First barcode: 11 f3 00 00 08 37 35 38 34 39 37 31 31 33 38 34 39 fc 72

Second: 16 f3 00 00 03 32 32 30 33 38 35 35 30 30 30 31 35 33 30 30 34 36 fb 98

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?

1 Like

@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.

I found this: Download S700 Product Documentation - Socket Mobile

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.

1 Like

@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!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.