Code 39 (also known as USS Code 39, Code 3 of 9) is the first
alpha-numeric symbology developed to be used in non-retail environment.
It is widely used to code alphanumeric information, such as the model number
etc. It is designed to encode 26 upper case letters A-Z,
10 digits 0-9 and 7 special characters - hyphen (-), period (.),
dollar sign ($), forward slash (/), plus sign (+), percent (%) as well as the
space character.
Note
In version 1.5, to print a barcode character that will be scanned
as space, use underscore (_) or equal
sign (=). To print a blank white space, use space
character (ASCII 32).
Although you can put as many characters as you can, in practice you can not encode many characters. Many barcode scanners have a short scan range at 3 inches.
Each Code 39 barcode must begin and end with special bar/space patterns.
They are often referred as start/stop character. In the font,
the start/stop character is mapped to the asterisk (*). You can also use
left square bracket [ and right square bracket ]
to produce them.
Creating a Code39 barcode is a simple task. Just enclose your data with asterisks and print the whole string with a C39 font, you get a barcode. For example, let's say that the data to be encoded is:
PN99018
The barcode string for printing as a Code39 barcode would be:
*P99018*
The below escape sequences print the barcode, and the human readable text below the barcode:
(0Y<ESC>(s0p8h12*PN99018* <ESC>(3@PN99018
C Code
The following code snippet demonstrates the barcode printing in C language.
int PrintPartNumberBarcode(char* pPartNo, FILE* prn)
{
/*select MRV Code39MA Height 12 points) */
fprintf(prn, "\x1B(0Y\x1B(s12.00H");
fprintf(prn, "*%s\n", pPartNo);
}
BASIC Code
The following BASIC function returns the same barcode string.
Function GetBarcodeString(partno as string) as String
Dim str as string
str = "*" & partno & "*"
GetBarcodeString=str
End Function
Some applications, such as HIBC and LOGMARS, require a modulo 43 Check character at the end of the barcode (just before the stop character). The scanners must be configured with the checksum verification turned on in order to use the feature. The design of the checksum is to guard the data integrity. The scanner calculates the checksum at the time of scan, if it does not match the one in the barcode, the scanner assumes that some portion of the barcode was misprinted or misread and rejects the barcode.
Table 5.1. Code39 value table
| char | value | char | value | char | value | char | value |
|---|---|---|---|---|---|---|---|
| 0 | 0 | A | 10 | N | 23 | hyphen (-) | 36 |
| 1 | 1 | B | 11 | O | 24 | period (.) | 37 |
| 2 | 2 | C | 12 | P | 25 | SPACE ( ) | 38 |
| 3 | 3 | D | 13 | Q | 26 | dollar ($) | 39 |
| 4 | 4 | E | 14 | R | 27 | slash (/) | 40 |
| 5 | 5 | F | 15 | S | 28 | plus (+) | 41 |
| 6 | 6 | G | 16 | T | 29 | percent (%) | 42 |
| 7 | 7 | H | 17 | U | 30 | ||
| 8 | 8 | I | 18 | V | 31 | ||
| 9 | 9 | J | 19 | W | 32 | ||
| K | 20 | X | 33 | ||||
| L | 21 | Y | 34 | ||||
| M | 22 | Z | 35 |
The following procedure explains how to calculate the modulo 43 checksum:
First assign each character in the barcode a numeric value (0 through 42) according to Table 5.1, “Code39 value table”. The start and stop characters do not participate the checksum calculation.
Sum the values of all the data characters.
Divide the result from step 2 by 43.
The remainder from the division in step 3 is the checksum character that will be appended to the data message before the stop character