IoTClient

repository·master·Indexed 23 days ago

https://github.com/zhaopeiym/iotclient

A .NET Standard 2.0 library for industrial IoT device communication. It provides implementations for mainstream PLC protocols, including ModBus (TCP, RTU, ASCII, and RTU over TCP), Siemens (S7-200, S7-300, S7-400, S7-1200, S7-1500), Mitsubishi, Omron (FINS), and Allen-Bradley.

Tokens
4.9K
Snippets
9
Records
19
Agent score
32%

What's inside IoTClient

  1. Understand the Result object structure

    master

    In iotclient, all read and write operations return a Result object. This object is used to inspect the outcome of the communication with the PLC. The following properties are available:

    • IsSucceed: A boolean indicating if the operation was successful.
    • Err: Contains error/exception information if the operation failed.
    • Requst: The actual request packet sent to the server.
    • Response: The raw response packet received from the server.
    • Value: The actual data value retrieved from the operation.
  2. Understand the Result object returned by operations

    master

    All read and write operations in IoTClient return a Result object containing the outcome of the operation.

    Key properties of the Result object:

    • IsSucceed: bool - Indicates if the operation was successful.
    • Err: string - Contains the error message if the operation failed.
    • Requst: byte[] - The actual request packet sent to the device.
    • Response: byte[] - The response packet received from the device.
    • Value: T - The actual value retrieved (for read operations).
  3. Understand the Result object returned by client operations

    master

    Most read and write operations in the iotclient return a Result object. This object provides metadata about the communication attempt, allowing for robust error handling and debugging.

    Properties:

    • IsSucceed: (bool) Indicates whether the operation was successful.
    • Err: (string/exception info) Contains error information if the operation failed.
    • Requst: (string/object) The actual request message sent to the server.
    • Response: (string/object) The raw response message received from the server.
    • Value: (T) The actual data value retrieved from the operation (if successful).
  4. Best practices for SiemensClient

    master

    To optimize performance and stability when working with Siemens PLCs, follow these guidelines:

    Connection Management

    • When NOT to use Open(): Siemens PLCs typically allow a limited number of long-lived connections (e.g., max 8). If you are hitting connection limits or running tests, do not call Open(). The client will automatically open and close the connection for each operation.
    • When to use Open(): Use Open() to maintain a long-lived connection when you have available connection slots and want to maximize read/write performance.

    Performance Optimization

    Use batch operations to significantly reduce communication overhead.

    Batch Read:

    Dictionary<string, DataTypeEnum> addresses = new Dictionary<string, DataTypeEnum>();
    addresses.Add("DB4.24", DataTypeEnum.Float);
    addresses.Add("DB1.434.0", DataTypeEnum.Bool);
    addresses.Add("V4109", DataTypeEnum.Byte);
    var result = client.BatchRead(addresses);

    Batch Write:

    Dictionary<string, object> addresses = new Dictionary<string, object>();
    addresses.Add("DB4.24", (float)1);
    addresses.Add("DB4.0", (float)2);
    addresses.Add("DB1.434.0", true);
    var result = client.BatchWrite(addresses);

    Important Notes

    • Explicit Typing: Just like ModBus, always explicitly cast values when writing to ensure the correct data type is sent (e.g., client.Write("DB4.12", 9) writes an int, while client.Write("DB4.12", (float)9) writes a float).
    • Thread Safety: SiemensClient is thread-safe. You can register it as a singleton and share a single instance across multiple threads for PLC communication.
  5. Use MitsubishiClient for Mitsubishi PLC operations

    master

    To interact with Mitsubishi PLCs, instantiate a MitsubishiClient with the appropriate MitsubishiVersion, IP address, and port.

    Key Operations:

    • Write: Use .Write(address, value) to write booleans or numeric types (e.g., short, int).
    • Read: Use specific methods like .ReadBoolean(address), .ReadInt16(address), or .ReadInt32(address) to retrieve values.
    • Connection Management: While the client can automatically open/close connections per operation, it is highly recommended to call .Open() manually to improve efficiency.
    • Result Handling: Read operations return a Result object containing success status, error messages, request/response logs, and the actual value.
  6. Use IoTClient Tool for testing

    master

    The IoTClient Tool is an open-source desktop application used to:

    1. Test communication with PLCs and various industrial protocols.
    2. Serve as a visual reference/example for how to use the iotclient library.

    It is available in the IoTClient.Examples repository.

  7. Use OmronFinsClient for Omron PLC operations

    master

    To interact with Omron PLCs via the FINS protocol, instantiate an OmronFinsClient with the target IP and port.

    Key Operations:

    • Write: Use .Write(address, value) for booleans or numeric types.
    • Read: Use .ReadBoolean(address), .ReadInt16(address), or .ReadInt32(address).
    • Connection Management: Call .Open() manually to avoid the overhead of opening/closing the connection on every single operation.
    • Result Handling: Operations return a Result object. Use result.IsSucceed to check for success and result.Value to access the data.
  8. Use AllenBradleyClient for Allen-Bradley PLC operations

    master

    To interact with Allen-Bradley PLCs, instantiate an AllenBradleyClient with the target IP and port (commonly 44818).

    Key Operations:

    • Write: Use .Write(address, value) to write data.
    • Read: Use .ReadInt16(address) or similar typed read methods.
    • Connection Management: Call .Open() manually to prevent the client from opening and closing the connection for every operation, which significantly improves performance.
    • Result Handling: Operations return a Result object containing IsSucceed, Err, Requst, Response, and Value.
  9. Use SiemensClient for Siemens PLC communication

    master

    The SiemensClient provides specialized support for Siemens PLCs (S7-200, S7-300, S7-400, S7-1200, S7-1500).

    Initialization

    Specify the Siemens version, IP, and port.

    // Example for S7-200 Smart
    SiemensClient client = new SiemensClient(SiemensVersion.S7_200Smart, "127.0.0.1", 102);

    Writing Data

    Write to addresses like Q (outputs), V (memory), or DB (data blocks). You must clarify the data type via casting.

    client.Write("Q1.3", true);
    client.Write("V2205", (short)11);
    client.Write("DB4.12", (float)9); // Explicitly writing a float

    Reading Data

    Use specific methods for the type you wish to retrieve. The address string does not need to include the type.

    var value1 = client.ReadBoolean("Q1.3").Value;
    var value2 = client.ReadInt16("V2205").Value;
    var value3 = client.ReadInt32("V2209").Value;

    Batch Operations

    To improve performance, use BatchRead and BatchWrite with dictionaries.

    // Batch Read
    Dictionary<string, DataTypeEnum> addresses = new Dictionary<string, DataTypeEnum>();
    addresses.Add("DB4.24", DataTypeEnum.Float);
    addresses.Add("DB1.434.0", DataTypeEnum.Bool);
    var result = client.BatchRead(addresses);
    
    // Batch Write
    Dictionary<string, object> writeAddresses = new Dictionary<string, object>();
    writeAddresses.Add("DB4.24", (float)1);
    writeAddresses.Add("DB1.434.0", true);
    var resultWrite = client.BatchWrite(writeAddresses);

    Best Practices

    1. Connection Management: Siemens PLCs often have limited long-term connections (e.g., up to 8). If you have many clients or are testing, do not call Open() manually; the client will automatically open/close per operation. If you have sufficient connections and need high performance, call Open() manually.
    2. Thread Safety: SiemensClient is thread-safe. You can use a single instance as a singleton across multiple threads.
    3. Data Types: Always clarify the type when writing (e.g., (float)9 vs 9).
    SiemensClient client = new SiemensClient(SiemensVersion.S7_200Smart, "127.0.0.1", 102);
    client.Open();
    var result = client.ReadInt16("V2205");
  10. Use ModBusRtuClient and ModBusAsciiClient for Serial Communication

    master

    Both ModBusRtuClient and ModbusAsciiClient use serial communication. Their read/write methods are identical to ModBusTcpClient once instantiated.

    ModBus RTU

    Instantiate with: [COM Port, Baud Rate, Data Bits, Stop Bits, Parity].

    ModBusRtuClient client = new ModBusRtuClient("COM3", 9600, 8, StopBits.One, Parity.None);

    ModBus ASCII

    Instantiate with: [COM Port, Baud Rate, Data Bits, Stop Bits, Parity].

    ModbusAsciiClient client = new ModbusAsciiClient("COM3", 9600, 8, StopBits.One, Parity.None);
  11. Use AllenBradleyClient for Rockwell PLC communication

    master

    To communicate with Allen-Bradley (Rockwell) PLCs, instantiate an AllenBradleyClient with the target IP and port.

    Best Practice: Manually call .Open() to maintain a persistent connection and improve performance.

    All operations return a Result object containing the operation status and data.