Siemens S7-300 CPU-317 Profibus Barcode Scanner Integration

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

Siemens S7-300 CPU-317 Profibus Barcode Scanner Integration Tutorial

This reference documents the complete engineering procedure for wiring, configuring, programming, and commissioning a Datalogic CBX500 connection box with a Datalogic scanner head onto a Siemens SIMATIC S7-300 CPU-317 over PROFIBUS-DP. The article targets industrial automation engineers who must capture scanned barcode data into a STEP 7 data block, decode ASCII payloads, and trigger the read cycle from a photoelectric sensor without sacrificing deterministic scan-to-tag latency.

The procedure applies to all 6ES7317-x CPU variants (for example, 6ES7317-2EK14-0AB0, 6ES7317-2AJ10-0AB0, 6ES7317-2PN/DP) provided the CPU exposes an integrated PROFIBUS-DP master port or is paired with a CP 342-5 communications processor. STEP 7 V5.5 + SP2 (or later) and SIMATIC Manager are the configuration environments covered here; TIA Portal equivalents are referenced where they apply to the migration path.

Engineering scope. This article assumes the PLC rack, power supply (PS 307), and signal modules (SM 321/SM 322) are already installed and that the AS-Interface network has been commissioned with an AS-i master (CP 343-2 or CPU 31x with integrated AS-i). The barcode scanner PROFIBUS node is added to the existing DP master system.

1. System Architecture Overview

The reference rig has four logical segments:

  1. Sensor segment: One through-beam or retro-reflective photoelectric switch (P&N or PNP, 24 VDC) wired to a digital input on the SM 321. This photocell generates the read-trigger signal.
  2. AS-i segment: An AS-i slave module (for example, Siemens 3RG9001-0AB00 or Datalogic ST-501) reporting the second photocell (operator-side enable) and any lamp/horn feedback.
  3. Scanner segment: Datalogic scanner head (Matrix 2xx, DS2x00, or D-Box series) terminated into a CBX500 connection box. The CBX500 supplies power to the head and exposes an on-board PROFIBUS-DP slave (typically the Datalogic BC9xx0 series PROFIBUS gateway, or the integrated slave on newer CBX100/500 LT models).
  4. Controller segment: S7-300 CPU 317 (firmware V3.x or later recommended) acting as DP master class 1, hosting the STEP 7 user program that copies received barcode strings into a DB.

For a typical conveyor sortation cell, the data flow is:

Photocell triggers → Scanner reads code → CBX500 transmits ASCII payload over PROFIBUS input area → CPU 317 copies bytes into DB → STEP 7 string-conversion FB resolves the human-readable label.

2. Prerequisites

Category Requirement Notes
Controller CPU 317-2 (6ES7317-2xxx) with PROFIBUS DP master port Firmware V3.x or later for full GSD Rev. 5 compliance
Power supply PS 307 (6ES7307-1xA00-0AA0) at 5 A or 10 A Scanner head draws 0.4-0.8 A at 24 VDC
Scanner head Datalogic DS2x0x, Matrix 2xx, or D-Box 2xxx Selected for read range and code density
Connection box Datalogic CBX500 (with BC9xx0 PROFIBUS gateway option) Provides power, I/O breakout, and field-bus interface
GSD file Datalogic GSD (e.g., DALA0F4C.GSD or revision-specific) Imported into STEP 7 HW Config
Engineering tool STEP 7 V5.5 + SP2/HF1 or STEP 7 V5.6 SIMATIC Manager used in this guide
Cabling PROFIBUS cable (6XV1830-0EH10), 9-pin D-sub connectors with terminating resistor Baud rate 1.5 Mbit/s max for cable > 50 m
Sensor PNP retroreflective photocell, 24 VDC 10-30 VDC operating range, N.O. output
AS-i master CP 343-2 (6GK7343-2AH01) or integrated AS-i on CPU Only required if second photocell is on AS-i
PROFIBUS termination. Enable the terminating resistor only on the physical end nodes (last slave and DP master). Leaving termination ON at intermediate slaves causes reflections and DP diagnostic errors (SF on CPU).

3. Datalogic CBX500 Wiring and PROFIBUS Address Setting

The CBX500 exposes dip-switch SW1 (or rotary SW2 on later revisions) for PROFIBUS station address selection. Valid addresses are 1 to 126; address 0 is reserved for master-class-2 service tools. A typical selection is address 7, leaving 1-6 free for I/O slaves.

  1. Power the CBX500 from a dedicated 24 VDC branch (PS 307 -> breaker -> CBX500 pin 13/25 for V+/GND).
  2. Connect the digital trigger input (External Trigger, pin 6 of the 25-pin D-sub on the CBX500) to the PNP output of the trigger photocell through SM 321 DO. Common the 0 V reference between the PLC and the CBX500.
  3. Wire the PROFIBUS D-sub (9-pin) on the CBX500 to the DP master port of the CPU 317 using a PROFIBUS drop cable. Activate the terminator on the CBX500 only if it is the final node.
  4. Configure the trigger polarity in the Datalogic Genius™ or DL.CODE™ configuration tool: Trigger Source = External (Rising Edge), Trigger Edge = Leading, Reading Phase Time = 50-200 ms.
  5. Set the PROFIBUS communication parameters on the CBX500: Byte Order = Motorola (Big-Endian), Input Data Length = 32 bytes, Output Data Length = 4 bytes. This is the default slot configuration for most Datalogic PROFIBUS gateways.

4. GSD File Import and HW Config

The GSD file describes the scanner slave to the DP master so HW Config can place it onto the bus and assign I/O addresses.

  1. Copy the manufacturer-supplied GSD file (extension .GSD) and any associated bitmap (.BMP) into C:\Program Files\Siemens\Automation\SIMATIC Manager\S7DATA\GSD or the equivalent user-defined path.
  2. Open SIMATIC Manager → HW Config. Right-click the DP master system line and choose Options → Install GSD File. Browse to the file and confirm.
  3. Open the Datalogic catalog folder (now appearing under PROFIBUS DP → Other Field Devices → Sensors → Datalogic) and drag the scanner onto the DP master system line.
  4. Assign PROFIBUS address 7 (matching the CBX500 dip switch).
  5. Open the slot configuration dialog. Configure the module as:
    - Input, 32 bytes at address IW x .. IW x+30 (Word-aligned).
    - Output, 4 bytes at address QW y .. QW y+2 for trigger/read commands and lamp control.
  6. Save and compile (Station → Save and Compile). Download the hardware configuration to the CPU 317.

If the CPU reports SF after download, double-check the baud rate. Datalogic PROFIBUS gateways auto-detect up to 12 Mbit/s but the cable length and connector quality must support the chosen rate. Drop the rate to 1.5 Mbit/s for cable runs exceeding 100 m.

5. Data Block Design for Barcode Storage

STEP 7 offers three usable structures for barcode data:

Data type Length Best use case Field width
ARRAY of CHAR User-defined (e.g., 1..64) Raw byte storage, fast FC access 1 byte per character
STRING 0..254 + 2-byte header Standard Siemens string handling 1 byte per character
ARRAY of BYTE User-defined Direct PROFIBUS input area mapping 1 byte per element

Recommended DB layout for an industrial sortation cell (DB100):

DATA_BLOCK DB100
TITLE: BarcodeCapture

STRUCT
    TriggerFlag         : BOOL;       // One-shot from photocell
    ScanActive          : BOOL;       // Scanner Ready/Triggered feedback
    ScanComplete        : BOOL;       // Valid data flag from scanner
    ReadError           : BOOL;       // No-read or CRC error
    BarcodeLength       : INT;        // Actual length returned by scanner
    RawData             : ARRAY[1..64] OF BYTE;  // PROFIBUS input mirror
    AsciiString         : STRING[64]; // Resolved string for HMI/recipe
    CodeType            : INT;        // 0=Code39, 1=Code128, 2=EAN, ...
    Timestamp           : DATE_AND_TIME;  // PLC time of valid read
    RecipeNumber        : INT;        // Optional decoded integer for recipe select
END_STRUCT;

BEGIN
    TriggerFlag         := FALSE;
    ScanActive          := FALSE;
    ScanComplete        := FALSE;
    ReadError           := FALSE;
    BarcodeLength       := 0;
    RawData             := 64#00#;
    AsciiString         := '';
    CodeType            := 0;
    RecipeNumber        := 0;
END_DATA_BLOCK

The RawData array length (64 bytes) corresponds to the scanner input slot length. AsciiString is the cleaned string the HMI/operator will see. BarcodeLength comes from the scanner's status word (typically bytes 1-2 of the input area; check the Datalogic manual for the exact offset for the chosen firmware revision).

6. ASCII to STRING Conversion Logic

PROFIBUS transmits 8-bit bytes. Barcode data arrives as a stream of ASCII codes - each character occupies one byte. To present the label on an HMI or to compare against a recipe, the byte array must be copied into a STRING with correct length and no trailing null padding shown to the operator.

Create an FB (FB200, instance DB200) that performs the copy:

FUNCTION_BLOCK FB200
TITLE: AsciiArrayToString
VAR_INPUT
    pSource      : POINTER;     // Points to RawData[1] of DB100
    MaxLen       : INT;         // 64
    ValidLen     : INT;         // BarcodeLength from DB100
END_VAR
VAR_OUTPUT
    pTarget      : POINTER;     // Points to AsciiString of DB100
END_VAR
VAR_TEMP
    i            : INT;
    ch           : BYTE;
    dbNo         : INT;
END_VAR
BEGIN
    // Copy ValidLen bytes from source byte array to STRING,
    // respecting STRING max length (255).
    IF ValidLen > MaxLen THEN
        ValidLen := MaxLen;
    END_IF;
    IF ValidLen > 254 THEN
        ValidLen := 254;
    END_IF;

    // Write length header (BYTE 0) of STRING
    dbNo := WORD_TO_INT(SHR(IN:=DWORD_TO_WORD(DWORD#0), N:=16));
    // In practice, use direct DB number via SFC and a temporary ANY pointer.
    // Length header offset 0 of STRING = max length (set during DB init).

    FOR i := 1 TO ValidLen DO
        ch := BYTE#00;
        // Read from source area using BLK_MOV equivalent or pointer arithmetic
        // Placeholder for actual implementation:
        // ch := pSource^[i];
        // pTarget^[i] := ch;
    END_FOR;
END_FUNCTION_BLOCK
Pointer-based access is non-standard in STEP 7 V5.x. Use SFC 20 (BLKMOV) or SFC 81 (UBLKMOV) for predictable byte copying. The example above shows the conceptual control flow; the production code should use BLKMOV with an ANY pointer source to avoid AT-view conflicts on multi-instance DBs.

A cleaner alternative is to use SFC 20 BLKMOV from the PROFIBUS input area (PIW area) directly into DB100.RawData, and then convert with the well-known FC for STRING-from-ARRAY:

// In OB1 or OB35 (cyclic interrupt, e.g., every 50 ms):
CALL SFC 20  // BLKMOV
   SRCBLK  := P#I 100.0 BYTE 64        // PIW starting at IW 100
   RET_VAL := MW 200                  // Return code (0 = OK)
   DSTBLK  := P#DB100.DBX 6.0 BYTE 64 // RawData[1..64]

// Trigger one-shot from trigger photocell
A    I 0.0        // Photocell NPN/PNP input
FP   M 10.0       // Rising-edge bit
=    DB100.DBX0.0 // TriggerFlag

// Edge-triggered scanner activation
A    DB100.DBX0.0 // TriggerFlag
S    QW 80.0, 1   // Output byte bit 0 = Trigger (per scanner mapping)

// De-activate after 200 ms
A    DB100.DBX0.0
L    S5T#200MS
SD   T 1
A    T 1
R    QW 80.0, 1

// When scanner reports valid data
A    IW 102 == W#16#0100   // Status word bit: Valid Read
=    DB100.DBX0.2          // ScanComplete

7. Photocell Trigger Logic and One-Shot Handling

The trigger photocell produces a pulse that is wider than the read latency. A one-shot (single-scan) guarantees the scanner is triggered exactly once per object, regardless of how long the object dwells in the field of view. Use FP (rising-edge) on the trigger input before setting the scanner trigger bit.

Two-photocell design rationale:

  • Trigger photocell (SM 321 DI): Initiates the read. Hard-wired for minimum latency. Latched by edge detect.
  • Enable photocell (AS-i): Confirms the operator has authorized a read (for example, a hand-held reader at a workstation). Adds safety interlocks without consuming a digital input.

If the application is a fixed-position conveyor scan, the AS-i enable photocell is optional - the trigger alone is sufficient. Retain the AS-i enable only when an operator must confirm the read (recipe selection, manual picking).

8. Sample Complete OB1 Network

// OB1 - Main program sweep
// ---- Network 1: Trigger one-shot ----
A    I 0.0
FP   M 0.0
S    M 1.0          // TriggerFlag (M1.0 mirrors DB100.DBX0.0)

// ---- Network 2: Enable window ----
A    M 1.0
A    "ASi_Enable_OK"  // Mapped from AS-i slave input word
=    M 1.1          // Combined enable

// ---- Network 3: Scanner trigger pulse (200 ms) ----
A    M 1.1
L    S5T#200MS
SD   T 1
A    M 1.1
AN   T 1
=    Q 80.0         // Scanner external trigger

// ---- Network 4: Latch raw data when scan complete ----
A    IW 102
L    W#16#0100      // Bit pattern: Valid Read
==I
JCN  NO_READ
CALL SFC 20
   SRCBLK  := P#I 100.0 BYTE 64
   RET_VAL := MW 50
   DSTBLK  := P#DB100.DBX 6.0 BYTE 64
S    DB100.DBX0.2    // ScanComplete = TRUE
JU   END_READ
NO_READ: R DB100.DBX0.2
END_READ: NOP 0

// ---- Network 5: Reset trigger flag after capture ----
A    DB100.DBX0.2
R    M 1.0

9. Verification and Commissioning Steps

  1. Power up the CBX500. The LEDs on the connection box should show PWR (green) and READY (green). If COM is red, the PROFIBUS link is not active.
  2. Download the HW Config and the S7 program to the CPU 317. Switch to RUN. The CPU BF (Bus Fault) LED must be off.
  3. In SIMATIC Manager, open PLC → Monitor/Modify. Observe the input words starting at IW 100. With no barcode in view, bytes 0-1 should read the scanner status word; bytes 2-33 should be 16#00.
  4. Present a known barcode (for example, a Code 128 string (20)12345) under the scanner head.
  5. Verify in VAT or a watch table that DB100.RawData contains the ASCII sequence: 28 for '(', 32 for '2', 30 for '0', ... 53 for '5'.
  6. Trigger the conversion FB. Check that DB100.AsciiString shows (20)12345 on the HMI.
  7. Repeat with an EAN-13 code and a Code 39 alphanumeric to verify Code Type field.

10. Troubleshooting Matrix

Symptom Likely Cause Diagnostic Action Corrective Step
BF LED on CPU 317 lit PROFIBUS cable short, wrong baud, address mismatch Check address on CBX500 dip-switch vs HW Config Match address; verify 1.5 Mbit/s for > 50 m cable
SF LED on CPU 317 lit Slave diagnostic message - configuration mismatch PLC → Diagnostic Buffer; check slave diagnostic Confirm slot configuration matches Datalogic manual
RawData all zeros Trigger not reaching scanner Force Q 80.0 from VAT; observe scanner activity LED Re-wire external trigger polarity; check PNP vs NPN
RawData shows first byte but no payload Byte order swapped (Little-Endian vs Big-Endian) Compare bytes against DL.CODE display Set CBX500 to Motorola/Big-Endian byte order
AsciiString empty after conversion BarcodeLength <= 0 or ValidLen uninitialized Monitor DB100.BarcodeLength in VAT Read scanner status word offset from manual; remap
Duplicate scans per object Trigger dwell time too long Monitor I 0.0 in VAT during pass-by Use FP edge detect instead of level; shorten trigger pulse
Occasional CRC errors in diagnostic buffer EMI from VFD on adjacent cable tray Check PROFIBUS diagnostics for CRC count Separate PROFIBUS cable from power by 200 mm minimum
AS-i photocell always FALSE AS-i slave address conflict or uncommissioned AS-i Control Tool → slave list Re-address AS-i slave, reconfigure CP 343-2
Scanner LED green but no data in PIW GSD slot length mismatch Compare configured I/O length vs Datalogic manual Adjust slot to 32 bytes input / 4 bytes output
String shows garbage characters Endianness or ASCII vs raw byte interpretation Compare first two bytes against ASCII chart Confirm Motorola byte order; verify CHAR interpretation

11. ASCII Reference for Barcode Strings

Barcode payloads typically use printable ASCII (0x20-0x7E). Code 39 and Code 128 allow the full ASCII range; Code 128 in particular can encode any byte 0x00-0xFF using its code set B. For sortation use cases the data is alphanumeric, so direct copy of the byte array into a STRING is sufficient. The table below shows the codes engineers see most often:

Char Hex Decimal Char Hex Decimal
'(' 0x28 40 '0' 0x30 48
')' 0x29 41 '9' 0x39 57
' ' 0x20 32 'A' 0x41 65
'-' 0x2D 45 'Z' 0x5A 90
'.' 0x2E 46 CR 0x0D 13
'/' 0x2F 47 LF 0x0A 10

The example label (20)12345 is the AI 20 (Product Number) identifier used in GS1-128 barcodes. To confirm the conversion, observe in VAT that bytes 1-9 of RawData read 16#28 16#32 16#30 16#29 16#31 16#32 16#33 16#34 16#35.

12. Performance and Timing Considerations

For high-throughput conveyors (objects per second > 5), place the barcode copy logic in OB35 (cyclic interrupt) rather than OB1. A 20 ms OB35 interval leaves headroom for the SFC 20 BLKMOV execution while still meeting a 200 ms cycle budget per object. The trigger flag remains in OB1 to maintain deterministic edge detection.

PROFIBUS cycle time at 1.5 Mbit/s with 64 input bytes is approximately 1-2 ms per slave. With four slaves on the bus, total bus time is below 10 ms, well within the OB1 scan budget of a CPU 317 (typical OB1 cycle 5-15 ms with the described program).

13. Migration to TIA Portal

If the project is migrated to TIA Portal V16 or later, the GSD import path is Options → Manage general station description files (GSD). The slot configuration moves from HW Config to Device View. SFC 20 BLKMOV is still available as MOVE_BLK in TIA Portal with identical semantics. The data block design transfers intact; STRING handling is identical. The principal engineering effort in migration is reassigning symbolic I/O addresses.

14. Spare Parts and Catalog Numbers

Component Catalog Number Manufacturer
CPU 317-2 PN/DP 6ES7317-2EK14-0AB0 Siemens
PS 307 5A 6ES7307-1EA00-0AA0 Siemens
SM 321 DI 16x24VDC 6ES7321-1BH02-0AA0 Siemens
CP 343-2 AS-i Master 6GK7343-2AH01 Siemens
CBX500 Connection Box 93A301080 Datalogic
Matrix 300 Scanner Head 937600002 Datalogic
DS2100N Scanner Head 931151000 Datalogic
PROFIBUS Connector with Term. 6GK1500-0FC10 Siemens
PROFIBUS Cable 6XV1830-0EH10 Siemens
Firmware notes. CPU 317 firmware V3.3 or later is required for full GSD Revision 5 compatibility. Datalogic CBX500 firmware revisions above 5.5 support 32-byte input slots natively; older revisions require firmware update via DL.CODE.

FAQ

Do barcode scanners output ASCII by default, and will that ASCII appear directly in a STEP 7 data block?

Yes. Most industrial 1D/2D scanners, including the Datalogic family behind the CBX500, transmit the decoded label as 8-bit ASCII over PROFIBUS. Each character occupies one byte. When copied into a CHAR or BYTE array inside a STEP 7 DB, the byte value equals the ASCII code (for example, '(' = 16#28, 'A' = 16#41). A subsequent STRING conversion presents the label as readable text on the HMI.

Can I avoid using the second (AS-i) photocell and still reliably capture scans?

Yes. For conveyor sortation, the trigger photocell wired to an SM 321 digital input is sufficient. The AS-i photocell is only required when an operator-initiated enable signal must accompany the read (for example, workstation recipe selection). Eliminating it removes one AS-i slave and one slot from the CP 343-2 configuration.

What PROFIBUS baud rate should I select for a CBX500 over 50 m of cable?

Use 1.5 Mbit/s for cable runs between 50 m and 200 m. For runs under 50 m, 12 Mbit/s is permissible if the connectors and cable are high quality. Datalogic PROFIBUS gateways auto-detect baud rate up to 12 Mbit/s; the DP master must be set to the matching rate in HW Config.

Why does DB100.RawData contain only zeros even though the scanner LED is green?

The most common cause is a slot-length mismatch between HW Config and the scanner. Verify that the configured input slot is 32 bytes (or the length specified in the Datalogic manual for the installed firmware). Also confirm the byte order is set to Motorola/Big-Endian in the CBX500 configuration. A status word of 16#0100 in bytes 0-1 indicates a valid read even when the payload appears at a different offset.

How do I trigger the scanner read from STEP 7?

Set the scanner's external trigger bit in the output slot (typically QW 80.0) for at least 50 ms following the rising edge of the trigger photocell. A 200 ms pulse with FP edge detection reliably covers scanner latency. The scanner acknowledges via the input status word; latch the data on the rising edge of the Valid Read bit to avoid duplicate captures.

Should I use ARRAY OF BYTE or STRING to store the barcode in the data block?

Use ARRAY OF BYTE for the raw PROFIBUS mirror (DB100.RawData) and STRING for the operator-facing value (DB100.AsciiString). This dual layout separates the field-bus representation from the application layer, simplifies BLKMOV copies, and lets the HMI bind directly to STRING without conversion on the panel side.

Back to blog