I've done a number of projects implementing serial number allocation and printing as part of the production process, typically written in C# and using a label printer (Brother, Zebra etc)
Every project has been differnt because of the testing requirements, but one of the earlier ones is published on Github at
https://github.com/moorescloud/HolidayTest1This was for a early IoT product that ran Linux on an iMX233, and sent data to an ATmega328 for controlling a WS2812 RGB LED string. The test code was used by the PCB assembler to test each PCB by programming the ATMega via ISP and then bootload the iMX via USB with test code to check all functionality. It then programmed a unique serial number into the iMX one-time programmable ROM area, recorded results in a local database, and then show a green or red pass/fail status. Finally on success it printed a barcode label to be stuck on the PCB via a USB connected Brother QL570. The Windows app controlling it would display messages to indicate any failure to assist with identifying any required rework.
There was a second end of line test process used once the PCB was populated with SD Card and WiFi dongle and placed in its housing. On first power up it would connect to an pre-defined AP over WiFi, and submit a simple HTTP request to a local web server which printed the barcode label for the box with serial number and MAC address using a Zebra "clone" label printer with partly pre-printed labels. Again this code recorded the manufacturing info to be submitted back each day to management. On successful label printing a flag would be set on the SD card filesystem so future boots would run the end user code.
Typically I use serial numbers starting with a 2 digit mfg year, followed by a 1 or 2 digit numeric or alpha batch code, followed by a multi-digit unique sequence number. I tend to switch batch numbers or skip unique sequence numbers so as to help disguise actual production volume from end users or competitors.
ETA: Oh, and here is a little more info on the product/PCB:
http://kean.com.au/oshw/Holiday/