1. Overview of CP340 Red-Color Printing
The Siemens CP 340 point-to-point communication processor is widely deployed on S7-300 stations to drive serial printers, barcode readers, and weighing terminals. The standard function block FB4 "P_Print" (part of the SIMATIC S7 PtP library) ships message text either from a CP340-resident message text list or from a user-defined data block, and forwards it to the printer through the configured protocol (3964R, ASCII, or RK512).
To colorize a section of a printed line - for example, marking a "Filling not ok" condition in red - the application must inject a printer-specific escape sequence into the data stream at the exact point where the color change is required. The CP340 supports this through a control character translation table in the PtP parameter assignment, where a printable ASCII character (commonly 'R') is mapped to the printer's vendor escape string (for example, ESC r 0 / ESC r 1 in Epson-compatible devices).
This document describes the engineering workflow that maps a logical "red" tag (M9.1 = "Filling not ok") onto a correctly framed ASCII stream, the CP340 parameter assignment, and the FB4 P_Print interface, including verification and troubleshooting.
2. Prerequisites
| Item | Specification / Version | Notes |
|---|---|---|
| CP340 module | 6ES7 340-1AH02-0AE0 (RS232C) or 6ES7 340-1BH02-0AE0 (20 mA TTY) or 6ES7 340-1CH02-0AE0 (RS422/485) | Firmware V1.x or V2.x; newer variants are CP341 |
| STEP 7 / SIMATIC Manager | V5.5 SP2 or later with PtP library installed | Needed for parameter assignment in HW Config |
| S7 PtP library blocks | FB2 P_SEND, FB3 P_RCV, FB4 P_Print (or P_Print_R for newer CP340 firmware) | Part of "SIMATIC S7 PtP Param / S7-300" library |
| Printer | Epson ESC/P compatible (TM-T20, TM-U220, LX-300+II) or any device with documented escape sequence for red/black ribbon lift | Vendor manual must list the red/black selection escape |
| Serial cable | RS-232 null-modem or printer-specific; pinout must match CP340 template selected in HW Config | Handshake lines (RTS/CTS, DTR/DSR) verified |
| Engineering station | PG with MPI/Profibus or Ethernet (RFC1006) to download HW Config and blocks | Online test capability required for FB4 status check |
3. CP340 Architecture and P_Print Function Block
The CP340 runs the physical layer, frame handling (start/stop characters, XON/XOFF, 3964R control characters) and the message text list internally. The CPU-side interface is a dual-port mailbox accessed by the standard point-to-point function blocks. For printing, FB4 P_Print is used.
3.1 FB4 P_Print - Calling Interface
| Parameter | Declaration | Data Type | Description |
|---|---|---|---|
REQ |
INPUT | BOOL | Rising edge triggers print job |
RST |
INPUT | BOOL | Resets the FB and clears internal state |
PRINT |
INPUT | BOOL | Enable continuous print job (level-triggered) |
LEN |
INPUT | INT | Number of bytes to print (1-2048) |
DB_NO |
INPUT | INT | Number of the data block containing the message text |
DBB_NO |
INPUT | INT | Byte offset inside the DB where the text starts |
LADDR |
INPUT | WORD | I/O start address of the CP340 (e.g. W#16#100) |
DONE |
OUTPUT | BOOL | Job completed without error |
ERROR |
OUTPUT | BOOL | Error occurred; check STATUS
|
STATUS |
OUTPUT | WORD | CP340 status word; 0x0000 = OK, 0x0F01..0x0Fxx = job state, 0x8xxx = error class |
3.2 P_Print Data Block Layout
The pointer DB specified by DB_NO / DBB_NO contains the raw byte stream that will be transmitted. The byte stream is built string-fashion; a typical 80-column line looks like:
DB100
DBB 0 : 'Line 01: Filling = ' (22 bytes)
DBB 22 : 27 dec (ESC)
DBB 23 : 114 dec ('r')
DBB 24 : 49 dec ('1') -- enter red ribbon position
DBB 25 : '123.45 kg' (9 bytes)
DBB 34 : 27 dec (ESC)
DBB 35 : 114 dec ('r')
DBB 36 : 48 dec ('0') -- return to black
DBB 37 : 16#0A (LF)
The two preceding bytes - 27 dec (ESC) and 114 dec ('r') - form the printer's red/black ribbon select escape. The third byte (48 dec = '0' or 49 dec = '1') selects the ribbon half: '0' for black, '1' for red on Epson ESC/POS / ESC/P ribbon-shift printers. The MOVE instructions in the user's program are simply writing either '0' or '1' into byte 24 or 36 of the message DB, controlled by the boolean tag M9.1 ("Filling not ok").
4. ASCII Escape Sequence Theory for Printer Color Control
An escape sequence is a multi-byte in-band command that begins with the ASCII control character ESC (27 dec / 0x1B). The CP340 must be told to forward the ESC byte literally to the printer (it is normally filtered as a non-printable control character). The mechanism is the control character translation table in the PtP parameter assignment.
4.1 ASCII Code Reference
| Dec | Hex | Char | Meaning in this application |
|---|---|---|---|
| 27 | 0x1B | ESC | Introduces all Epson escape sequences |
| 48 | 0x30 | '0' | Ribbon selector: black half (Epson ESC r 0) |
| 49 | 0x31 | '1' | Ribbon selector: red half (Epson ESC r 1) |
| 114 | 0x72 | 'r' | Epson "Select print colour" command letter |
| 92 | 0x5C | '\\' | Backslash - escape introducer in STEP 7 STRING syntax |
4.2 Two Methods to Inject Color Sequences
-
Method A - String-internal escape (recommended for variable data): Build the message DB byte-by-byte using
MOVE/BLKMOVinstructions. The user's program is the source of the escape bytes; CP340 is left at its default configuration. This is the technique observed in the original program (FC40 / FC41 from the source project). -
Method B - Symbolic control characters: Configure the CP340 control-character table so that a printable character such as
'R'is replaced at transmission time with the actual red-select sequence. The CPU program only contains\R text\-Rtokens; the CP340 expands them. This is cleaner but requires HW Config parameter assignment and is limited to fixed escape strings per color.
5. Step-by-Step: Configuring CP340 Control Characters (Method B)
This section implements Method B for completeness. Skip to Section 6 if you intend to use Method A (string-internal escape), which is what the source program uses.
- Open the S7 project in SIMATIC Manager and double-click the CP340 in the S7-300 station.
- In HW Config, right-click the CP340 and choose Object Properties > Parameter > PtP.
- Open the Control characters tab (chapter 2.7.5 of the CP340 manual). The default table contains 7 ASCII control chars (0x00..0x06, 0x09..0x0D, 0x1B).
- Click New and add a symbolic control character. Recommended mapping:
Symbol ASCII On-sequence (hex) Off-sequence (hex) R 0x52 1B 72 31 1B 72 30 B 0x42 1B 72 30 1B 72 31 - Save, compile, and download the HW Config to the CPU.
- In the CPU program, write the message text using STEP 7 STRING syntax:
The// String constant example "\R FILLING NOT OK \-R Qty = 123.45 kg"\Ris replaced by the CP340 withESC r 1, the\-RwithESC r 0.
6. Step-by-Step: Programming the P_Print FB for Colored Output (Method A)
This is the technique the user's program implements, and the one most likely to be the correct approach for variable-driven color decisions (e.g. M9.1 "Filling not ok").
6.1 Build the Message DB
Create a shared DB (e.g. DB100 "PrintBuffer") with the following initial values. The colored portion is parameterised by a flag byte at DBB24 / DBB36.
DATA_BLOCK DB100
TITLE = PrintBuffer
STRUCT
Line1Red_On : BYTE := B#16#1B; // ESC -- 27 dec
Line1Red_Cmd : BYTE := B#16#72; // 'r' -- 114 dec
Line1Red_Sel : BYTE := B#16#30; // placeholder, set by program (48 or 49)
Body1 : STRING[20] := ' Filling = ';
Value1 : REAL := 0.000000e+000;
Line1Red_Off : BYTE := B#16#1B;
Line1Red_OffC: BYTE := B#16#72;
Line1Red_OffS: BYTE := B#16#30; // placeholder, set by program
CrLf : BYTE := B#16#0A;
END_STRUCT;
END_DATA_BLOCK
6.2 Conditional MOVE Logic (FC40 excerpt)
// In FC40, network 1: ribbon select = red when M9.1 = 1, else black
A M 9.1 // "Filling not ok"
JCN NO_RED
L B#16#31 // '1' = red ribbon
T DB100.DBB 24 // Line1Red_Sel
L B#16#30 // '0' = black ribbon for the OFF token
T DB100.DBB 36 // Line1Red_OffS
JU CONT
NO_RED: L B#16#30 // '0' = black ribbon for the ON token
T DB100.DBB 24
L B#16#30 // '0' = black ribbon for the OFF token (redundant but safe)
T DB100.DBB 36
CONT: NOP 0
The exact byte offset depends on how the message DB is structured. The two pairs of MOVE instructions observed in the source project are populating the two positions reserved for the ribbon selector character (49 dec = '1' for red, 48 dec = '0' for black).
6.3 P_Print Call (FC41 excerpt)
// Triggered by I0.0 (manual print button) or print cycle flag
A I 0.0
FP M 10.0
= #print_trigger
CALL FB 4, DB 5 // FB4 P_Print, instance DB5
REQ := #print_trigger
RST := FALSE
PRINT := FALSE
LEN := 38 // total length in bytes
DB_NO := 100
DBB_NO:= 0
LADDR := W#16#100
DONE := M 10.1
ERROR := M 10.2
STATUS:= MW 12
AN M 10.2 // no error
JC PRT_OK
// handle error -- see Section 8
PRT_OK: NOP 0
7. P_Print Status Word and Error Reference
| STATUS (hex) | Meaning | Remedy |
|---|---|---|
| 0000 | Job successfully completed | - |
| 0F01 | Job in progress, no error | Wait; poll DONE / ERROR |
| 0F02 | Job finished, data in transit | Wait for printer echo / hardware handshake |
| 8001 | LEN > 2048 or LEN = 0 | Bound LEN to 1..2048 |
| 8010 | DB_NO does not exist or is too short | Check DB length >= DBB_NO + LEN |
| 8020 | CP340 not configured / not synchronised | Re-download HW Config; check SF LED on CP |
| 8030 | CP340 is in "STOP" (e.g. diagnostic interrupt active) | Read CP340 diagnostic buffer; clear via STEP 7 "CP diagnostics" |
| 8040 | Hardware fault (overrun, parity, framing) | Check baud rate, parity, cable length, shielding |
| 8050 | Handshake timeout (CTS, DSR, XOFF) | Verify printer is online, paper feed, ribbon |
| 80F1 | Internal FB4 / CP firmware inconsistency | Upgrade to current P_Print FB version; check Siemens support entry 109751769 |
8. Troubleshooting Matrix
| Symptom | Probable Root Cause | Diagnostic Step | Fix |
|---|---|---|---|
| Color change ignored, text printed in black only | Control character table not loaded, or ESC filtered by CP340 | Open CP340 online > PtP diagnostics > check "control character set in use" | Re-download HW Config; if Method A, ensure ESC byte is in DB and LEN covers it |
| Garbled characters before/after the colored section | Wrong byte count (LEN too small, or DB mis-aligned) | Online > DB100 > "Monitor/Modify" all DBBs of the print buffer | Recompute LEN; verify DBB_NO is a multiple of 2 for STRING[xx] use |
| Printer prints red even when M9.1 = 0 | Initial values of the DB still contain 49 dec after restart | Compare offline initial values vs. online actual values of DB100 DBB24 / DBB36 | Initialize the DB at startup in OB100 with the black token (48 dec) so that the actual value is correct |
| FB4 returns 0x8050 after the first print | Printer offline / ribbon jam / paper out | Read printer error LED; check CP340 SF / BF LEDs | Clear printer error; CP340 auto-retries once handshake returns |
| Print happens, but with random characters in front | M9.1 is set, but the two MOVE instructions are racing each other | Trace M9.1 and the two MOVE outputs in a VAT | The two MOVEs are mutually exclusive in the source code - verify JCN/JU structure, never place both MOVEs in the same unconditional network |
| P_Print instance DB says "not Know_How_protected" warning at compile | The block was modified or replaced with a different version | Check FB4 family (P_Print, P_Print_R) and version stamp | Re-import the FB4 from the official SIMATIC S7 PtP library matching your CP340 firmware |
9. Verification Procedures
- Offline verification - byte-level: Open the message DB online, force the M9.1 flag, and read DBB24 / DBB36. The byte at DBB24 must be 0x31 (red) and DBB36 must be 0x30 (black) when M9.1 = TRUE; both must be 0x30 when M9.1 = FALSE.
-
Online verification - FB4 status: After the print trigger, monitor
MW 12. Expected transitions: 0x0F01 (job running) → 0x0F02 (job finishing) → 0x0000 (DONE set). Any 0x8xxx is a fault - consult Section 7. -
Printer verification: Issue a print, visually confirm the red section aligns with the M9.1 condition. On Epson-compatible devices, also send a test string
1B 40(ESC @) to reset the printer and ensure the escape parser is in a known state. -
Cable and protocol verification: Use a serial line analyser or a passive RS-232 tap to capture the bytes on the wire. Confirm:
- Bytes 0..2 of the stream are
0x1B 0x72 0x31(or 0x30) when red (or black) is requested. - The 8-bit framing matches what the printer is configured for (commonly 8N1).
- Handshake lines DTR/DSR toggle as expected.
- Bytes 0..2 of the stream are
- Long-run verification: Print at least 1000 lines with alternating M9.1 states to confirm that the CP340 buffer never overruns and that the printer does not drop into an undefined color state.
10. Field-Commissioning Notes and Caveats
- DB initial vs. actual values: S7-300 retains DBs across power cycles only if the DB is non-volatile (set the "Retain" property). The CP340 control character table is in the CP's flash and survives power-cycle, but the message DB does not - always initialise the selector bytes in OB100.
- Know_How_protect on FB4: The original Siemens FB4 P_Print is delivered without know-how protection but with a comment header. A version that shows up as "cracked" in the cross-reference has been edited and is a red flag for code quality.
- Comment loss: The source program lost its comments in the upload. Re-importing FB4 from the original library will not affect the application blocks (FC40, FC41) but will reset FB4 internal documentation.
- Legacy CP340 vs CP341: If the project is migrated to a CP341, FB4 is replaced by FB8 P_Print, and the parameter assignment path moves under "CP341 > Properties > PtP". The escape-sequence mechanism is identical.
- Character set: The CP340 transmits only 7-bit or 8-bit ASCII; non-Latin characters (e.g. Cyrillic) require the printer to be set to a matching codepage (PC437, PC850, etc.). Otherwise the ESC byte (0x1B) is still forwarded correctly, but surrounding text will be illegible.
11. Frequently Asked Questions
How do I print only part of a line in red on a Siemens CP340?
Inject the printer's red-select escape sequence (typically ESC r 1 = bytes 0x1B 0x72 0x31) at the start of the section and the black-select escape (ESC r 0 = 0x1B 0x72 0x30) at the end. Build the message in a DB and pass it to FB4 P_Print with the correct LEN. The CP340 forwards the ESC bytes literally only if the control-character table in HW Config permits it (default yes) or if the bytes are part of a Method-A user DB.
Which ASCII characters does the CP340 use for color control?
The CP340 itself does not interpret color; the printer does. For Epson ESC/P and ESC/POS ribbon-shift printers the standard sequence is ESC 27 dec, 'r' 114 dec, and a selector 48 dec ('0') for black or 49 dec ('1') for red. Different printer brands use different escape strings; always confirm with the vendor manual.
What is the difference between Method A and Method B for CP340 color printing?
Method A places the raw escape bytes (27, 114, 48/49) directly inside the message DB and lets the CP340 transmit them unchanged. Method B configures the CP340 control-character table to replace a symbolic character (e.g. 'R') with the escape sequence at transmission time. Method A is preferable when the color decision is data-driven (M9.1, comparison result); Method B is preferable for fixed message templates with constant formatting.
Why does my P_Print job return STATUS = 0x8010?
STATUS 0x8010 means the pointer DB does not exist or is too short. Check that DB_NO references a valid DB and that the byte length DBB_NO + LEN does not exceed the DB length. A common mistake is to grow the message text but forget to update LEN accordingly.
Can I use CP340 control characters for both color and double-width text in the same line?
Yes. Define multiple symbolic control characters in the CP340 parameter assignment (one per printer feature: R for red, B for bold, W for double-width, etc.) and use them inline. Each is independent and the CP340 expands them in stream order. Remember that each substitution adds bytes; LEN must reflect the expanded length, not the source-string length.