C# code to exchange data between a PC and an iC9200 using TLS function blocks over TCP protocol
| Version Number | Description |
|---|---|
| 1.0 | Initial version |
|
|
|
This application note shows how to exchange data between a PC and an iC9200 using TLS function blocks using TCP protocol.
1. Supported Components
| Component Name | Version |
|---|---|
| iC9200 series | ≥ 2024.3 |
| iCube Engineer | ≥ 2024.3 |
| VisualStudio | 2022 |
2. Solution Details
2.1. Code on iC9200 (server) written in iCubeEngineer
Create a socket. A valid handle will get created when a client connects to the server successfully.
The data being sent to the PC is an array of 256 bytes. In the example below, the last element of the array is incremented every scan. If the same instance of the TLS_SEND_2 function block is used as shown below, one can guarantee that data will be sent every scan from the iC9200.
2.2. C# code running on PC (TCP client)
In a new Visual Studio 2022 console project, include the following libraries.
Code below is used to read a byte array of size 256 from the iC9200 and send a byte array of 256 bytes. The iC9200 is at IP address 192.168.207187 and communicating over port 8000.
2.3. Execution Sequence
- Start the execution of the project on the iC9200. The iC9200 waits for a connection from the client.
- Run the C# application.
- When the PC client makes a connection with the iC9200 server, a handle is created in the iC9200 and the code on the iC9200 starts sending the array of size 256 and receives an array of 256 bytes.
- The C# code stops executing if the user presses any key. When the code is executing, the code reads data sent from the iC9200 and prints the last element of the array.
- This value is stuffed into the first element of the array sent back to the iC9200.
- The value coming back from the PC into the iC9200 can be read as the first element of the array in the iC9200.
- The C# code stops executing if the user presses any key. The client connection is closed and the communication stops.
- In order to restart communication, the TLS_SOCKET FB on the iC9200 will have to be activated again.