Sunday, October 2, 2011

Printing GS1 DataMatrix with ZPL

GS1 is a standard for encoding information in barcodes. It can be used to label products in a warehouse, or when shipping the products to customers. A GS1 code consists of a number of pre-defined fields, such as item number or product type.

The format is relatively simple. Each field is prefixed by a decimal number called the Application Identifier (AI for short). The length of the number depends on the first two digits (e.g. 250 begins with '25', so the length must be 3 digits). After the label, comes the data. Depending on the field number, the field can be either fixed-length or variable-length. The variable fields also have a maximum length, but must be suffixed by a special character: ASCII 29 (Group Separator, GS for short).

Some barcode formats only allow fixed-length fields, because they cannot encode the GS character. I will only talk about DataMatrix codes, which can support any 8-bit code. A GS1 DataMatrix code starts off with the meta-character FNC1. More on this later.

DataMatrix comes in a few different quality levels: 0, 50, 80, 100, 140 and 200. These numbers stand for the amount of data that is added to the symbol for error correction. 0 use no error correction. 50 to 140 use convolution encoding. 200 use Reed-Solomon encoding. These quality levels are also referred to with a ECC prefix: ECC 50 to ECC 200. The GS1 standard recommends using only ECC 200, and it is the one I will be talking about here.

ZPL is one of the languages that can be used to talk to your Zebra label printer. It is mainly text-based, and consists of a large number of cryptic commands. Each command begins with ^ or ~ (These characters can also be customized). Then comes one or two uppercase letters specifying the name of the command. Last comes zero or more comma-separated arguments.

Each document begins with ^XA (Start Format) and ends with ^XZ (End Format). In between you can change almost any setting, issue print commands, and even flash a new ROM. I will mostly discuss the ^BX command, which print DataMatrix barcodes.

^BX can take up to 7 arguments:
orientation, height, quality, columns, rows, format, escape character. Quality should be 200 for ECC 200, and you want to set some escape character. Here is the command I used:
To specify the data, you should follow the ^BX command with a ^FD...^FS region. Inside this region, you can enter almost any data, but you might want to stay with simple ASCII characters. You can use the following escape sequences to encode other characters:
  • @1 to @3 - FNC1 to FNC3. These are meta characters that lies outside the normal data-range.
  • @5NNN - codepage. Changes to codepage NNN (three digit decimal number).
  • @dNNN - character. Inserts data character NNN (three digit decimal number).
  • @@ - escaped. Inserts the escape character itself.
To encode the GS1 string (90)SE000(250)1234567890 as a DataMatirx barcode you could write:
Of course, you would need to specify a few settings before hand. You could get a template to work with by using the program Zebra Designer that comes with the printer. You just design your label, and print it to a file. (File → Print → Print to file)

For more information: