Configuring CP-340/CP-341 RS-232 ASCII for DTE Communication

David Krause11 min read
S7-300SiemensTutorial / How-to
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Overview

The SIMATIC CP 340 and CP 341 are point-to-point communication processors for the S7-300 / ET 200M backplane. They expose a single serial port (RS-232C, RS-422, or RS-485 depending on the order number) and offload serial framing, handshaking, and protocol handling from the CPU. When the downstream equipment is a generic Data Terminal Equipment (DTE) such as a barcode reader, scale, hand-held HMI, or weigh indicator, the loadable ASCII driver is the correct protocol choice. The CP 340/341 do not natively speak DF1, Allen-Bradley DH+, or any proprietary PLC protocol — those require a gateway (e.g. Prosoft, Anybus, Helmholz) or a discontinued loadable driver.

Supported loadable drivers on the CP 340/341:

  • ASCII (standard, ships with the module)
  • 3964(R) (standard)
  • RK 512 (standard on CP 341; option on CP 340)
  • Modbus master / slave, RTU + ASCII (loadable, order separately)
  • DF1 (loadable, discontinued — see notes below)
DF1 driver status: The loadable DF1 driver (formerly 6ES7 870-1AE01-0YA0) was withdrawn from the Siemens product portfolio. It is only usable on the older CP 341-1AH01 / -1BH01 / -1CH01 hardware (-1xH01 suffix) which is itself no longer available. New projects targeting DF1 must use an external PROFIBUS-to-DF1 or PROFINET-to-DF1 gateway (e.g. Prosoft PTQ-PDPMV1, Anybus Communicator).

Prerequisites

  • S7-300 station with CP 340 or CP 341 installed in the rack (slot 4-11), configured in HW Config with no address conflicts.
  • STEP 7 V5.5 + SP2 / SP3 (or TIA Portal with a SIMATIC S7-300 CP type that maps to the same module). The CP must be assigned a hardware type matching the physical module (e.g. 6ES7 340-1AH02-0AE0 for RS-232 CP 340).
  • Loadable ASCII driver installed: SIMATIC S7, CP 340/341 ASCII driver Vx.x, license on diskette/USB or activated through the Automation License Manager.
  • DTE device datasheet stating: baud rate, parity, data bits, stop bits, flow control (XON/XOFF, RTS/CTS, or none), terminator characters (CR, LF, CR+LF, none), and the byte order of any multi-byte values.
  • RS-232 cable, DB-9 male-to-female (or DB-25) wired as a DTE-to-DCE crossover. Siemens RS-232 CP modules implement a DCE physical interface, so a straight-through cable is normally used when the DTE is the “computer side”.

CP 340 vs CP 341 — Selecting the Module

Feature CP 340 CP 341
Order numbers (RS-232 variant) 6ES7 340-1AH02-0AE0 6ES7 341-1AH01-0AE0 (also -1AH02 for newer rev.)
Max baud rate (RS-232) 19.2 kbit/s (some variants 9.6 kbit/s) 76.8 kbit/s (firmware ≥ V1.0)
Max frame length send/receive 1024 bytes 4096 bytes
Number of FBs / FBs name FB 2 P_SEND / FB 3 P_RECV (CP 340 only) and the parameterized variants for ASCII FB 8 P_SEND / FB 9 P_RECV (with CP 341) and the same parameterized FBs used for 3964(R), RK 512, ASCII, Modbus
ASCII driver Yes (loadable) Yes (loadable, default)
Modbus driver No Yes (loadable)
Typical use today Migration projects, simple printer/scanner integration New installations requiring Modbus RTU/ASCII master or slave

For pure ASCII I/O to a DTE both modules behave almost identically. The difference that matters is frame length: if the DTE may burst more than 1024 bytes per transaction, use the CP 341.

RS-232 Pinout and Cable

The CP 340/341 RS-232 variants ship with a female DB-9 (D-subminiature) connector. The signal assignment is:

Pin Signal Direction (CP = DCE) Comment
2 RXD (received data) OUT Data from DTE to CP
3 TXD (transmitted data) IN Data from CP to DTE
4 DTR (data terminal ready) IN Hold DTE active
5 Signal GND Required
6 DSR (data set ready) OUT Indicates CP ready
7 RTS (request to send) IN Hardware handshake from DTE
8 CTS (clear to send) OUT Hardware handshake to DTE
1, 9 Shield / +5 V (DCD on some variants) Shield recommended on pin 1

If the DTE does not use hardware handshaking, leave the CTS/RTS loop connected (CTS grounded on the DTE side) and set the protocol parameter in the CP to “none” for both handshake directions. The cable length for RS-232 must not exceed 15 m (50 ft) at 19.2 kbit/s, less at higher baud rates; for DTE equipment installed in switchgear cabinets prefer a screened, twisted-pair cable (e.g. LiYCY 3×2×0.25 mm²).

STEP 7 Hardware Configuration

  1. Open SIMATIC Manager → your S7 project → HW Config.
  2. Insert the CP 340 or CP 341 from the hardware catalog (path: SIMATIC 300 → CP-300 → Point-to-Point → CP 340 / CP 341). Use the order number that matches the silk-screen on the module.
  3. Double-click the CP to open the Properties dialog. The diagnostic addresses are set here (default input 384, output 384). Do not overlap these with the CPU process-image or any other module.
  4. Switch to the Interface tab. Select RS-232C as the physical layer.
  5. Switch to the Protocol tab. Select ASCII driver. If the entry is greyed out, the driver was not installed — re-run the SIMATIC NET CD / install via Start → SIMATIC → SIMATIC NET → Communication Settings.
  6. Configure the protocol parameters (see next section).
  7. Save and compile (Station → Save and Compile). Download the hardware configuration to the CPU.

ASCII Protocol Parameters

The following parameters must match the DTE exactly. One mismatch is enough to corrupt every frame.

Parameter Typical values for DTE Notes
Baud rate 9600 (default), 19200, 38400, 57600, 115200 (CP 341 only) Check CP 340 max: 19.2 kbit/s on -1AH02
Data bits 8 (binary values), 7 (pure ASCII printable) 8N1 is the modern de-facto default
Parity None, Even, Odd Match the DTE; “None” requires 2 stop bits on many devices
Stop bits 1 (with parity), 2 (without parity)
Flow control None / XON-XOFF (DC1/DC3) / RTS-CTS Use XON-XOFF for legacy terminals, RTS-CTS for industrial devices
End-of-receive criterion Character count, fixed length, CR, LF, CR+LF, timeout, character + timeout Pick the one the DTE naturally produces
End-of-transmit character Append CR / LF / CR+LF / nothing Most hand-held terminals expect CR
Receive buffer 1…4096 bytes (CP 341), 1…1024 bytes (CP 340) Buffer the longest expected message
Wait time (echo suppression) 0…65535 ms Set to half the character time of the slowest expected gap
No 9-bit mode: the CP 340/341 ASCII driver is strictly 8-bit clean (or 7-bit with parity). 9-bit RS-485 multidrop addressing — common on some scales — is not supported by the ASCII driver; switch to the 3964(R) driver or use a converter.

PLC Programming — Function Blocks

Two FBs from the standard library are used. For the CP 340 the call is:

  • FB 2 — P_SEND — sends one frame
  • FB 3 — P_RECV — receives one frame

For the CP 341 the parameterized FBs are:

  • FB 8 — P_SEND
  • FB 9 — P_RECV

The FBs are multi-instance capable. Each instance DB belongs to the calling FB and holds the CP address, the send/receive DB, and the start/length parameters. A minimal “send integer to terminal” call in STL:

// One-shot send of a 12-byte ASCII message
CALL "P_SEND", DB30
  REQ    := M10.0           // rising edge triggers one send
  R      := M10.1           // abort / reset
  LADDR  := 384             // diagnostic address of the CP
  DB_NO  := 20              // data block containing the message
  DBB_NO := 0               // start byte in DB 20
  LEN    := 12              // bytes to send
  DONE   := M20.0           // set for one cycle on success
  ERROR  := M20.1           // set for one cycle on error
  STATUS := MW22            // error/status word

The receive side is identical, except EN_R must be set continuously and LEN is the maximum buffer size (the actual received length is reported in LEN on the rising edge of NDR).

Data Storage Location and Conversion

Send and receive buffers must reside in a DB of type BYTE / CHAR / STRING. Mixing WORD/INT directly as the buffer source works because STEP 7 just sends raw bytes, but it makes debugging and the DTE’s parser unhappy if the value needs to be human-readable.

Typical approaches:

  1. Raw binary, fixed length. An INT (-32768..32767) occupies 2 bytes. Use a DB declared as ARRAY[0..1] OF BYTE and copy the value with BLKMOV or MOVE after endian-swap (S7 is big-endian; most DTE devices are little-endian).
  2. ASCII decimal. Use FC 5 / FC 30 (STRING conversion) from the standard library: DI_STRNG, I_STRNG, R_STRNG convert INT, DINT, REAL into a STRING of decimal digits, which can then be sent as a contiguous block.
  3. Alphanumeric mixed string. Build a STRING DB manually: 'LOT=0012345' or similar tag/colon format the DTE expects. Concat the converted number and a header string with CONCAT (SCL) or string manipulation in STL.

Example SCL for “send the integer DB10.DBD0 preceded by 'VAL=' and terminated by CR+LF”:

// Build transmit buffer in DB20
DB20.VAL[0] := 'V'; DB20.VAL[1] := 'A'; DB20.VAL[2] := 'L'; DB20.VAL[3] := '=';
DB20.STR   := INT_TO_STRING(DB10.DBD0);
DB20.TAIL[0] := 16#0D; // CR
DB20.TAIL[1] := 16#0A; // LF

The total frame length fed to P_SEND is the header length + string length + 2 for CR/LF.

Verification

  1. Open the online CP 340/341 Diagnostics tool in SIMATIC Manager (right-click the CP → Module Information). Confirm: module OK, protocol loaded, no diagnostic interrupts, send/receive counters incrementing.
  2. Use the “Send/Receive Test” panel built into the diagnostic tool: type ASCII text, send it out of the CP port, loop it back with a TX-RX shorting plug, and verify the receive side returns the same bytes.
  3. Trigger P_SEND from a watch table with a 1-byte forced value; observe DONE and STATUS from the FB instance DB.
  4. Connect a serial line analyser (e.g. Wireshark with the appropriate serial tap, or a dedicated RS-232 protocol analyser) between CP and DTE to verify baud rate, framing, and that the CR/LF terminator is present.
  5. With a hyperterminal-style program on a PC emulating the DTE, confirm that the CP can both send and receive a round-trip message.

Troubleshooting Matrix

Symptom Likely cause Remedy
STATUS = 0x000A in P_SEND (request rejected) CP not in RUN / protocol not loaded Check CPU RUN, diagnostic buffer of CP, re-download HW config
STATUS = 0x0E0A (timeout on P_SEND) CTS held low, XOFF received, or hardware handshake mismatch Loop CTS to RTS at DTE side or change flow control to “None”
Send works, NDR never set on P_RECV End-of-receive criterion never met Add a receive timeout, or set end character to the one the DTE sends
Garbled characters Baud rate / parity / stop bits mismatch Match every parameter; verify with analyser that the line is 8N1 vs 7E1 etc.
Every other byte missing Half-duplex RS-485 inadvertently used on RS-232 pinout Confirm CP order number is the RS-232 variant (-1Axx)
Frame length > 1024 not received CP 340 limit Split messages or upgrade to CP 341
DF1 protocol requested DF1 driver discontinued, hardware CP 341-1xH01 EOL Use PROFIBUS-DP to DF1 gateway (Prosoft PTQ-PDPMV1 or anybus X-gateway)
Send OK, but DTE displays nothing DTE expects CR only, CP sends nothing Set “end of transmit character” in protocol parameters
CPU goes into STOP with SF LED on Wrong LADDR in P_SEND/P_RECV Match LADDR to the CP diagnostic address in HW Config

When to Use a Gateway Instead

If the DTE truly speaks DF1 (Allen-Bradley legacy), or any other protocol outside the CP’s set, the CP 340/341 is the wrong choice. A PROFIBUS-DP ↔ serial gateway (e.g. Prosoft PTQ-PDPMV1) or a PROFINET ↔ serial converter (e.g. Helmholz PN/CAN, Anybus X-gateway) is more robust and avoids loadable-driver licensing. The CP is then merely a PROFIBUS slave on the same rack, and the gateway handles all the protocol conversion transparently. For a DTE that already supports ASCII but the customer is being asked to avoid the CP 340/341 altogether (e.g. to reduce spare-parts stock), a CP 1243-1 / CP 1542SP with free-port ASCII over Ethernet can also be considered, although that introduces a different cabling topology.

Migration to a Current Platform

The CP 340 was phased out of new sales in 2014 and the CP 341 in 2016 (Siemens product discontinuation notices). Both remain available for spare-parts service for a defined window. For new machines the recommended replacements are:

  • ET 200SP CM PtP (6ES7 137-6AA00-0BA0) — free-port ASCII, 3964(R), Modbus, USS, on PROFINET, up to 250 messages/s.
  • S7-1200 CM 1241 (RS-232 or RS-485) — for compact machines using a single serial DTE.
  • CP 1542SP-1 — for full Modbus master / slave and security features.

The program migration is straightforward: P_SEND/P_RECV call signatures are similar, the diagnostic status codes map closely, and the same RS-232 cable (DB-9, DCE pinout) can be reused.

Does the Siemens CP 340 / CP 341 natively support DF1 protocol?

No. A loadable DF1 driver existed (order number 6ES7 870-1AE01-0YA0), but it has been discontinued and the matching CP 341-1xH01 hardware is no longer available. New DF1 projects must use an external PROFIBUS/PROFINET-to-DF1 gateway such as Prosoft PTQ-PDPMV1 or an Anybus Communicator.

Which protocols does the CP 340 / CP 341 support out of the box?

Both modules support ASCII, 3964(R), and (on CP 341) RK 512. The CP 341 additionally supports a loadable Modbus master and slave driver (RTU and ASCII variants). All drivers are configured through HW Config and a parameter dialog, not in the user program.

Where in the PLC is the data I want to send to the DTE stored?

In a DB of type BYTE, CHAR, or STRING. The FBs P_SEND and P_RECV take a DB number, a starting byte offset, and a length. To send a human-readable integer you must first convert it to a STRING (using FC 5 INT_TO_STRING or the SCL INT_TO_STRING function) and place that string into the DB.

What is the maximum cable length for RS-232 between the CP and a DTE?

15 m (50 ft) is the EIA-232 guaranteed limit at 19.2 kbit/s, dropping to roughly 3 m at 115 kbit/s. For longer distances, switch to RS-422/485 (CP 340/341 -1Bxx / -1Cxx variants) or convert to Ethernet with a serial-Ethernet gateway.

My ASCII driver is greyed out in HW Config — why?

The loadable driver was not installed on the programming device. Re-run the SIMATIC NET installation or copy the driver files from the SIMATIC NET CD into the STEP 7 directory, then restart HW Config. Confirm in Options → Install HW Updates that the latest Support Package is applied.

Can I send and receive simultaneously on the CP 340/341?

Yes. Call P_RECV cyclically with EN_R=TRUE and call P_SEND on demand. The CP arbitrates the two half-duplex directions internally; the user program only has to manage the FB instances and the data buffers.

Back to blog