CP340 Serial Communication with S7-300: Complete Setup Guide

David Krause19 min read
Serial CommunicationSiemensTutorial / 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 Siemens CP 340 communication processor turns an S7-300 CPU into a serial master or slave over RS 232C, 20 mA (TTY), or RS 422/485. The module sits in the central rack next to the CPU and exposes a logical base address to the STEP 7 program. Three integrated drivers — ASCII, 3964(R), and RK512 — let one module cover point-to-point printers, barcode scanners, scales, drives, weigh indicators, and similar field devices. The CP 340 is the entry-level PtP module for S7-300; the CP 341, CP 440, and CP 441 expand capability (loadable drivers, quantity structures, RK512 on industrial protocols), but the CP 340 is the right choice when ASCII telegrams with a few tens of bytes per cycle are sufficient and the budget is constrained.

This article walks through commissioning a CP 340 next to an S7-315-2DP or S7-314C-2DP, starting with hardware configuration in HW Config, the ASCII driver parameter set, the P_SEND and P_RCV function block calls, the data-block layout, and a field-proven troubleshooting matrix that covers the most common commissioning issues — including the classic "DBB32 keeps changing on every keystroke" behavior, the need for telegram framing, and the Modbus-versus-ASCII error-checking question raised when the first signs of bit corruption appear in a live machine.

Scope note. The CP 340 ships with three firmware-resident drivers. The ASCII driver is the universal workhorse. The 3964(R) driver uses the Siemens proprietary 3964/3964R handshake. The RK512 driver is the formalized 3964R + layer-7 block transfer. Pick the ASCII driver whenever the field device speaks printable characters with a defined terminator; pick 3964(R) when the device explicitly supports it for guaranteed in-order delivery; pick RK512 only when the partner is an S5 or S7 acting as a PtP peer using the RK512 protocol.

CP 340 Hardware Variants and Order Numbers

The CP 340 is sold as three sub-models, each with a different physical interface. The STEP 7 hardware catalog entry differs accordingly, so the order number must be matched before HW Config will accept the slot.

Order number (MLFB) Interface Typical use
6ES7340-1AH02-0AE0 RS 232C (V.24) PC, printer, Hyperterminal, laser length meter
6ES7340-1BH02-0AE0 20 mA TTY (active/passive) Legacy displays, panel meters, old S5 PtP peers
6ES7340-1CH02-0AE0 RS 422 / RS 485 (4-wire or 2-wire) Multi-drop weigh indicators, drives, multi-slave ASCII buses

All three variants use the same STEP 7 FB interface (P_SEND, P_RCV, P_PRINT) and the same HW Config parameter dialog. The only difference is the front-panel connector and the electrical driver. The RS 422/485 variant is software-selectable between 4-wire full-duplex and 2-wire half-duplex; in 2-wire mode the transmitter is automatically disabled during reception, so collisions are avoided when the bus discipline is polled-response.

Current-draw and slot constraints: the CP 340 draws 5 V / 360 mA from the backplane and occupies one slot. There are no buffer restrictions on the rack position, but the logical base address must be set inside the I/O address range (default 256..287 for slot 4, adjustable). Keep at least one slot between the CP 340 and any high-speed digital input module to avoid timing jitter on short telegrams.

Prerequisites

  1. STEP 7 V5.5 SP4 (or higher) installed on the engineering PC. TIA Portal does not configure the CP 340 — use the classic STEP 7 HW Config for the S7-300 family. The CP 340 manual is available on Siemens Industry Online Support.
  2. CPU 31x in the central rack (S7-312C through S7-319-3 PN/DP). The CP 340 is a PtP module, not a PROFIBUS slave, and does not require a DP master on the same station.
  3. RS 232C null-modem cable (DB9 female-to-female, pins 2↔3 crossed, 4↔5↔6 bridged, 7↔8 bridged) for a PC-to-CP 340 loopback. For RS 485, a 2-wire twisted pair with 120 Ω termination at each end.
  4. A spare instance DB for the P_SEND and P_RCV blocks (one DB per FB call) plus a working DB holding the actual telegram data.
  5. A terminal program on the PC (Hyperterminal, PuTTY, or Docklight) for round-trip testing before the field device is connected.

Step 1: Hardware Configuration in HW Config

Open the SIMATIC 300 station in HW Config. Drag the CP 340 from the hardware catalog (path: SIMATIC 300 > CP-300 > PtP) into the slot next to the CPU. Double-click the module to open the properties dialog. On the Addresses tab, set the logical base address — the same address that the LADDR input of P_SEND and P_RCV will reference (W#16#100 is a common choice for slot 4, but STEP 7 auto-assigns 256 by default).

On the PtP Parameters tab, choose the driver:

  • ASCII — for printable-character protocols, polled weigh indicators, scanners, simple instruments, modems in AT command mode.
  • 3964(R) — for Siemens-native handshake to S5/S7 peers and drives that implement 3964R.
  • RK512 — for layer-7 RK512 block transfer.

Click Properties next to the driver selection to open the interface definition. Save and compile the station, then download the hardware configuration to the CPU. The CP 340 only becomes active after the parameterized data record has been received from the CPU on startup; if a CP 340 is hot-swapped or its parameters are changed online, a STOP-RUN transition on the CPU (or a CP 340 power-cycle) is required to load the new parameters.

Step 2: ASCII Protocol Parameterization

The ASCII driver parameter screen is the same dialog whether the physical layer is RS 232, TTY, or RS 485. The dialog is divided into four sub-tabs: Interface, Character Delay / XON-XOFF, Data Flow Control, and Telegram / Receive Buffer. The first two tabs are mandatory; the others are application-specific.

Parameter Typical value Notes
Baud rate 9600 (also 1200, 2400, 4800, 19200, 38400) Must match the partner. 38400 is the upper limit for the RS 485 variant without isolation considerations.
Data bits 8 Use 7 only for true 7-bit ASCII (rare). 8 is required for any binary payload or extended character set.
Stop bits 1 2 is occasionally needed for legacy TTY devices with timing drift.
Parity None Even or Odd for legacy devices. None is the default for most modern ASCII instruments.
Data flow control None XON/XOFF (software) or RTS/CTS (hardware) when the partner requires it.
XON character DC1 (0x11) Only relevant if XON/XOFF is enabled.
XOFF character DC3 (0x13) Only relevant if XON/XOFF is enabled.
End-of-receive timeout (ms) 4 character times, e.g. 4 × (1/9600 × 10) ≈ 4 ms Defines the gap that closes a telegram. Critical for partners that send without an explicit terminator.
End-of-text character 1 / 2 0x0D (CR) for text-mode instruments, none for continuous streams Up to two characters can be defined. Both are stripped from the data placed into the receive DB.
Receive buffer size (bytes) 32, 64, 128, 256, 512, 1024 Selects the FIFO size on the CP 340. The CPU-side receive DB must be at least this large.
Why the receive buffer matters. The CP 340 stores incoming characters in an on-board FIFO. The P_RCV call only copies from that FIFO into the CPU data block. If the receive buffer is too small for the partner's longest telegram, the CP 340 silently truncates the message and the CPU never sees the trailing characters. Always set the receive buffer to the maximum expected telegram length plus 10 %.

Step 3: Creating the Send/Receive Data Blocks

Two DBs are required for the simplest configuration: one instance DB for each FB, and one working DB for the actual telegram payload. The P_SEND instance DB is created by STEP 7 when the FB is inserted; the same applies to P_RCV. The working DB can be a standard DB or a DB-of-CHAR-array, which is the strongly recommended pattern.

Recommended DB layout for receive:

DATA_BLOCK DB10
TITLE = Receive buffer for CP 340
VERSION : 0.1
  STRUCT
      RxBuf : ARRAY[1..256] OF CHAR;   // up to 256 characters
      RxLen : INT;                       // actual byte count, set by P_RCV
  END_STRUCT;
END_DATA_BLOCK

Declaring the receive area as ARRAY OF CHAR lets you view the buffer with a VAT in character format, which makes it trivial to verify that "STXPACR"-style telegrams are being framed correctly. Using BYTE arrays forces the engineer to mentally convert 0x50 0x41 0x0D into the letter string "PA<CR>" — a significant source of commissioning errors.

For the send side, an identically structured DB of CHAR values is recommended. The telegram to the weigh indicator in the source thread (<STX>PA<CR>, or for address 1 the byte sequence 02 50 41 0D) can be preloaded as default values so the program does not have to assemble the string at runtime.

Step 4: P_SEND Programming Example

The P_SEND FB (FB 2 in the standard CP 340 library) is the only sanctioned way to push a telegram out of the CP 340. Calling the underlying SFCs directly is unsupported and the module will not respond. The call signature for STEP 7 V5.5 is:

CALL "P_SEND" , "DB_P_SEND"
     REQ    :=#Send_Trigger      // BOOL, edge-triggered start pulse
     R      :=#Send_Abort         // BOOL, abort a pending transmission
     LADDR  :=#CP340_Addr         // WORD, logical base address from HW Config
     DB_NO  :=#Datablock          // INT,  number of the working DB
     DBB_NO :=#Send_Byte_Offset   // INT,  byte offset of the data in that DB
     LEN    :=#Send_n_Bytes       // INT,  number of bytes to send
     DONE   :=#Send_Done          // BOOL, request completed without error
     ERROR  :=#Send_Error         // BOOL, request completed with error
     STATUS :=#Send_Status;       // WORD, detailed result (see status table)

Operational rules:

  • REQ must be a positive edge. A level-held REQ re-triggers the FB on every scan and overruns the FIFO — symptom is intermittent lost characters on the wire.
  • LEN counts bytes, not characters. For "STX PA CR" set LEN = 4.
  • DB_NO and DBB_NO are both required; CP 340 cannot read from a bit-mapped area, only from a byte-aligned DB starting at any offset.
  • DONE and ERROR are mutually exclusive within one scan. If both come back false and STATUS is non-zero, the FB is still busy.

Step 5: P_RCV Programming Example

The P_RCV FB (FB 3) is the symmetric counterpart for incoming data. Set EN_R to TRUE for continuous reception, R to FALSE under normal conditions (a TRUE on R flushes the FIFO — use only on commissioning or protocol reset).

CALL FB 3 , DB23                  // P_RCV instance
     EN_R  :=M1.0                  // continuous receive enable
     R     :=M1.1                  // one-shot reset, normally FALSE
     LADDR :=MW21                  // CP 340 logical base address
     DB_NO :=MW60                  // receive working DB number
     DBB_NO:=MW62                  // receive byte offset in that DB
     NDR   :=M29.0                 // new data received this scan
     ERROR :=M29.1                 // receive error this scan
     LEN   :=MW64                  // actual bytes copied into the DB
     STATUS:=MW30;                 // detailed result code

EN_R = TRUE is the normal pattern for polled ASCII traffic. Setting EN_R = FALSE closes the receive window; setting R = TRUE clears any partially received telegram. LEN is the number of valid bytes the CPU should interpret, not the buffer size. Always re-initialize the receive buffer in the working DB on a R pulse; the CP 340 does not guarantee zero-padding of the trailing bytes.

Critical implementation note from field experience. The '#Send.Abort' and '#receive.Abort' inputs must both be cleared. A stranded abort bit prevents the FB from accepting a new REQ and the symptom is "first send works, subsequent sends hang with STATUS = 16#1E0F". Tie both abort bits to a single reset coil that is pulsed at startup and on any fatal error.

Step 6: Commissioning with Hyperterminal

  1. Connect the CP 340 to the PC COM port with a null-modem cable. Power up the S7-300 station and ensure the CP 340 SF LED is off (solid green RDY indicates parameterized and ready).
  2. On the PC, open Hyperterminal (or PuTTY serial) at the same baud rate, data bits, stop bits, and parity configured in HW Config. Disable any local echo on the PC side so the transmitted characters are not mixed with the local echo.
  3. Trigger a P_SEND in the CPU (set REQ for one scan via a force table or a watch table). The expected ASCII string should appear on the Hyperterminal window. If the characters appear doubled (for example GERRYGERRYGERRY as reported in the source thread), the local echo on the PC is on, or the CP 340 is in a half-duplex mode where its own transmitter is being looped back. Disable the local echo on the terminal program first.
  4. Type characters in Hyperterminal. The receive buffer in the CPU should populate. Monitor DB10.DBB32 (or the first byte of the RxBuf array) in a VAT in Character format. The displayed character should match the key pressed. If DBB32 changes on every keystroke as in the source case, the telegram is not being framed — proceed to the next section.
  5. Once the round-trip works, replace the Hyperterminal with the real field device (laser length meter, weigh indicator, drive) and repeat the verification. Do not skip the Hyperterminal step; it isolates the PC, the cable, and the CP 340 as the three possible fault sources before adding the field device into the loop.

Telegram Framing Rules

The CP 340 ASCII driver closes a received telegram and posts NDR to the CPU on one of three events:

  1. An end-of-text character (1 or 2) configured in the driver parameters is received. Up to two characters can be defined; both are stripped from the data buffer.
  2. The configured end-of-receive timeout expires after the last character. The timeout is specified in milliseconds and is typically set to 4 character times. For 9600 8N1, one character time is ~1.04 ms, so a 4 ms timeout is the default.
  3. The configured receive buffer is full. In this case, the telegram is posted immediately and any further incoming characters are buffered for the next P_RCV call (subject to FIFO overflow at 1024 bytes).

For polled ASCII indicators (the source thread case, <STX>PA<CR> for poll, response ending in <CR>), framing on CR is correct. For continuous-output instruments (laser length meter in the source thread), the meter has no terminator and the telegram is essentially the entire stream. In that case, the only working strategy is the end-of-receive timeout. If the timeout is set too short, characters are split across multiple P_RCV calls and the telegram must be reassembled in the CPU. If the timeout is set too long, the meter data appears delayed by one telegram length at the application layer.

The "DBB32 keeps changing" symptom. When a polled ASCII instrument is connected to a CP 340 and the receive DBB seems to flip on every character, three root causes are responsible in 95 % of cases:
  1. No terminator configured — the CP 340 is closing the telegram on every single character because no end-of-text character and no timeout were parameterized. Add the end-of-text character (usually 0x0D) and increase the end-of-receive timeout to 4 character times.
  2. Receive buffer too small — the CP 340 fills the FIFO, posts a partial telegram, and never sees the end-of-text character. Increase the receive buffer in HW Config to the maximum expected telegram length.
  3. Continuous-output instrument — the partner sends without any terminator. Re-architect the receive side around a time-based reassembly buffer in the CPU, or switch to a protocol that includes length headers.

Error Checking: ASCII vs. Modbus

Plain ASCII has no integrity check. A 4-character string PA<CR> corrupted by a single EMI-induced bit flip becomes any other 4-character string with the same probability — and the CPU will accept it. For non-critical displays this is acceptable; for any value that drives a downstream action (a shutdown on a temperature reading, a brake release on a weigh value, a setpoint change to a drive), plain ASCII is unsafe.

Protocol Error check Overhead CPU implementation When to use
ASCII (raw) None 0 bytes Trivial — P_SEND/P_RCV only Non-critical display / logging
ASCII + checksum (XOR or sum) 1 byte ~3 % for 32-byte telegrams Append on send, strip + verify on receive Low-cost field instrumentation
Modbus ASCII LRC (1 byte, 2's complement of byte-sum) 1 byte + : + CRLF framing Manual LRC compute in FC Mixed-vendor multi-drop RS 485
Modbus RTU CRC-16 (2 bytes, polynomial 0xA001) 2 bytes + 3.5-char silent gap Standard CRC table; CP 340 cannot do RTU timing reliably — use CP 341 with Modbus master or slave loadable driver Industrial multi-drop, critical control

For a 5-indicator RS 485 bus where one wrong bit can command a frequency drive to ramp to 1000 °C, switch to Modbus ASCII or Modbus RTU. Modbus ASCII keeps the printable framing (so it is easy to debug in a terminal) and adds an LRC byte that catches every single-bit error. Modbus RTU is binary and more efficient but requires the loadable Modbus driver on a CP 341 or a custom CRC implementation in the CPU; the CP 340 can in principle do Modbus RTU but the strict 3.5-character inter-frame gap is hard to honor on a general-purpose ASCII driver.

Troubleshooting Matrix

Symptom Probable cause Fix
CP 340 SF LED steady red No parameterized data record loaded from CPU, or driver parameter error Recompile HW Config, download to CPU, perform STOP→RUN; verify the logical base address matches LADDR in the FB call
STATUS = 16#1E0F on P_SEND Abort bit latched from a previous failed send; or no receive resource available Pulse the abort bit, check FB instance DB is not overwritten by other code
STATUS = 16#1E01 / 16#1E02 Parameter error: DB_NO, DBB_NO, or LEN outside the DB area Verify DBB_NO + LEN ≤ DB length; verify LADDR matches the configured logical base address
DBB32 changes on every keystroke (PC test) No telegram framing configured Add end-of-text character (0x0D for typical ASCII) and a 4-character-time receive timeout
DBB32 changes on every meter reading (field device) Field device sends without terminator and timeout is too short Increase end-of-receive timeout to 4–10 character times; verify the meter can be switched to polled mode if random access is required
Hyperterminal shows doubled characters Local echo on the terminal program Disable local echo in Hyperterminal / PuTTY session configuration
Some characters lost on long telegrams Receive buffer too small Increase receive buffer in HW Config to expected maximum telegram length + 10 %
First send works, second send hangs Send.Abort bit latched Pulse the abort bit on startup and on any error; verify the FB instance DB is not multi-written
Spurious values in received data (e.g. 1000 instead of 20) No error checking on plain ASCII, EMI on RS 485 bus Switch to Modbus ASCII (LRC) or Modbus RTU (CRC-16); add 120 Ω termination at both ends of the RS 485 bus; check shield grounding
No NDR ever set, but bytes are visible in DB EN_R = FALSE or R = TRUE on the P_RCV call Set EN_R continuously TRUE; tie R to a one-shot reset pulse at startup

Status Code Reference (Selected)

STATUS (hex) Meaning Action
0000 No error, request completed None
1E01 Parameter error: DB_NO out of range Verify DB number exists in S7 program
1E02 Parameter error: DBB_NO out of range Verify DBB_NO + LEN ≤ DB length
1E05 Receive buffer full on CP 340 Increase receive buffer size in HW Config
1E0F No resources (FB busy, abort active) Check abort bits, wait for DONE before next REQ
1F01 / 1F02 Hardware fault / parameter record missing Recompile HW Config, download, STOP→RUN

The full status code list is in the CP 340 manual available on Siemens Industry Online Support.

Verification

After commissioning, validate the link end-to-end with the following checklist:

  1. CP 340 RDY LED solid green, SF LED off.
  2. Send a known ASCII string from P_SEND and verify the byte count in LEN matches the expected telegram length. Hyperterminal should display the same string.
  3. Trigger a P_RCV from the field device and verify NDR is set, LEN reflects the actual received length, and the RxBuf array contains the expected characters in CHAR view.
  4. Toggle the abort bit and verify the next REQ on P_SEND is accepted (DONE within one scan, STATUS = 16#0000).
  5. Power-cycle the S7-300 station and confirm the CP 340 re-parameterizes automatically (RDY LED green within 5 s of CPU RUN).
  6. For RS 485 multi-drop: verify 120 Ω termination at both physical ends, shield grounded at one end only, and no more than 32 nodes on a single segment (CP 340 RS 485 driver limit).

What is the difference between CP 340, CP 341, and CP 341-1?

The CP 340 is the entry-level ASCII / 3964(R) / RK512 PtP processor with three fixed driver slots and a maximum of 1024-byte receive buffer. The CP 341 adds loadable drivers (Modbus master/slave, USS, GE Fanuc, others) and larger buffers. The CP 441-1 / CP 441-2 extend to 32 simultaneous protocols and industrial-strength quantities. Use CP 340 for simple ASCII telegrams under 256 bytes; switch to CP 341 the moment Modbus RTU, Modbus TCP-to-serial gateway, or USS to a Siemens drive is required.

Can the CP 340 speak Modbus RTU natively?

No. The CP 340 ASCII driver does not honor the strict 3.5-character inter-frame silent gap that Modbus RTU requires for reliable frame detection, and the CP 340 has no Modbus CRC-16 implementation. For Modbus RTU on an S7-300, use the CP 341 with the loadable Modbus master or Modbus slave driver, or implement Modbus RTU in the CPU on top of a generic CP 340 ASCII driver with manual CRC computation — feasible but not recommended for production.

Why does the receive DBB keep changing on every received character?

Almost always because the CP 340 has no way to identify the end of the telegram. Either the field device does not send the configured end-of-text character (e.g. a continuous-output meter that streams measurements without a terminator), or the receive buffer is too small and the CP 340 closes the telegram when the FIFO is full. Configure an end-of-text character (typically 0x0D for CR-terminated ASCII), set the end-of-receive timeout to 4 character times, and ensure the receive buffer in HW Config is at least the expected maximum telegram length plus 10 %.

What is the maximum number of nodes on a CP 340 RS 485 segment?

The CP 340 RS 422/485 variant (6ES7340-1CH02-0AE0) supports up to 32 nodes per segment in 2-wire half-duplex mode (the most common multi-drop configuration). For larger networks, install a repeater. The bus must be terminated with 120 Ω at both physical ends, and the shield should be grounded at one end only to avoid ground loops. For new installations with more than 16 nodes, consider migrating to PROFIBUS DP or PROFINET — RS 485 multi-drop with the CP 340 is appropriate for legacy weigh indicators and small drive clusters, not for greenfield designs.

How do I move from STEP 7 to TIA Portal with a CP 340?

The CP 340 is supported in TIA Portal V13 and later, but only for legacy migration projects. The device is configured in the device view the same way it was in HW Config: drag the CP 340 from the catalog, set the logical base address, parameterize the ASCII driver, and use the same P_SEND and P_RCV FBs from the global library. New projects should target the CP 341, ET 200SP PTP module, or S7-1500 communication modules, all of which are fully native in TIA Portal. The legacy CP 340 remains in service for the entire lifecycle of S7-300 systems and Siemens continues to ship spares — the module is not end-of-life.

Back to blog