Siemens S7

For Siemens S7, SQL4automation provides four sample projects: one for Siemens S7-1200 and Siemens S7-1500 in TIA Portal, one for S7-300 / S7-400 PN/DP controllers, and one in combination with the communication processor CP-343 / CP-443 in Simatic Studio.

Connecting Siemens S7 PLC to SQL Databases, ERP, MES

For Siemens S7, SQL4automation provides four sample projects. For Siemens S7-1200 and Siemens S7-1500 in TIA Portal, one for S7-300 / S7-400 PN/DP controllers, and one in combination with the communication processor CP-343 / CP-443 in Simatic Studio. No OPC UA server, no middleware, and no additional hardware are required. SQL4automation connects your Siemens PLC directly to any ODBC-compatible database.

Project-Specific Adjustments DB SQL4Siemens

Only a few project-specific adjustments need to be made in the SQL4Siemens data block: the IP address and port of the SQL4automation Connector, and the SQL string. The data block structure is identical across all supported S7 families, making it straightforward to migrate between controller generations.

SQL4automation Link Settings

SQL4automation provides dedicated connection settings for Siemens S7. The project-specific adjustments are made in just a few clicks. In the download section, you will find detailed instructions on how to properly integrate the SQL4Siemens library and configure the SQL4automation Connector.

In the download section, you will find detailed instructions on how to properly integrate the SQL4Siemens library and configure the SQL4automation Connector.

FAQ's

Freuqently Asked Questions to Siemens S7

Can SQL4automation connect a Siemens S7 PLC directly to a SQL database without OPC UA?

Yes. SQL4automation establishes a direct TCP/IP connection between the Siemens S7 controller and the SQL4automation Connector running on a Windows PC. No OPC UA server, no additional middleware, and no extra hardware are needed. The Connector translates SQL queries from the PLC data block into native ODBC calls to your database.

What is the limitation of string length per query between PLC & SQL Database?

Unlimited. There is no limitation except for the buffer size which you can set to your liking.

Which Siemens TIA Portal versions are supported?

SQL4automation provides ready-to-use sample projects for TIA Portal V13 and newer, covering both S7-1200 and S7-1500 controller families. Legacy projects for S7-300 / S7-400 PN/DP controllers are available for Simatic Manager / Step 7 as well. Check the download section for the latest compatible project files.

Which SQL databases are compatible with SQL4automation for Siemens S7?

SQL4automation supports all ODBC-compatible databases, including Microsoft SQL Server, MySQL, MariaDB, PostgreSQL, Oracle, SQLite, and many more. The database selection is made in the SQL4automation Connector. No changes to the Siemens PLC project are required when switching databases.

Does SQL4automation support TLS encrypted communication for Siemens S7?

Yes, from Connector version V5.1.0.0 onwards. TLS requires firmware V2.5 or newer on S7-1500 and firmware V4.2 or newer on S7-1200. Note: TLS connection setup on S7-1200 can take up to 14 seconds, so it is recommended to use the ConnectionObserver pattern with xHoldConnection = TRUE to keep the connection open and avoid repeated setup delays.

For which Siemens PLC controllers has the SQL4automation library been tested?

SIMATIC S7-1200 CPU, SIMATIC S7-1500 CPU, SIMATIC S7 CP 1243-1, SIMATIC S7 CP 1543-1, SIMATIC S7-300 CPU 315-2 PN/DP, SIMATIC S7-300 CPU 317-2 PN/DP, SIMATIC S7-300 CPU 319-3 PN/DP, SIMATIC CP 343-1, SIMATIC S7-400, SIMATIC CP 443-1, SIMATIC WinAC RTX

Does SQL4automation work with Siemens S7-300 and S7-400 controllers?

Yes. SQL4automation includes a dedicated sample project for S7-300 and S7-400 PN/DP controllers, as well as a variant using the CP 343-1 and CP 443-1 communication processors configured in Simatic Studio. All three S7 generations (300/400, 1200, and 1500) use the same SQL4Siemens data block structure, so the integration approach is consistent across platforms.

Technical Data

REQUIREMENTS & VERSION INFO
Programming environment TIA Portal V13 or newer for S7-1200 / S7-1500; Simatic Manager / Step 7 for S7-300 / S7-400 with CP 343-1 / CP 443-1
Firmware for TLS (S7-1500) Firmware >= V2.5 required for TLS/SSL encrypted communication
Firmware for TLS (S7-1200) Firmware >= V4.2 required for TLS/SSL encrypted communication
TLS Connector version SQL4automation Connector V5.1.0.0 or newer required for encrypted communication
TLS note for S7-1200 WARNING: TLS connection setup on S7-1200 can take up to 14 seconds. Recommend using S7-1500 for TLS, or keep connection open with xHoldConnection = TRUE (ConnectionObserver pattern) to pay setup cost only once.
Older CPUs S7-300 / S7-400 CPUs do not support the TCON_IP_V4_SEC module and cannot use TLS encryption. Use standard unencrypted communication for these generations.
REQUIRED BLOCKS / LIBRARIES
  • All blocks in 'Program blocks' > 'SQL4Siemens'
  • All data types in 'PLC data types' > 'SQL4Siemens'
  • MOVE_BLK_VARIANT
  • TRCV_C (S7-1200 only)
  • TSEND_C
  • fbSql4Siemens (separate version exists for S7-1200 and S7-1500 due to different TRCV block parameters)
  • idbSql4Siemens (instance data block - internal use)
  • dbSqlUserData (user data: request string array, response string table)
CONNECTOR-SIDE SETTINGS
Receive buffer Set to match the receive buffer size defined in utSqlResponseBuffer
CP 343-1 Interface Deactivate for PN/DP controllers. Activate only for CP 343-1 / CP 443-1 variants.
Fix Buffer Deactivate for PN/DP controllers. Activate only for CP 343-1 / CP 443-1 variants.
Target type Siemens S7 (dedicated target setting, not Standard)
TLS / SSL ENCRYPTION
Connector version TLS/SSL requires SQL4automation Connector V5.1.0.0 or newer
Certificate paths C:\ProgramData\SQL4automation\certs\CA (RootCA)
C:\ProgramData\SQL4automation\certs\S4AConnector (server cert)
C:\ProgramData\SQL4automation\certs\S4AQueryTool (query tool cert)
TIA Portal settings Import certificates rootCA.p12, server.p12, client.p12 via TIA Portal security settings. User: SQL4automation, pwd: SQL4automation.
Block parameters xActivateSecureConn: TRUE
iTLSClientCertRef: <ID of client certificate>
iTLSServerCertRef: <ID of RootCA certificate>
Commissioning order Always test connection WITHOUT TLS first. Only enable encryption once unencrypted communication is confirmed working. Check system time before enabling TLS.
FUNCTION BLOCK PARAMETERS
Parameter Type Description
xExecute BOOL Triggers the database request
iIP1 INT 1st octet of the Connector IP address
iIP2 INT 2nd octet of the Connector IP address
iIP3 INT 3rd octet of the Connector IP address
iIP4 INT 4th octet of the Connector IP address
iRemotePort INT Port number of the SQL4automation Connector
iLocalPort INT Port used locally on the controller
tTimeout TIME Communication timeout (default: 10 s)
xAbort BOOL Resets the connection
xHoldConnection BOOL Keeps the socket connection open after a database query
iConnectionID INT Unique ID for the connection
wHardwareID BYTE ID of the Ethernet interface (found in TIA Portal device configuration)
tFbExecTimeLimit TIME Maximum time the block may use per PLC cycle
stUserRequestST utSqlUserRequestST Query structure (string table)
stUserResponseST utSqlUserResponseST Response structure (string table)
xReady BOOL Output: ready for a new database query
xBusy BOOL Output: database query is in progress
xDone BOOL Output: database query completed successfully
xError BOOL Output: database query ended with an error
diResultState DINT Output: status / error code (0 = no error)
diResultRows DINT Output: number of rows returned by the query
diResultColumns DINT Output: number of columns returned by the query
tFbExecTimeMax TIME Output: measured maximum block execution time
tLastRequest TIME Output: measured duration of the last request
ERROR CODES (diResultState)
Error Code Description
1 Unknown SQL command
2 Query returns more records than MaxRows. Adjust query or increase MaxRows.
3 Query returns more columns than MaxColumns. Adjust query or increase MaxColumns.
4 Query returns more data than the defined buffer size. Adjust query or increase buffer size in the Connector and the controller.
5 Query returns at least one value larger than MaxStringLength. Adjust query or increase MaxStringLength.
10 Internal Connector error
11 Internal Connector error; database cannot be opened
21 No IP address defined
22 No port number defined
23 Request string is empty
24 MaxRows not defined
25 MaxColumns not defined
29 MaxStringLength size is invalid
41 No connection to SQL4automation Connector. Check firewall, IP address, and port.
51 Error sending the query. Check firewall, IP address, and port.
91 Timeout: no response from the Connector within the time window
95 Error receiving data
>100 ODBC database connection error codes
40002 General error in the SQL query; request string is invalid

Switch to SQL4automation Now.

Connect industrial controllers directly with SQL databases. Field-proven since 2007 for maximum performance and security, completely without middleware.

Siemens S7
by Industry.