Code readingHow-to

Send ASCII label data from a PLC serial port to a barcode printer

Send label data from a PLC serial port to a barcode printer: RS232 crossover wiring, 9600 8-N-1 ASCII settings, one TX string per label, line-time math, fixes.

LumaSpec VisionLettura 9 min Condividi su LinkedIn

In questa pagina Sezioni 8
  1. 01What you need
  2. 02Wire the link: PLC TX to printer RX
  3. 03Configure the PLC port and send one string
    1. Step by step
  4. 04Worked example: one 88-character label at 9600 baud
  5. 05Verify what you printed
  6. 06When nothing prints or the print is wrong
  7. 07Before you order parts
  8. 08Questions

Most guides on this site start with a scanner feeding a PLC. This job runs the other way: the PLC is the talker, and a label printer with an RS232 port prints a barcode when your program pushes plain ASCII out of a serial transmit line. There is no protocol stack to build and no Ethernet addressing to plan — one correctly framed string per label does the work. Here you get the five serial settings, the crossover wiring, the one transmit call, the timing math for 9600 baud, and the checks that prove the link. It is written for a compact all-in-one PLC with a spare serial port and any industrial label printer that lists RS232 ASCII among its interfaces.

What you need

  • One spare serial port on the PLC, set to raw ASCII (many platforms call this free-port or no-protocol mode). A port locked into a Modbus master role is the wrong port: the printer answers with text, not registers.
  • The printer's five serial values on paper. On current compact industrial label printers the documented factory defaults are 9600 baud, 8 data bits, no parity, 1 stop bit, XON/XOFF software flow control — but a menu change on the floor overrides them, so read the values from the printer, not from habit.
  • A crossover (null-modem) cable or an open-end cable you terminate yourself. The serial and I/O cables category carries DB9-to-open styles that suit this wiring.

A PLC serial port and a printer port are both DTE — data terminal equipment — so neither side crosses the lines for you the way a modem would. The cable does the crossing; that is what null modem means. On DB9 connectors the working wiring is:

Signal PLC side Printer side
PLC transmits TXD, pin 3 RXD, pin 2
Printer transmits RXD, pin 2 TXD, pin 3
Signal common GND, pin 5 GND, pin 5

Three crossed wires is the working minimum, and ground is not one you may skip. You can leave the printer's TX line out and still print, but then you never see XOFF (0x13) arrive when the buffer fills, and long bursts get dropped on the floor. Wire the return line.

For the printer end, a DB9 female with open solderable leads saves an adapter: LS-CBL-0224 is a frame grabber I/O cable with a DB9 female connector on one end and open leads, 7 m long.

CablesFrame Grabber I/O Cable, DB9F to Open, 7 m (LS-CBL-0224)LS-CBL-0224
Type
Frame grabber I/O cables
Length
6-8 m
$79.00View partBuild a system with it

Configure the PLC port and send one string

Set the PLC port to the printer's five values, in raw ASCII mode. The send itself is one instruction. On one compact-PLC platform the pair is PortPrint() to transmit and PortInput() to read the return line; most platforms give you a transmit-ASCII instruction that takes a string start address and a length. The direction of that call is the whole difference from the reading side: when you wire a barcode scanner's RS232 ASCII string into a PLC serial port, the PLC receives; here it transmits. Sending a command outward works the same as a serial trigger command to a barcode scanner: one ASCII line, one device that acts on it.

Step by step

  1. Print the printer's configuration page and write down the five serial values.
  2. Wire the crossover: PLC TXD (pin 3) to printer RXD (pin 2), printer TXD (pin 3) to PLC RXD (pin 2), pin 5 to pin 5. Land the shield at one end only.
  3. Configure the PLC port: raw ASCII mode, the printer's baud, 8 data bits, no parity, 1 stop bit, and software flow control if the port supports it.
  4. Build the label string in PLC memory as printable ASCII — your printer's own command fields — and terminate it with CR (0x0D) then LF (0x0A).
  5. Arm the transmit on the part-present edge and call the transmit instruction once per edge. One call, one label.
  6. Watch the printer's receive indicator while it runs: at 9600 baud an 88-character string clears in about 92 ms.
  7. Compare the PLC label counter with the count of labels in the bin.
// Illustrative pseudocode — variable names are mine
bool   port_busy     := false        // set by serial_send, cleared on port-done
int    label_count   := 0
string cmd           := ""           // label command stream, 88 characters

IF rising_edge(part_present) AND NOT port_busy THEN
    cmd         := build_label(lot_id, serial_no, qty, date_str)
    port_busy   := serial_send(cmd, 88)   // one call, one label
    label_count := label_count + 1
END_IF

The proof is on the HMI: label_count must equal the part counter. If label_count runs ahead, the transmit is retriggering.

Worked example: one 88-character label at 9600 baud

Inputs, each with its source:

  • Serial settings: 9600 baud, 8 data bits, no parity, 1 stop bit — the documented factory defaults for this printer class.
  • Command stream: the 88-character line below, counted including the CR and LF. The field names are illustrative; your printer's command set is its own, printed in its manual as ASCII lines.
HDR=LBL1;LOT=B12;SN=4711082;QTY=24;DD=2026-03-15;BC=4711082;TO=DOCK3;CUST=C0447;PRINT;<CR><LF>

The rule in words: each character crosses the line as one start bit, eight data bits and one stop bit — ten bits with no parity — so the line time is the character count times ten divided by the baud rate.

bits_per_character = 1 start + 8 data + 0 parity + 1 stop = 10 characters_per_second = baud / bits_per_character line_time = characters × bits_per_character / baud

The arithmetic: 88 characters × 10 bits = 880 bits. 880 bits ÷ 9600 bit/s = 0.0917 s ≈ 92 ms of line time, out of a port that moves 9600 ÷ 10 = 960 characters per second. At 115200 baud the same 880 bits take 880 ÷ 115200 = 0.0076 s ≈ 7.6 ms.

The decision: keep 9600 baud when one label rides each part cycle of 1 s or more — 92 ms is under a tenth of the cycle. If you burst ten labels in one cycle the line alone needs 10 × 880 ÷ 9600 = 0.92 s; when that eats the cycle, or the PLC must report done within 50 ms, set both ends to 115200 baud or move the printer to its Ethernet port. Line time is transmission only — the print mechanism takes its own time after the last bit lands.

Verify what you printed

Printing is half the loop. On a traceability line, scan the fresh label before the part moves: bottle label barcode verification with a fixed reader and a PLC serial link shows the full arrangement. The fixed code readers category has compact models for that check — LS-CRD-0047 is a compact fixed reader, 1.2 MP, with a 60-120 mm reading distance over USB.

Fixed code readersCompact fixed industrial barcode reader, 1.2 MP, USB 2.0, 60-120 mm reading distance (LS-CRD-0047)LS-CRD-0047
Resolution
Up to 3 MP
Shutter
Global
Interface
USB
$559.00View part

When nothing prints or the print is wrong

Symptom Most likely cause Fix
Nothing prints, receive lamp dark Straight-through cable or TX on the wrong pin Cross the lines: PLC TXD (pin 3) to printer RXD (pin 2)
Garbage characters on labels or in the receive log Baud, data bits or parity mismatch Match all five values; reprint the configuration page
First label prints, the rest stop XOFF (0x13) ignored while the buffer filled Wire the printer's TX return and pause on XOFF until XON (0x11)
Printer holds a partial command Missing terminator End every string with CR (0x0D) and LF (0x0A)
Duplicate labels Transmit retriggered every scan Fire once per part-present edge, re-arm on port-done

Baud and parity mismatches turn your commands into garbage exactly as they do on the receive side, so the diagnosis in fixing garbage characters from a scanner on a PLC serial link applies line for line. More receive-side and print-side serial guides are collected on the code reading topic page.

Before you order parts

  • One PLC serial port free and settable to raw ASCII; if scanners or drives claim every port, plan the printer on Ethernet instead.
  • The printer's five serial values on paper from its configuration page, not from memory.
  • A crossover wiring plan: TXD to RXD both ways, plus ground.
  • A DB9-to-open cable long enough for the cabinet run.
  • A fixed reader on the discharge side if the line logs serial numbers.

The system configurator walks the cable and accessory step if you would rather have the parts matched for the build. Stuck between two ports, or a printer that will not match settings? Ask through the contact page — expert support is free with every order.

Domande

Do I need a straight-through or a crossover cable to the printer?

A crossover. A PLC serial port and a printer port are both DTE, so transmit must land on receive: PLC TXD (DB9 pin 3) to printer RXD (pin 2), printer TXD (pin 3) to PLC RXD (pin 2), pin 5 to pin 5. A straight-through cable belongs between DTE and DCE, such as a modem. If settings match and nothing prints at all, swap pins 2 and 3 at one end — that swap has fixed more silent serial links than any setting change.

How long can the RS232 run from the PLC to the printer be?

The classic working figure for RS-232 is about 15 m at 9600 baud on decent shielded cable, and inside one cabinet keep it under 3 m if you can. Ground the shield at one end only and route it away from drive and motor wiring. If the printer must live farther out, give it its Ethernet port rather than pushing serial down a long run at low baud.

Why does the printer print one label and then stop responding?

Two usual causes. Either the printer sent XOFF (0x13) because its buffer filled and your PLC never paused for XON (0x11), or your string lacks the CR/LF terminator, so the printer sits on a partial command that never executes. Wire the printer's TX return line, react to XOFF by pausing transmission, and end every label string with CR and LF.

What serial values do I use if nobody documented the printer's settings?

Start with 9600 baud, 8 data bits, no parity, 1 stop bit and XON/XOFF software flow control — the documented factory defaults on current compact industrial label printers. Then print the printer's configuration page from its front panel and match the PLC to whatever it lists, because a floor-side menu change overrides any default. Any single mismatch corrupts the string; a wrong baud shows up as garbage characters immediately.

Can the same serial port read a barcode scanner and drive the printer?

No. A serial port is one point-to-point link, and here the printer listens on the PLC's transmit line. A scanner talks the other way — it transmits and the PLC receives — so it needs its own port with the roles reversed. Wire the scanner to a second port; the mirror-image job of reading a scanner's RS232 ASCII string into a PLC serial port has its own guide.