Barcode extracting digits

jackberg

New Member
Hello everyone,

I'm seeking comment to validate the code I'm using are 'ok as is' or can be edited for faster running.

The code use an 20x2 at 64Mhz, with the hserin to receive a UPC-A (0 59749 96859 1) from a barcode scanner.

After receiving, the left digits 2~6 (59749) are converted to a decimal value and assigned to w11 variable, later w11 will be use as address for an EEPROM.

Thanks to all.

1732559716764.png



Code:
#picaxe 20x2
#NO_DATA
#no_table
setfreq m64

hsersetup B9600_64, %00001
#terminal 76800
let dirsB = %00000100   
let dirsC = %00000000   

DO
;scan 12 digits UPC-A barcode : 0 59749 96859 1
hserin 0,12

;get & assign to var
get 0,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11

;ascii digit 2~6 "59749" to decimal value
w6=b1-48*10000
w7=b2-48*1000 
w8=b3-48*100   
w9=b4-48*10   
w10=b5-48     

;assign w11 decimal value 59749
w11=w6+w7+w8+w9+w10
;debug

;Send to Terminal barcode & w11
sertxd (b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,13,10,#w11,13,10)
LOOP
 
Your Mode byte for the 'hsersetup' command does not appear to be correct if you want to use the immediate 'hserin' command. If you want to receive the data with an 'hserin' command then Mode bit 0 shoud be set to 0.

Working with async data can get complicated. With your configuration of the 'hserin' command, the PICAXE will sit and wait until it receives 12 characters. If it should only receive 11 characters, it will wait until it receives the first character of the next packet and then you will find that all data after that point will be corrupted. That is why the command has an optional timeout parameter.

When using asynchonous data, you will have to consider what could happen if the PICAXE is not 100% ready to receive the full data string from the barcode reader. Are there packet-start character/s or checksum characters included in the data string from the reader? You may need to use these to be sure that the received data has your required address in positions 2-6.

Personally, I tend to use background receive and the scratchpad to receive long strings of async data. Once the data string is received, my code can search the string for start and stop characters as well as use a checksum to confirm the validity of the data.
 
Last edited:
Back
Top