Skip to main content

On Socket Data

This block is triggered when data is received through a socket connection. Use it to process incoming data from remote servers and devices.

On Socket Data Block

Overview​

The On Socket Data block receives data from TCP/IP socket connections, enabling your application to process responses from remote servers and custom protocols.

Configuration​

  • Socket Connection: Which socket to receive data from
  • Output: Variable to store received data
  • Buffer Size: Maximum data size to receive

Data Reception​

When data arrives:

  1. On Socket Data block triggers
  2. Received data stored in output variable
  3. Process and parse data
  4. Extract relevant information
  5. Take appropriate action

Data Format​

Handle various data formats:

Text/String​

  • UTF-8 or ASCII text
  • Line-based protocols
  • JSON messages
  • XML data

Binary​

  • Raw binary data
  • Protocol-specific packets
  • File transfers
  • Image data

Hexadecimal​

  • Hex-encoded data
  • Debugging format
  • Legacy protocols

Data Parsing​

Parse received data:

Line-Based Protocols​

Split by newline characters

Fixed-Length Messages​

Extract fixed-size chunks

Delimited Messages​

Parse by delimiter characters

JSON/XML​

Parse structured data formats

Use Variable Set and string manipulation to parse data.

Buffer Management​

Socket receive buffer:

  • Configurable buffer size
  • Automatic buffering
  • Multiple message handling
  • Overflow protection

Partial Data​

Handle partial message reception:

  • Data may arrive in chunks
  • Accumulate until complete
  • Detect message boundaries
  • Validate message completeness

Use Cases​

Common socket data reception scenarios:

  • Process Modbus TCP responses
  • Receive API responses
  • Handle custom protocol replies
  • Parse sensor data streams
  • Process database query results

Protocol Implementation​

Implement request/response protocols:

  1. Socket Connect to server
  2. Socket Send request
  3. On Socket Data receives response
  4. Parse and validate response
  5. Extract result data
  6. Continue communication or close

Data Validation​

Validate received data:

  • Check message format
  • Verify checksums/CRCs
  • Validate protocol headers
  • Detect errors and corruption
  • Handle invalid data gracefully

Error Handling​

Handle data reception errors:

  • Incomplete messages: Wait for more data
  • Invalid format: Log error and discard
  • Buffer overflow: Increase buffer or limit data
  • Connection closed: Handle in On Socket Event

Performance​

Optimize data reception:

  • Use appropriate buffer sizes
  • Process data efficiently
  • Avoid blocking operations
  • Handle large messages in chunks

State Machine​

Implement protocol state machines:

State: WAITING_FOR_HEADER
On Socket Data:
Parse header
Transition to WAITING_FOR_BODY

State: WAITING_FOR_BODY
On Socket Data:
Parse body
Process message
Transition to WAITING_FOR_HEADER

Multiple Connections​

Receive from multiple sockets:

  • Create separate data blocks
  • Track state per connection
  • Process messages independently
  • Coordinate responses

Binary Protocol Example​

Parse binary Modbus response:

  1. On Socket Data triggers
  2. Extract function code (byte 0)
  3. Extract data length (byte 1)
  4. Extract data values (bytes 2+)
  5. Calculate and verify CRC
  6. Store results in variables

Logging​

Log received data for debugging:

  • Raw data (hex dump)
  • Parsed values
  • Timestamps
  • Connection information

Security​

Secure data reception:

  • Validate all input data
  • Sanitize before use
  • Check message source
  • Implement rate limiting
  • Use TLS/SSL encryption

Troubleshooting​

Common data reception issues:

  • No data received: Check connection and sender
  • Partial data: Increase timeout or buffer size
  • Garbled data: Check encoding and format
  • Parse errors: Log raw data for analysis

See Also​