09 July 2021

Parts Inventory, and Digi-Key and Mouser Data Matrix Barcodes

Hey, look at that, an actual blog post that doesn't require me to remember things from a week or more ago! Anyway, I think I might've mentioned in some recent post that I was going to inventory all the parts I have so it's easier to keep track of what I have, instead of having to dig through a couple boxes because "I think I have...".

The first step was to get a barcode scanner that also reads 2D barcodes and is reasonably-priced, and SparkFun's 2D Barcode Scanner Breakout was the answer. When I found it, I thought their stock of 42 would be okay, but when I looked at it the next day, they were all gone! I think I sat for a few days before I found that SparkFun does have a "wait for backorder items before shipping" option, so I went ahead and put in an order, since they said they had about 144 units being made. I technically would've been fine with the module itself, but I don't know if they'll ever be bringing them back in stock, since it's part of their "SparkX" line which is for experimental stuff. Looking through their blog yesterday, I guess the scanner breakout was released 28 May, so maybe because it's such a new product, they're focusing all the modules to the breakout boards until demand goes down. Hell, even as I write this, they're out of stock once again and planning on making 60 more; the "Top Sellers" tab on their front page contains the scanner breakout, and for a good reason.

I got the scanner a couple days ago and played around with it some... I mean I was seriously like this:


And it was great being able to scan something that would be automatically entered in a spreadsheet, text editor, whatever. While the barcode scanner app for Android by ZXing Team is a long-time favourite of mine, it's not a very streamlined approach when I need to get the scanned data to my computer (I have it set to auto-copy the scanned contents to the clipboard, and then I paste it into Discord).

Digi-Key's labels for each bag of parts contains a Data Matrix code (at least the newer ones do, I don't know when they switched over, but I have some Digi-Key bags at work that were from 2015 that has a 1D barcode), and scanning that with the barcode scanner (the one from SparkFun) yielded info similar to this:
[)>06P559-1099-ND1PPS2801A-4-AK1K7013275410K8165562911K14LJPQ1311ZPICK12Z956474013Z5583520Z00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Which I could kinda figure out stuff from, but I needed more info. I did a quick search and found some people trying to figure it out (this being one), but no one had a complete list, so I decided to just figure it out myself. At work, I scanned one with the app and found some non-printing characters as "tofu" and it made a lot more sense to me.

♫A little bit of tofu in the text.♫

Using Discord, I pasted it within a code box so that I don't lose the tofu, and though they don't show, they were still there when I transferred it into Mousepad.

Tofu, demystified.

I use CudaText as my main text editor, but well, the non-printing characters are less obvious. Anyway, Unicode 001d is "Information Separator Three" or "[GS]" (Group Separator), and 001e is "Information Separator Two" or "[RS]" (Record Separator). I didn't really care what they were besides that they were non-printing control characters, but well, the difference between 001d and 001e would kinda serve me later. Looking at the data in Mousepad with the boxed control characters, It was much easier to make things out, and I was able to figure out that the "category" markers precede whatever number they are. I think it'll just be easier to share the findings first before I talk about certain items individually.

Digi-Key barcode data (using data from Mousepad screenshot):
  1. [)>
    • Probably a scanning software thing.
  2. 06
    • Probably another scanning software thing, this number is the same between Digi-Key and Mouser.
  3. P559-1099-ND
    • Part number, either customer-specified or retailer-specified, prefixed with P.
  4. 1PPS2801A-4-A
    • Manufacturer part number, prefixed with 1P.
  5. K9438
    • Customer purchase order (PO) number, prefixed with K, may be blank.
  6. 1K70132754
    • Sales order (SO) number, prefixed with 1K.
  7. 10K81655629
    • Invoice number, prefixed with 10K.
  8. 11K1
    • No idea what this is, but seems to always be 1, prefixed with 11K.
  9. 4LJP
  10. Q13
    • Quantity of items, prefixed with Q.
  11. 11ZPICK
    • No idea what this is, but seems to always be PICK, prefixed with 11Z.
  12. 12Z9564740
    • Part ID, prefixed with 12Z.
  13. 13Z55835
    • Load ID, prefixed with 13Z.
  14. 20Z00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    • Probably some sort of filler, prefixed with 20Z
I had thought 11K might've been MSL, but nope, the NeoPixels have the same value of 1. The invoice number (10K) stumped me for a little, until I went to check an email that I probably still had, and sure enough, the numbers matched. Since I don't need purchase order numbers for my personal orders, work was the best bet for figuring out K, though before scanning the two samples at work, I wasn't entirely sure how the data was structured.

While at work, I decided to scan Mouser's to see what it contained, and it was much less. Let's start with a screenshot of the data, and then jump right into the breakdown.

Miniscule compared to Digi-Key.

Mouser barcode data:
  1. >[)>06
    • Probably scanning software stuff. There's no 001e control character before 06, but at the least, the 06 is still the same in comparison to Digi-Key.
  2. K9439
    • Customer purchase order (PO) number, prefixed with K, web order number is used if customer does not provide a PO number.
  3. 14K022
    • Line item number (the line the item appears on an invoice, packing list, etc.), prefixed with 14K.
  4. PTP0194
    • Customer-specified part number, prefixed with P.
  5. 1P02-09-1117 (Cut Strip)
    • Manufacturer part number, prefixed with 1P, appears instead if there is no customer-specified part number. (This is from something else and isn't in/from the image.)
  6. Q10
    • Quantity of items, prefixed with Q.
  7. 11K062059574
    • Invoice number, prefixed with 11K.
  8. 4LLI
  9. 1VNeutrik
    • Manufacturer name, prefixed with 1V, may not be present.
I wasn't paying too much attention yesterday, so I thought Digi-Key and Mouser use the same structure, but I guess not. It's possible 11K for Digi-Key might be an another invoice number field, but I can't say for sure, and it's something I don't care about in all honesty. I was double-checking 11K for Mouser with some other part and found 1V, which might be something new, but again, not worthwhile for me. At the least, hopefully people looking for the structure find this blog post and is helpful to them.

Moving on. Because I found out about the control characters, I needed to figure out a way for the barcode scanner to send them. I tried the "serial over USB" (USB-COM) mode of the barcode scanner and ended up in an empty screen when loading up /dev/ttyACM1 (/dev/ttyACM0 is my Powermate), so I had to go the serial over UART route with a dev board. I used the Trinket M0 because it's small, and hooking up the barcode scanner to it was easy after I got a 6-position header soldered to the barcode scanner. I then pulled up a UART serial Adafruit guide as reference and had to try to fill in the blanks myself. It didn't seem to print anything out properly, and I tried baud rates of 9600 and 115200 in the code which both were claimed as the default in the settings manual (9600 is listed first as the default, but then 115200 is listed as the default three times after that, but eventually, I decided to scan the 115200 baud rate setting. And that was the problem all along, some mysterious default baud rate setting (I had a thought to go through them individually, but I didn't feel like it). I also tried USB-COM mode again and scanned the 115200 baud rate setting, but no dice, so I gave that mode up entirely.

A new problem arose, the printed text still contained no control characters, but the code I used has a line for printing the raw data, and after commenting that out, huzzah! I forgot when I swapped my janky wire set up for the 20-position GPIO set I bought along with the barcode scanner, but I did so because the ground wire kept popping off the barcode scanner header, and honestly, I should've done it sooner since it had a much better hold on the header pins. Anyway, the next issue was the fact that the raw data is a bytearray, and I needed to convert it to a string. Well, I thought I did, but I eventually realised that the conversion in the code was actually keeping the control characters, and that print() was the culprit of not printing them (I mean, they're called "non-printing" characters for a reason). While trying to see if print() had some sort of option to maybe print the characters, my eyes glanced over "sep" and I was reminded of replace(). Huge duh moment. I used a tab character to replace 001d and had replace() replace 001e with nothing, this would make things much more printable.

It was kinda in that mess where I was also thinking of trying to write the raw data to a file, and because I normally use open() in text, it took me a moment to remember about its ability to write bytes. While it did work with normal Python, CircuitPython refused to do it in the test because the flash appeared as read-only to it, and though I would've used an SD card over SPI, I didn't want to make things harder than it should be (plus I had just ordered the micro SD card breakout from Adafruit, so I would've had to wait anyway). Anyway, I think it was after I got replace() into the code (or it might've been a little before?) was when I decided to use the USB-HID library to emulate a keyboard, and because there's a library that allows a string as the input of what to send to the computer, I wanted to utilise that. The problem is that the examples changed with the documentation, so I had to try to figure it out, which wasn't too hard, but I was able to get it to work. It doesn't type nearly as fast as the barcode scanner directly, but at least I can get the control characters for processing before sending to the computer.

Because the .read() command for the UART serial only lasts for a bit, it was hard to test in the CircuitPython console, but it was fine in the code because of the looping. I also tried to send commands to the barcode scanner, but it was difficult at first because I forgot that there was a command prefix it looks for (^_^, caret underscore caret, I'm not kidding), and when I got it in there, it worked as intended. There was some other things I did/tried, but I won't talk about it because it's not really worthwhile.

The output (or at least simulated) from the MCU. I forgot the newline, but oh well.

Though I could do all the processing on the MCU of the stuff I care about (manufacturer's part number mainly), I wanted to keep the processing that needs to be done on the MCU as minimal as possible to make the job of scanning the inventory as quick as possible. I also added line in the code to send the save hotkey after it "types" the data, so that it's as automated as it can get. I still need to write the code for processing the file down to part numbers, but that won't be too hard and Pod will have it done before the snap of a finger.

Originally, I was thinking of buying a second barcode scanner (before I even bought the first) for a "scan and display" sort of thing, but this morning, I was considering on buying a third to keep in UART mode so that I don't have to change the settings. Along with this "third" barcode scanner, I also thought to make a custom breakout board with the ATSAMD21E18A-A because I thought I could maybe still get them, but it seems like Mouser's finally run out. Anyway, it would make it a single-board device instead of having two boards and some wires and be a little less awkward to use than the current setup with the Trinket M0. I'll still design it and such, but I won't be able finish the assembly for a while. Because I didn't really go over it that well, one would be in the fairly default mode for when I don't expect to run into control characters that would have the original board, another would be the self-contained "scan and display", and the last would be the UART version for when I need/expect control characters. Anyway, for anyone wanting the code for an Adafruit CircuitPython dev board to be able to get the above output with the barcode scanner, here's the not-entirely-pretty code based off of the code in the Adafruit guide I used:

import board
import busio
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS

uart = busio.UART(board.TX, board.RX, baudrate=115200) #you can use any baud rate, just make sure your scanner is set to that rate!
kbd = Keyboard(usb_hid.devices) #make the keyboard object
klo = KeyboardLayoutUS(kbd) #make the keyboard layout object with the keyboard object

while True:
    data = uart.read() #because the barcode length is unknown, don't set an amount of bytes to read
    if data is not None:
        data_string = ''.join([chr(b) for b in data]) #this converts the data to a normal string
        data_string = data_string.replace("\x1d", "\t").replace("\x1e", "").replace("\r", "\n") #make the necessary replacements (the barcode scanner defaults to \r for newline, but can be changed to \n, so it's there in case of forgetfulness)
        klo.write(data_string) #use the keyboard layout object to "type" the data string
        kbd.send(224,22) #send ctrl+s to save the file, 224 is the keycode for control, 22 is the keycode for s

Important thing to note is to keep the focus in the target programme, otherwise you could lose the data from the scan and/or make things weird. Also, make sure RX of the scanner is connected to TX of the dev board, and TX of the scanner is connected to the RX of the dev board! But anyway, with this code, you just use the barcode scanner's scan button like normal and the MCU will spit out the data after it gets and processes it.

I need to double-check what appears in the Mouser barcode for my stuff, since I don't use/have PO numbers, and based on how the P/1P is for Mouser, I'm thinking I'll se a 1K and the corresponding number. I'll edit this when I confirm it and probably will say that I did below.

Found that Mouser uses the web order number for the PO number, so no 1K category in the place of K. Also found that Digi-Key's packing list has some small Data Matrix codes that include Unicode 0004, which is "End of Transmission". I won't modify the above code, but if the codes on the packing list are to be used, just tack on .replace("\x04","") to the replacement chain.

(Edit: 2021-07-17)
While doing some testing for projects that include a graphic screen of some sort (the aforementioned "scan and display" and maybe the PSU tester), I came across one of my old code tests when I was playing around with the Trinket M0 and various libraries. If you're using Linux of some sort, or at least something that allows you to type Unicode characters using Ctrl+Shift+U, then you can actually type any Unicode character using Adafruit's adafruit_hid.keyboard library. I completely forgot about this and I honestly should've remembered because it saves the chain of replacements. Anyway, the code for raw output:

import board
import busio
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS

uart = busio.UART(board.TX, board.RX, baudrate=115200) #you can use any baud rate, just make sure your scanner is set to that rate!
kbd = Keyboard(usb_hid.devices) #make the keyboard object
klo = KeyboardLayoutUS(kbd) #make the keyboard layout object with the keyboard object

def send_unicode(char): #define a function to send the untypeable unicode character
    parts = hex(ord(char))[2:] #get the hexadecimal value of the untypable character without the "0x" prefix
    kbd.send(224,225,24) #send ctrl+shift+u so that the unicode value can be typed
    klo.write(parts) #send the unicode value
    kbd.send(44) #send space to finish the unicode entry

while True:
    data = uart.read() #because the barcode length is unknown, don't set an amount of bytes to read
    if data is not None:
        data_string = ''.join([chr(b) for b in data]) #this converts the data to a normal string
        for char in data_string: #iterate through each character in data_string
            try:
                klo.write(char) #try sending the character normally
            except:
                send_unicode(char) #or send it the other way if it can't be typed
        kbd.send(224,22) #send ctrl+s to save the file, 224 is the keycode for control, 22 is the keycode for s

No comments:

Post a Comment