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
- 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. - Thread Safety:
SiemensClient is thread-safe. You can use a single instance as a singleton across multiple threads. - 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");