Troubleshooting
5 ms PLC data loss between PLC and SQL Server
Short answer
Your chain of CompactLogix, KEPServer, a VB.NET app and SQL Server captures 5-10% of the rows because each polled stage reads on its own schedule and the PLC overwrites tags between reads. The fix is to sample the 11 tags in a 5 ms periodic task into double-buffered PLC arrays and bulk-insert whole blocks into SQL Server.

On this page
Your chain of CompactLogix, KEPServer, a VB.NET desktop app and SQL Server cannot capture every 5 ms sample, and no scan-rate tuning will make it. A polled architecture reads each tag at the reader's convenience while the PLC overwrites it every task period, so the 5-10% capture you see is the expected result, not a fault. The decision: capture the 11 tags in a 5 ms periodic task inside the PLC, hand over full blocks through two alternating arrays, and write each block to SQL Server with a bulk insert keyed on a PLC sample counter.
Read the symptom first #
The pattern in your SQL table tells you which stage loses the data before you change anything:
- A steady fraction of rows with irregular timestamp spacing means the poll cycle (device scan, OPC update, request round trip) is slower than the target rate. This is the signature of your 5-10% case.
- Rows in bursts followed by long gaps mean the app stalls on per-row inserts or UI-thread work while the PLC keeps overwriting values.
- Several identical consecutive rows mean the app reads faster than the OPC cache refreshes and stamps the same stale value again.
- Timestamps that are PC receive time, not sample time, mean no sample timing exists in the PLC data at all; the jitter is baked in.
Why a polled chain cannot hold the target rate #
Every stage adds latency and none is synchronized to the PLC task. The PLC writes each tag once per task period; the OPC server reads on its own scan rate; the app picks up whatever the server cached. Anything produced between two reads is gone. Even a link configured at a 100 ms scan rate can settle around an effective 500 ms delivery rate in practice - orders of magnitude away from the target rate. OPC UA formalizes this with a minimum sampling interval the server advertises in its ServerCapabilities, and classic OPC DA behaves the same way in practice: the cache, not the PLC task, is what the client sees. Faster polling only shifts the percentage.
Capture in a PLC periodic task #
Move the sampling to the source. Run the capture logic in a PLC periodic task set to the target rate and confirm in the task monitor that the maximum task time stays under the task period with the full application running. Every overrun is a sample lost where no downstream check can see it.
Build the handshake with two arrays, A and B, of a structure holding the 11 tag values plus an incrementing sample counter:
- Each task execution writes the 11 values and the counter into the active array at the current index, then increments the index. Write straight into the indexed element so the task never moves thousands of values in one scan.
- When the index reaches the array length, set that buffer's ready flag to 1, swap to the other buffer, and reset the index to 0.
- If the newly activated buffer still shows ready = 1, the PC has not acknowledged it: increment an overrun counter and never overwrite unread data silently.
- The PC polls both ready flags at a relaxed rate well inside one buffer period, reads the whole ready array as a single block read, then clears the flag to release it.
Size the arrays against the controller memory and the maximum array read your OPC server allows; take both from the controller documentation and the server documentation, not from guesswork. The same block-transfer principle applies to the other fast-data paths described for reading Allen-Bradley PLC data without RSLinx running.
Sizing the buffer for 11 tags at the target period #
Inputs, all from the project: 11 tags, the task period you set above, and a one-second block transfer as the working pattern.
- Samples per block = block time / task period = 1 s / 5 ms = 200 samples.
- Values per block = samples x tags = 200 x 11 = 2,200 values, plus one counter per sample.
- Rows per insert = 200 rows, one per sample, written as a single bulk operation about once per second.
Result: two arrays of 200 elements each, 200 rows arriving per block. Decision: at 11 tags a one-second block is comfortably small for a CompactLogix-class controller and a single block read; if you later add tags, shorten the block time (half a second halves the array) rather than stretching the PC read cycle. Above roughly a thousand tags or when the capture task alone overruns its period, stop and take the task-monitor data and controller model to the controller vendor's support.
Write blocks with SqlBulkCopy, not 200 INSERTs #
A block of 200 rows written as individual INSERT statements on the UI thread is exactly the burst-and-gap symptom above. In VB.NET, load the block into a DataTable and write it with SqlBulkCopy, or wrap the inserts in one transaction. Move the database write onto its own worker thread and hand blocks over through a queue so the read loop never waits on SQL Server. Store the PLC sample counter in the table and reconstruct the true sample spacing from it, never from the PC clock. Logging architecture choices around this are covered in logging PLC values from an OPC server into SQL Server.
Prove zero loss #
Run a fixed test period and check four things:
- Gap query: count rows where the counter is not the previous counter plus 1. The result must be 0.
- Row count equals elapsed time / 5 ms.
- The PLC overrun counter stays at 0 while you deliberately slow SQL Server, for example during a running backup.
- The periodic task's maximum time stays below the task period with everything else running.
Decide #
- Keep your hardware. The chain is sound; only the sampling architecture is wrong.
- Do the PLC double-buffer rewrite you already started; it is the correct method and needs no new software.
- Buy a historian or a logger plugin only if you also need compression, bad-value substitution or plant-wide collection - and only one that reads blocks or streams controller data, because a logger that still polls tags inherits the same loss. The question of when a historian earns its place is covered in does SCADA need a historian between it and an OPC UA server.
- If the capture task overruns with only the capture logic in it, the problem is the controller's program load, and no PC-side change will fix it.
Questions asked
Why does the OPC server only deliver 5-10% of the 5 ms samples?
The server, the desktop app and the database all run on their own schedules, none tied to the PLC task. Each read returns whatever value the tag holds at that instant, and every sample written between two reads is overwritten. Tuning scan or update rates only shifts the percentage; capture has to happen inside the PLC.
Do I still need a faster network or a real-time PC after adding the PLC buffer?
No. With one block per second the network and PC have about a second of slack, and an ordinary industrial network carries a block of 2,200 values easily. The timing burden sits in the PLC periodic task, which must never overrun its 5 ms period; confirm that in the task monitor, not on the PC side.
Can a historian or a logger plugin replace the PLC buffer?
Only if it captures at the source. A logger that still polls tags on a scan rate inherits the same overwrite loss, whatever its price. A product that reads blocks or streams controller data losslessly works, but the PLC-side double buffer achieves the same result without new software, which is why it is the first step.
Why store a PLC sample counter instead of timestamping on the PC?
The PC clock reflects when the app received a value, which jitters with network and database delay; it cannot recover when the sample was taken. An incrementing counter written in the capture task fixes the true spacing and lets one query prove continuity: any row whose counter is not the previous one plus 1 marks a gap.


