convert uint8_t to String

Hi, i am using a ‘barcode scanner project’. for that; using code from this site.

following is my code:

#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>

#define DISPLAY_WIDTH 16

// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
 
USB     Usb;
//USBHub     Hub(&Usb);
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);

 
class KbdRptParser : public KeyboardReportParser
{
 
protected:
  virtual void OnKeyDown  (uint8_t mod, uint8_t key);
  virtual void OnKeyPressed(uint8_t key);
};
 
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)  
{
    uint8_t c = OemToAscii(mod, key);
 
    if (c)
        OnKeyPressed(c);
}
 
/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)  
{
static uint32_t next_time = 0;      //watchdog
static uint8_t current_cursor = 0;  //tracks current cursor position  
 
    if( millis() > next_time ) {
      lcd.clear();
      current_cursor = 0;
      delay( 5 );  //LCD-specific 
      lcd.setCursor( 0,0 );
    }//if( millis() > next_time ...
 
    next_time = millis() + 200;  //reset watchdog
 
    if( current_cursor++ == ( DISPLAY_WIDTH + 1 )) {  //switch to second line if cursor outside the screen
      lcd.setCursor( 0,1 );
    }
 
    Serial.println( key );
    lcd.print( key );


};
 
KbdRptParser Prs;
 
void setup()
{
    Serial.begin( 115200 );
    Serial.println("Start");
 
    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    }
 
    delay( 200 );
 
    Keyboard.SetReportParser(0, (HIDReportParser*)&Prs);
    // set up the LCD's number of columns and rows: 
    lcd.begin(DISPLAY_WIDTH, 2);
    lcd.clear();
    lcd.noAutoscroll();
    lcd.print("Ready");
    delay( 200 );
}
 
void loop()
{
  Usb.Task();
}

this code is working fine; but on display the varriable Key which is a type uint8_t; returning ASCII value.

for example, while reading barcode of
00312
it is giving output
48 48 50 48 52 56 48 52 56 48 53 49 48 52 57 48 53 48 19 48 48 51

i tried using following code… but didn’t work.

uint8_t key[700] = {0};
String str = (char*)key;
lcd.print( str );

appreciate if anyone can help.

Thanks in advance.

Cheers,

Faisal

48 48 50 48 52 56 48 52 56 48 53 49 48 52 57 48 53 48 19 48 48 51

'0' '0' '2' '0' '4' '8' '0' '4' '8' '0' '7' '1' '0' '4' '9' and so on

Regarding your 'millis()'-based timing. Your That website example's method will lead to errors when 'millis()' rolls over. It's far better to use subtraction:-

if(millis() - prevMillis >= period)
{
    //Do something here

    prevMillis += period;
}

try

    lcd.print((char) key );

the compiler sees you are passing a number, not a character to the print function, so decides to call the function that prints the value of that number rather than an ASCII value. if the signature detects it's a character that is being passed, then it will print the ASCII character corresponding to the value.

a simple example to prove this

void setup() {
  uint8_t c = 50;  // this is the value of character '2' in ASCII
  Serial.begin(115200);
  Serial.println(c);
  Serial.println((char)c);
}

void loop() {}

output in the console

[color=red]50[/color]
[color=blue]2[/color]

50 is the value which represents the ASCII char '2'

void setup() {
  uint8_t c = 50;  // this is the value of character '2' in ASCII
  Serial.begin(115200);
  Serial.println(c-48);//prints the value of c - 48 = 2 
}
 Serial.println(c - '0');

...is much more intuitive.

TGG3: void setup() {   uint8_t c = 50;  // this is the value of character '2' in ASCII   Serial.begin(115200);   Serial.println(c-48);//prints the value of c - 48 = 2 }

i would not recommend that... what will happen when he tries to scan

|250x67

Hi J-M-L,

(Char)Key has successfully converted ASCII to Text.

thanks a lot.

now need to solve receiving correct data. still getting junk from barcode reader.

ie. from barcode value 00312;i suppose to get 048 048 051 049 050. instead i am receiving 48 48 50 48 52 56 48 52 56 48 53 49 48 52 57 48 53 48 19 48 48 51.

maybe need to open another thread for this issue.

thanks again.

Z.Faisal

maybe need to open another thread for this issue.

Maybe not.

Ok then. i am continuing discussion on this thread.

So, can anyone help me find what i am doing wrong here and how to resolve?

please note that my Scanner is connected with a USB shield.

and with a keyboard connected to it gives a perfect result (no junk).

regards

Faisal

Why not post an image of the barcode?

ZFaisal: Ok then. i am continuing discussion on this thread. So, can anyone help me find what i am doing wrong here and how to resolve?

need info on your specific scanner model and possibly datasheet

And if you connect that same scanner to a PC and scan the same barcode (into notepad) what is the output?

AWOL:
Why not post an image of the barcode?

attachment enclosed.

00312.png

J-M-L: need info on your specific scanner model and possibly datasheet

My scanner info is as follows:

Brand : Honeywell Model : Eclipse MS5145 LS USB

datasheet :

Device descriptor: 
Descriptor Length:  12
Descriptor type:    01
USB version:        0110
Device class:       00
Device Subclass:    00
Device Protocol:    00
Max.packet size:    08
Vendor  ID:        0C2E
Product ID:     0200
Revision ID:        5881
Mfg.string index:   01
Prod.string index:  02
Serial number index:    00
Number of conf.:    01

Configuration descriptor:
Total length:       0022
Num.intf:       01
Conf.value:     01
Conf.string:        03
Attr.:          80
Max.pwr:        96

Interface descriptor:
Intf.number:        00
Alt.:           00
Endpoints:      01
Intf. Class:        03
Intf. Subclass:     01
Intf. Protocol:     01
Intf.string:        00
Unknown descriptor:
Length:     09
Type:       21
Contents:   11010001223F000705

Endpoint descriptor:
Endpoint address:   81
Attr.:          03
Max.pkt size:       0008
Polling interval:   0A


Addr:1(0.0.1)

i have used USB_Desc example from USB Host library.

note that at Line 29; its

Intf. Class: 03 Intf. Subclass: 01 Intf. Protocol: 01

that means it can boot as keyboard. Doesn't require any driver.

---------- ***

septillion: And if you connect that same scanner to a PC and scan the same barcode (into notepad) what is the output?

connecting on my notebook and scanning same barcode gives output on a notepad is :

00312

Regards

Z.Faisal

can you share the code you use on arduino again? did you implement the suggested modifications?

Here’s his barcode:-

00312.png

How is the barcode scanner configured? The Eclipse MS5145 Have TONS of options to do different things.

J-M-L:
How is the barcode scanner configured? The Eclipse MS5145 Have TONS of options to do different things.

For a true HID Barcode scanner; does it need special configuration?

regards

Z.Faisal

my complete code is here:

/*
 
Portable barcode scanner. Uses USB HID barcode scanner, Arduino Board, USB Host Shield and HD44780-compatible LCD display
 
  The circuit:
 * LCD RS pin to digital pin 7
 * LCD Enable pin to digital pin 6
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
*/
 
#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
 

#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
 
 
#define DISPLAY_WIDTH 16
 
// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
 
USB     Usb;
//USBHub     Hub(&Usb);
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);

 
class KbdRptParser : public KeyboardReportParser
{
 
protected:
  virtual void OnKeyDown  (uint8_t mod, uint8_t key);
  virtual void OnKeyPressed(uint8_t key);
};
 
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)  
{
    uint8_t c = OemToAscii(mod, key);
 
    if (c)
        OnKeyPressed(c);
}
 
/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)  
{
static uint32_t next_time = 0;      //watchdog
static uint8_t current_cursor = 0;  //tracks current cursor position  
 
    if( millis() > next_time ) {
      lcd.clear();
      current_cursor = 0;
      delay( 5 );  //LCD-specific 
      lcd.setCursor( 0,0 );
    }//if( millis() > next_time ...
 
    next_time = millis() + 200;  //reset watchdog
 
    if( current_cursor++ == ( DISPLAY_WIDTH + 1 )) {  //switch to second line if cursor outside the screen
      lcd.setCursor( 0,1 );
    }
 
    Serial.println( key );
    lcd.print( (char)key );

//    uint8_t key[700] = {0};
//    String str = ((char)key);
    
    Serial.println((char)key);
};
 
KbdRptParser Prs;
 
void setup()
{
    Serial.begin( 115200 );
    Serial.println("Start");
 
    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    }
 
    delay( 200 );
 
    Keyboard.SetReportParser(0, (HIDReportParser*)&Prs);
    // set up the LCD's number of columns and rows: 
    lcd.begin(DISPLAY_WIDTH, 2);
    lcd.clear();
    lcd.noAutoscroll();
    lcd.print("Ready");
    delay( 200 );
}
 
void loop()
{
  Usb.Task();
}

For a true HID Barcode scanner; does it need special configuration?

it should not but wondering if you did set it up with a special mode.

Your code prints multiple things per character

Serial.println( key );
...
Serial.println((char)key);

you just need one Serial.print((char) key );Serial.print(' ');

Also your watchdog thingy - why do you clear the LCD if you did not receive a char in the last 200ms ? Is that what you want?