The TSnap7Client class provides an asynchronous interface for communicating with S7 PLCs. Unlike standard synchronous calls, asynchronous methods (prefixed with As) initiate a job that runs in a background thread.
To handle the results of an asynchronous operation, you can use one of two patterns:
- Callback Pattern: Register a completion callback using
SetAsCallback. When a job finishes, the provided function is called with the operation code and the result. - Polling/Waiting Pattern: Use
CheckAsCompletion to poll for status or WaitAsCompletion to block the current thread until the job completes or a timeout occurs.
Common asynchronous operations include reading/writing data areas (AsReadArea, AsWriteArea), DB operations (AsDBRead, AsDBWrite, AsDBGet), and block transfers (AsUpload, AsDownload).
// Example: Using the callback pattern
TSnap7Client client;
// Define a callback function
void MyCompletionCallback(void * usrPtr, int opCode, int opResult) {
// Handle completion
}
// Register the callback
client.SetAsCallback(MyCompletionCallback, nullptr);
// Start an asynchronous DB read
int result = client.AsDBRead(1, 0, 10, buffer);