CP341 ASCII Protocol Configuration: S7-300 Modbus TCP Setup

David Krause16 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

System Overview and Problem Statement

The referenced application is a railway Remote Terminal Unit (RTU) built on a Siemens S7-300 station. The S7-300 contains a CPU 315-2 PN/DP, digital input module SM321 (32x), digital output module SM322 (32x), and a point-to-point communication module CP341. The CP341 is wired over RS-232 to an end device called Ebilock 850 (an interlocking controller), and to a Relay Interlocking (RI) end device through a separate interface. Upstream, an external "RTUCI task" communicates with the S7-300 over Ethernet using Modbus TCP.

The engineering questions that drive this guide are:

  1. Does the CP341 "ASCII driver" convert every binary byte (for example 0x67 0x89) into its printable ASCII representation (0x36 0x37 0x38 0x39), or does it transmit the raw byte stream?
  2. Can the CPU 315-2 PN/DP's built-in PN interface run Modbus TCP at the same time as the CP341 runs an ASCII protocol toward Ebilock 850?
  3. Can the same CPU maintain two distinct TCP connections from RTUCI (one to the Ebilock station object, one to the RI station object) using the same IP and different ports?
  4. Are standard Modbus TCP ports such as 502 and 3050 suitable for distinguishing the two connections?

The remainder of this article documents the correct answers, the underlying S7-300 / CP341 mechanisms, and the recommended STEP 7 V5.x configuration steps.

CP341 Hardware Variants and Order Numbers

The CP341 is a single-channel serial module for the S7-300 backplane. Three physical-interface variants are sold; they differ only at the sub-D connector and at the driver firmware loaded at parameterization time. The variants commonly referenced in field installations are:

Order Number (MLFB) Interface Typical Use
6ES7341-1AH02-0AE0 RS-232-C (V.24) Point-to-point, modem, Ebilock 850 direct cable
6ES7341-1BH02-0AE0 RS-422 / RS-485 (X27) Multi-drop RS-485 networks, RI units
6ES7341-1CH02-0AE0 RS-485 (isolated) Long-distance RS-485 trunks

Each variant is supplied with parameterization diskettes ("CP 341 Point-to-Point Communication, Parameter assignment"). The diskette must be installed once on the STEP 7 V5.x PG so that the CP341 properties dialog can be opened in HW Config. In the System Data area of HW Config, the CP341 occupies a 32-byte I/O area; the standard start address is 256 (decimal). The CP341's status and control bytes are mapped within that 32-byte window and are managed automatically by the CP341 communication FBs.

What the "ASCII Driver" Actually Does

Siemens uses the label "ASCII driver" for a free-form, character-transparent serial protocol on the CP341/CP340. It is not an ASCII-text converter. When the ASCII driver is selected with an 8-bit data frame, the CP341 transmits and receives any byte value from 0x00 through 0xFF without recoding it. The "ASCII" label refers to the framing model: characters are sent and received as a stream delimited by configurable end-of-frame conditions, in the same spirit as a generic terminal.

For a request such as 0x67 0x89, the CP341 puts exactly two bytes on the wire. The receiver observes 0x67 0x89, not the text representation "67 89" (which would be 0x36 0x37 0x20 0x38 0x39).

Critical: If the Ebilock 850 protocol truly requires printable text framing, the host application must perform the binary-to-ASCII conversion in the S7 user program before calling P_SEND. The CP341 itself never performs that conversion. Selecting "ASCII driver" in the parameterization dialog with 8 data bits is the correct setting for binary-transparent byte transfer.

End-of-Frame Conditions for the ASCII Driver

Because the ASCII driver has no embedded length field, the end of a frame is detected by one or more of the following configurable conditions:

Condition Typical Setting for Ebilock 850 Notes
Character delay time (Tic) 4 ms If the line is idle for Tic, the receive buffer is closed and the FB returns data
Fixed string terminator (1- or 2-character) Application-specific For example 0x0D 0x0A or a CRC tail byte
Fixed message length (receive only) Application-specific Used when the sender always emits a constant-length frame
Receive buffer full (512 bytes) Default safeguard CP341 receive FIFO limit

Siemens' official application note "CP340/CP341 Multi-Station polling based on ASCII driver protocol" lists a representative configuration for similar byte-oriented masters: 9600 bit/s, 8 data bits, no parity, character delay 4 ms. The same note documents the start address 256 (decimal) for the CP341 in HW Config and the use of P_RCV / P_SEND FBs with per-station instance DBs. Reference: Siemens Support Entry 88359826

Hardware Configuration in STEP 7 V5.x

  1. Open the S7-300 station in SIMATIC Manager and launch HW Config.
  2. Insert the CP341 at the desired slot. The S7-300 bus allows a CP341 in slots 4 through 11 of any rack (subject to power-budget rules). Confirm the start address (default 256 decimal); note the diagnostic address if interrupts are used.
  3. Double-click the CP341 to open the Properties dialog. If the parameterization diskette is installed, the "Parameter Assignment" tab exposes the protocol selector.
  4. Select the protocol: ASCII driver. Choose the physical interface matching the variant (RS-232, RS-422, or RS-485).
  5. Set baud rate, data bits, parity, and stop bits to match the Ebilock 850. Common values: 9600 / 8 / N / 1 or 19200 / 8 / E / 1. If the protocol requires hardware handshake, enable RTS/CTS; otherwise disable to allow three-wire RS-232 (TxD, RxD, GND).
  6. Set the end-of-frame condition. For Ebilock 850 use a character delay of 3-5 ms and, if the protocol uses a terminator, enter the 1- or 2-character terminator string.
  7. Compile and download the hardware configuration. After download, the CP341 is in data-transfer mode; the selected protocol is stored persistently in the module's flash and is re-applied on every cold restart.

Send and Receive Function Blocks for the ASCII Driver

For the ASCII driver on CP341/CP340, the standard communication FBs in STEP 7 V5.x are:

FB Number Symbolic Name Purpose Use on S7-300
FB 7 P_PRON Not used here (printer protocol) -
FB 8 P_SEND Send a buffer to CP341 OB1 / OB35
FB 9 P_RCV Receive a buffer from CP341 OB1 / OB35
FB 10 P_PRINT Not used here -
Note: The exact FB numbers depend on the library version installed on the PG. The library "SIMATIC NET CP - Point-to-Point" (or "CP340/CP341") provides the ASCII variants. Always read the FB header for the version, since later library versions (V2.x and V3.x) consolidate send and receive into a single FB with a RECV/SEND switch on REQ/EN_R.

Each call of P_SEND / P_RCV requires:

  • An instance DB (one DB per logical link, in the user program; the CP341 does not occupy a DB itself).
  • The module start address from HW Config (e.g. 256 / 0x100).
  • A pointer to a source/destination buffer in the S7 data area (any DB, bit memory, or process I/O area).

Minimum FB call shape (illustrative):

// ASCII send (ST)
CALL "P_SEND" , DB_ASCII_SEND
  REQ     := TRUE            // edge-triggered
  LADDR   := 256             // CP341 start address
  DB_NO   := 100             // user DB containing payload
  DBB_NO  := 0               // start byte offset in DB 100
  LEN     := 2               // number of bytes
  DONE    := sendDone
  ERROR   := sendErr
  STATUS  := sendStatus;

// ASCII receive (ST)
CALL "P_RCV" , DB_ASCII_RCV
  EN_R    := TRUE            // cyclic enable
  LADDR   := 256
  DB_NO   := 110
  DBB_NO  := 0
  NDR     := rcvNew
  ERROR   := rcvErr
  STATUS  := rcvStatus
  LEN     := rcvLen;

For the binary payload 0x67 0x89, declare DB100 with two bytes, initialize them with B#16#67 and B#16#89, and call P_SEND with LEN := 2. The wire output is two raw bytes; no expansion to "6789" occurs.

Concurrent Modbus TCP on the CPU 315-2 PN/DP

The CPU 315-2 PN/DP integrates a PROFINET interface that supports open TCP/IP communication. With a Modbus TCP library loaded, the same interface can act as a Modbus TCP server (slave) toward the RTUCI task while the CP341 runs an ASCII protocol to Ebilock 850. The two protocol stacks are independent and time-multiplexed by the CPU's operating system; they do not contend for any shared resource because the PN interface and the CP341 are physically separate modules.

Siemens provides the library "Modbus TCP for S7-300/400" with order number 2XV9 450-1MB02. The library contains FBs for Modbus TCP client and server roles, plus a sample project. The library is delivered as a STEP 7 V5.x archive and installs into the S7 program as a master copy library.

Library Order Number Target Roles
Modbus/TCP for S7-300/400 2XV9 450-1MB02 S7-300 / S7-400 with PN or CP Client (Master) and Server (Slave) FBs

Typical integration pattern:

  1. Install the library, then copy the FB blocks and the UDTs to the S7 program.
  2. Open NetPro and create a "TCP connection" to the RTUCI task IP address. Note the local and remote port numbers, and the automatically assigned connection ID (e.g. 1 for the Ebilock connection, 2 for the RI connection).
  3. For each TCP connection, instantiate one copy of the Modbus TCP client or server FB into its own instance DB. The library uses separate instance DBs to keep the connection state independent.
  4. Map the relevant Modbus registers to the user data that is also exchanged with the Ebilock 850 station object through the CP341 ASCII channel.

Multiple TCP Connections on the Same CPU IP Address

The S7-300 PN stack supports multiple concurrent TCP connections to the same peer IP address, distinguished by the port tuple. A TCP socket is uniquely identified by the 5-tuple {protocol, src IP, src port, dst IP, dst port}. If the RTUCI task opens one socket to the S7 CPU on port 502 and a second socket to the same IP on port 3050, the operating system dispatches the data to two independent connection structures in the S7.

To realize this in the S7 program, the user must instantiate the Modbus TCP FB twice and assign each instance its own connection ID and instance DB. The library "Modbus/TCP for S7-300/400" (2XV9 450-1MB02) is designed for exactly this use case; the documentation explicitly supports the use of different port numbers and connection IDs for parallel communications.

Field-proven caveat: The S7-300 CPU has a hard limit on the number of open TCP/ISO-on-TCP connections. For CPU 315-2 PN/DP, the maximum is 16 connections total in the integrated PROFINET interface. Two Modbus TCP connections (one per station) consume only two of those slots, leaving 14 for HMI, programming, OPC UA, and additional RTUCI links. Plan headroom for engineering access.

Port Number Selection: 502 and 3050

Port 502 is the IANA-assigned Modbus TCP port. Using port 502 for the connection to the Ebilock station object is the standard choice and is recognized by every Modbus diagnostic tool, including the Modbus Poll client, the Modbus Scanner utility, and Wireshark's Modbus dissector (registered on TCP/502). Port 3050 is in the user/private range; it is not a standard Modbus TCP port but is acceptable for a non-standard secondary service, provided both endpoints agree on the port.

Recommended mapping for this RTU:

Connection Local IP (CPU) Local Port Remote IP (RTUCI) Remote Port Connection ID (NetPro) Modbus FB Instance DB
Ebilock 850 station object 192.168.10.20 502 (server-side) 192.168.10.5 51000 (RTUCI ephemeral) 1 DB200
RI station object 192.168.10.20 3050 (server-side) 192.168.10.5 51001 (RTUCI ephemeral) 2 DB201

The RTUCI task must open two sockets: one to 192.168.10.20:502 and one to 192.168.10.20:3050. Inside the S7 program the two Modbus TCP server FBs listen on their assigned local ports and write to independent data blocks. Application code can route requests to the Ebilock 850 ASCII channel (CP341) by reading/writing DB200, and to the RI channel by reading/writing DB201. There is no cross-talk because each FB instance owns its connection state.

Data Flow and Protocol Boundaries

The end-to-end data path for a typical Ebilock 850 request becomes:

  1. RTUCI task writes a Modbus TCP request to S7 CPU on port 502.
  2. The Modbus TCP server FB (instance DB200) decodes the PDU and exposes the function code and register payload in the user DB.
  3. S7 user code maps the requested Modbus register(s) into a CP341 ASCII frame. The user code is responsible for the Ebilock 850 application protocol (any register-to-byte conversion, any byte-level CRC, any sequence numbering).
  4. P_SEND hands the frame to the CP341. The CP341 transmits the raw bytes to Ebilock 850 over RS-232.
  5. Ebilock 850 returns a response frame. P_RCV collects it, and the S7 user code parses the bytes back into Modbus register values.
  6. The Modbus TCP server FB replies to the RTUCI task with the corresponding Modbus PDU.

The RI station object follows the same path on connection 2 / port 3050 / instance DB201, with the RI's own serial protocol over a separate CP341 or a second point-to-point module.

Configuration Checklist

Item Setting / Value Verified
CPU 315-2 PN/DP firmware V3.3.x or later (recommended for Modbus TCP library compatibility) [ ]
CP341 order number 6ES7341-1AH02-0AE0 (RS-232) or per physical interface [ ]
CP341 start address 256 decimal (default) [ ]
CP341 protocol ASCII driver [ ]
Data bits / parity / stop 8 / N / 1 (or as specified by Ebilock 850) [ ]
End-of-frame Character delay 4 ms (or terminator string) [ ]
Modbus TCP library 2XV9 450-1MB02 [ ]
Modbus connection 1 (Ebilock) Port 502, Conn ID 1, Instance DB200 [ ]
Modbus connection 2 (RI) Port 3050, Conn ID 2, Instance DB201 [ ]
TCP connection budget ≤ 14 of 16 used (CPU limit) [ ]
Binary transparency test Send 0x67 0x89, capture 0x67 0x89 on the wire [ ]

Verification and Diagnostics

  1. Wire capture. Use a serial tap (e.g. an RS-232 line monitor or a logic analyzer on a break-out) to confirm that the bytes transmitted by the CP341 match the bytes written by P_SEND. For 0x67 0x89 the scope must show exactly two bytes, not five.
  2. CP341 diagnostics. Open the CP341 online in HW Config or use the "CP341 - Diagnostics" tool from the parameterization diskette. Check the diagnostic buffer for framing errors, overrun, or parity errors.
  3. Modbus TCP round-trip. From the RTUCI host, run a Modbus client against port 502 (Ebilock) and port 3050 (RI) and read a known register that mirrors a status flag. Verify that the value matches the value written to the Ebilock 850 input registers.
  4. Wireshark verification. Capture traffic on the PROFINET segment and apply the Modbus dissector (it auto-detects port 502). For port 3050, add a decode-as rule mapping TCP/3050 to the Modbus dissector. Each connection must show independent TCP streams with independent transaction IDs.
  5. Connection budget. In NetPro, confirm that the two Modbus connections are listed in the connection table and that the connection state of each toggles to "established" once the RTUCI task opens the socket.

Troubleshooting Matrix

Symptom Likely Cause Remedy
CP341 transmits 0x36 0x37 0x38 0x39 instead of 0x67 0x89 User code pre-converted the byte to its ASCII text representation before calling P_SEND Pass raw bytes; remove any ITB or string-conversion logic in the S7 user program
CP341 transmits raw 0x67 0x89 but the Ebilock 850 expects printable text Wrong protocol assumption; Ebilock 850 is ASCII-text based, not binary Insert explicit byte-to-text conversion in the user program before P_SEND
CP341 never returns data to P_RCV End-of-frame condition never satisfied Lower character delay to 3-5 ms, or configure the fixed terminator that the Ebilock 850 emits
Modbus TCP server does not respond on port 502 Connection ID not assigned in NetPro, or FB called with wrong ID Verify the NetPro connection ID matches the FB's ID input; recompile and download
Only one of the two RTUCI connections establishes Both connections share the same connection ID or the same instance DB Assign unique connection IDs (1 and 2) and unique instance DBs (DB200 and DB201)
Intermittent CRC errors on Ebilock 850 Baud rate mismatch or RS-232 cable too long without termination Confirm baud rate in CP341 properties; keep RS-232 cable under 15 m; consider RS-422 variant for longer runs
Modbus TCP timeouts from RTUCI S7 user OB1 cycle too long, or PN interface overloaded Move the Modbus FBs to OB35 (cyclic interrupt) and reduce cycle time
Wireshark shows TCP RST on port 3050 RTUCI task opens the socket but the S7 FB is not enabled Verify the EN input of the second Modbus FB instance is TRUE in OB1

Notes on Parallel Protocol Stacks

The CP341 and the integrated PN interface are independent. The ASCII driver on CP341 is a character-transparent, half-duplex, byte-oriented protocol with no dependence on the Ethernet side. The Modbus TCP library on the PN interface uses the standard open IE communication FBs (BSEND/BRECV, SEND/RECV, or USEND/URCV underneath, depending on the library version) to manage TCP socket state. There is no shared state between the two stacks, so failures on one do not propagate to the other.

If the S7-300 needs to serve a third protocol on Ethernet, a CP343-1 Lean or CP343-1 Advanced can be added to extend the connection budget beyond the 16-connection limit of the integrated PN. The Modbus TCP library supports the CP343-1 as well as the integrated PN interface, so porting to an external CP requires only a NetPro change, not a code change.

Standards and References

Does the CP341 ASCII driver convert binary bytes such as 0x67 to the printable text "67" before transmitting?

No. The ASCII driver on the CP341 is a free-form, 8-bit character-transparent protocol. With 8 data bits selected, it transmits every byte from 0x00 to 0xFF unchanged. The wire output of a 0x67 0x89 frame is exactly two bytes. Any binary-to-text conversion must be performed by the S7 user program if the end device truly requires printable ASCII.

Can the CPU 315-2 PN/DP run Modbus TCP at the same time as the CP341 runs an ASCII protocol to Ebilock 850?

Yes. The PN interface and the CP341 are physically separate modules and run independent protocol stacks. With the Modbus TCP library 2XV9 450-1MB02 loaded, the integrated PN can act as a Modbus TCP server on one or more ports while the CP341 handles the serial ASCII traffic to the Ebilock 850.

Can the same S7 IP address serve two Modbus TCP connections to the same RTUCI peer on different ports?

Yes. TCP sockets are identified by the 5-tuple {protocol, src IP, src port, dst IP, dst port}. Two sockets on ports 502 and 3050 to the same peer are independent. The S7 user program must instantiate the Modbus TCP FB twice, each with its own connection ID (assigned in NetPro) and its own instance DB.

Are ports 502 and 3050 suitable for distinguishing the two RTUCI connections?

Port 502 is the IANA-assigned Modbus TCP port and is the conventional choice for the primary Ebilock connection. Port 3050 is a non-standard, user-allocated port and is acceptable for the secondary RI connection as long as both endpoints are configured for it. Standard Modbus tools recognize port 502 natively; for port 3050 add a Wireshark "Decode As" rule for the Modbus dissector.

What is the maximum number of TCP connections on a CPU 315-2 PN/DP, and is two within budget?

The integrated PROFINET interface of the CPU 315-2 PN/DP supports up to 16 open connections in total. Two Modbus TCP connections to RTUCI leave 14 free for HMI, engineering, OPC UA, and additional links. If more are required, add a CP343-1 Lean or CP343-1 Advanced module to the rack.

Back to blog