3.1.2 TcpClient Demo
Scenario Simulation
A TCP Server listens on port 6666 with a custom binary protocol:
- Request:
0x01to0x04(command bytes) - Response: Little-endian byte stream containing total bytes, run status, temperature (float), and motor speed
Driver Development
Create a new class library project under Plugins\Drivers:
[DriverInfo("SimTcpClient", "TCP Client Simulator", "1.0.0")]
public class SimTcpClient : IDriver
{
private TcpClient _client;
private NetworkStream _stream;
[ConfigParameter("IP Address", "127.0.0.1")]
public string IpAddress { get; set; }
[ConfigParameter("Port", "6666")]
public int Port { get; set; }
public bool IsConnected => _client?.Connected ?? false;
public bool Connect()
{
try
{
_client = new TcpClient(IpAddress, Port);
_stream = _client.GetStream();
return true;
}
catch { return false; }
}
public bool Close()
{
_stream?.Close();
_client?.Close();
return true;
}
[MethodAttribute]
public DriverReturnValueModel Read(DriverAddressIoArgModel ioArg)
{
byte[] request = new byte[] { 0x01, 0x02, 0x03, 0x04 };
_stream.Write(request, 0, request.Length);
byte[] buffer = new byte[256];
int bytesRead = _stream.Read(buffer, 0, buffer.Length);
return ParseResponse(ioArg, buffer, bytesRead);
}
public void Dispose()
{
Close();
}
}
Register the Driver
- Build the driver project
- Copy the output DLL to the
Plugins\Driversdirectory - Restart IoTGateway — the driver will be auto-discovered
Create a Device
- Navigate to Device Management in the web UI
- Add a new device
- Select
SimTcpClientas the driver - Configure IP address and port
Add Variables
Add variables manually or import via Excel template. Define each variable's address mapping according to the protocol specification.
Start Collection
Click Start to begin data collection. Monitor real-time values in the variable list.
Verify Data
Check the collected data in the web interface. The variable values should update according to the TCP server responses.