0

Please bear with me here since this question is not so easy to explain and word correctly.

I am using the following code in order to get data from a USB connected barcode reader, the scanner works fine and data is passed in as expected but my DB lookups fail and I believe they are failing because the data I am passing into the DBLookup method is incorrect but I am unable to see why, I think NSLog is helping me to show clear text data when in fact it isn't and I am stuck at debugging further.

Here is my code

- (void)didBarcodeDataReceive:(StarIoExtManager *)manager data:(NSData *)data {
NSLog(@"%s", __PRETTY_FUNCTION__);

NSMutableString *text = [NSMutableString stringWithString:@""];

const uint8_t *p = data.bytes;

for (int i = 0; i < data.length; i++) {
    uint8_t ch = *(p + i);
    [text appendFormat:@"%c", (char) ch];
}

NSLog(@"Scanned info as NSData was: %@", data); // raw NSData
//NSString *stringWithData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *stringWithData = [[NSString alloc] initWithBytes:(char *)data.bytes length:data.length encoding:NSUTF8StringEncoding];
NSLog(@"Scanned info as StringFromData was: %@", stringWithData);
NSLog(@"Scanned ch conversion is: %@", text);

int createTransactionResult = -1;

createTransactionResult = [NWBarCodeHelper createTransactionRowFromBarCode:text];

if ([NWTillHelper isDebug] == 1) {
    NSLog(@"mPOP delegate holds barcode: %@", stringWithData);
    if(createTransactionResult != 0) {
        NSLog(@"TransactionListView:mPOPDelegate:createTransactionFrombarCode failed with errorCode %i", createTransactionResult);
    }
}
}

My Debug outputs shows the correct data as follows

    2017-04-19 10:19:01.868198 NWMobileTill[3751:1638657] Scanned info as NSData was: <30393235 38333834 33393439 35310d0a>
2017-04-19 10:19:01.868439 NWMobileTill[3751:1638657] Scanned info as StringFromData was: 09258384394951
2017-04-19 10:19:01.868652 NWMobileTill[3751:1638657] Scanned ch conversion is: 09258384394951
2017-04-19 10:19:01.868979 NWMobileTill[3751:1638657] NWBarCodeHelper:createTransactionRowFromBarcode:barcode = 09258384394951
2017-04-19 10:19:01.875938 NWMobileTill[3751:1638657] NWBarcodeHelper:CreateTransactionRowFromBarcode: 0 or more than one row returned, basic data error, item count = 0

But as you can see the last rows shows the DB lookup failing, I KNOW the method is correct cause when I scan using the iPhone camera and passing that data to the same method it works just fine on the same barcode so it must be something with the string that is passed in from the USB scanner that is tricking me out but I am unable to understand why and I think NSLog is trying to help me but not showing me the encoded data or something?

Matt Douhan
  • 2,053
  • 1
  • 21
  • 40
  • You have carriage return & line feed (0x0d and 0x0a) characters at the end of your string. You probably want to strip those. eg. http://stackoverflow.com/questions/26797739/does-swift-have-a-trim-method-on-string – Paulw11 Apr 19 '17 at 03:01

1 Answers1

1

Your string contains a \r\n at the end. Have a look at the following code:

unsigned char bytes[] = {0x30, 0x39, 0x32, 0x35, 0x38, 0x33, 0x38, 0x34, 0x33, 0x39 ,0x34, 0x39, 0x35, 0x31, 0x0d, 0x0a};
NSData *data = [NSData dataWithBytes:bytes length:16];
NSString *stringWithData = [[NSString alloc] initWithBytes:(char *)data.bytes length:data.length encoding:NSUTF8StringEncoding];

NSLog(@"%@", stringWithData); // 09258384394951
NSLog(@"%lu", (unsigned long)[stringWithData length]); // 16

// remove \r\n at the end which gets added by the barcode scanner
NSString *string = [stringWithData stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSLog(@"%@", string); // 09258384394951
NSLog(@"%lu", (unsigned long)[string length]); // 14

Or if you want to use your appendFormat approach you can just check if it is a valid digit before adding it to the string instead of removing it later.

To actually see the contents of your string you can either output the code point of each character in the string one by one or you can just set a breakpoint and Xcode will show it in the debugger: enter image description here

Nef10
  • 926
  • 2
  • 16
  • 26