-
Notifications
You must be signed in to change notification settings - Fork 642
Open
Description
Summary
Counter writes are currently encoded with bit-based addressing while reads use raw byte addressing, so a call like Write(DataType.Counter, startByteAdr = N) ends up targeting the wrong counter. The Types.Counter.ToByteArray method also shifts the high byte in the wrong direction, which zeroes the upper byte for any value above 255 and corrupts the data. On top of that, the documentation never clarifies that counters must be accessed with DataType.Counter (or Timer), that startByteAdr is the counter number, and that the DB parameter must stay at 0, leaving users unsure about the correct way to address counters.
Code references
- Address parsing: S7.Net/PLCAddress.cs lines ~184–200 — counters mapped to DataType.Counter, dbNumber = 0, address = counterNumber.
- Read request builder: S7.Net/PLCHelpers.cs lines ~118–145 — counters/timers write the start address directly (no *8) and carry the DB field (0).
- Write request builder: S7.Net/PlcSynchronous.cs lines ~377–404 (also async version) — multiplies start byte by 8, appropriate for DB/M/I/Q, wrong for Counter/Timer.
- Counter conversion: S7.Net/Types/Counter.cs lines ~28–33 — high byte uses (value << 8) & 0xFF instead of value >> 8.
Steps to reproduce
- Connect to any S7 PLC (or simulator) with a known counter value.
- Call plc.Write(DataType.Counter, 0, 5, (ushort)1234);
- Read back plc.Read(DataType.Counter, 0, 5, VarType.Counter, 1);
- Observe that the value did not update (or another counter did), and values ≥256 are truncated.
Expected
- Write and Read address the same counter number when startByteAdr = counter index and db = 0.
- Counter values round-trip correctly for full 16-bit range.
Actual
- Write encodes address as bits (startByteAdr * 8), so it targets an incorrect address for counters/timers.
- Values ≥256 are written with the high byte zeroed.
Impact
- Counter writes are unreliable/wrong; any user writing counters gets incorrect data.
- Misleading documentation increases confusion; users may set DBs unnecessarily.
Metadata
Metadata
Assignees
Labels
No labels