Troubleshooting
Why HMI Data Logs Export Bool True as -1
Short answer
A Comfort Panel USB data log shows -1 for a true Boolean because the export serializes the all-ones bit pattern as a signed value. Assign the literals 1 and 0 conditionally into a separate numeric tag and log that tag; a direct copy into an Int or UInt keeps the -1. Verify the change in the raw CSV with a text editor.
On this page
A Comfort Panel writing a data log to a USB stick records a true Boolean as -1 because the export serializes the bit's all-ones pattern and the file reads it as a signed value: -1 is the correct signed interpretation of that pattern, not an inverted input or a PLC fault. The fix that works is a conditional assignment that writes the numeric literal 1 when the bit is true and the literal 0 when it is false, into a separate numeric tag that the data log records instead of the Boolean. A direct copy of the Boolean into an Int or UInt does not fix it — the -1 carried over in the reported case, on a TP900 Comfort under TIA Portal V16, with internal HMI bits and PLC bits alike. Verify the result in the raw CSV with a text editor, not in a spreadsheet.
Why a true bit serializes as -1 #
A Boolean carries the logical states false and true, and its logical meaning does not oblige every software interface to represent true numerically as 1. Serialization — the conversion of a runtime value into a file representation — can expose a different numeric convention at that boundary.
The sign belongs to the signed numeric representation, not to the Boolean. Under two's-complement signed representation, the sign lives in the top bit of the field, and a field with every bit set to 1 reads as -1. That is enough to explain how a true state can appear as a negative number in the CSV; it does not identify which internal conversion the panel performs, and fixing the file does not require knowing.
One caution, once: diagnose the observable behavior — do not change machine logic to accommodate a file format.
Confirm the state itself first. Display the bit on the HMI and export a sample in each known state; the displayed logical state and the file record should agree even though the number differs.
Working the -1 out for the TP900 USB log #
The inputs, each from the reported case: a TP900 Comfort Panel running a TIA Portal V16 project; a data log on a USB stick in CSV format; Boolean tags of both kinds, internal HMI bits and PLC bits, that read -1 in the file whenever the bit is on. The field width the panel writes is not documented in the case, so take 16 bits as the illustration — the result is the same at any width.
The rule in words: a field of n bits, all set to 1, has the value 2^n - 1 read as unsigned, and the same pattern read as a signed two's-complement value equals (2^n - 1) - 2^n.
The arithmetic, written out:
- All ones across 16 bits: 2^16 - 1 = 65536 - 1 = 65535.
- Cross-check by summing the bit weights: 1 + 2 + 4 + ... + 32768 = 65535.
- Signed reading: 65535 - 65536 = -1.
- The same at 8 bits: 2^8 - 1 = 255, and 255 - 256 = -1.
Result: the exported field for a true bit reads -1, at any width.
The decision this leads to: if the raw CSV shows -1 exactly where the panel shows the bit on, the logging path is working and only the representation needs normalizing — go straight to the conditional assignment. If the CSV value ever disagrees with the displayed state, that is a mapping or ordering fault rather than a representation convention, and the archive reference is the first thing to check.
What does not work: copies and casts #
The first workaround in the reported case copied the Boolean into an Int, then an unsigned Int, then other types, and the -1 carried right over every time. That failure is informative: an assignment of the source variable converts through the source's representation, so the destination receives the same all-ones pattern and signs it the same way. The destination's data type is not the lever; the source expression is.
The trap works in reverse after the fix: any later direct copy of the Boolean into the logging tag overwrites the normalized 1 or 0 with the -1 representation again. Remove competing writes so the conditional assignment is the only writer of the logging tag.
The fix: assign the literals 1 and 0, both branches #
Conceptual logic, not platform-specific syntax:
IF the source Boolean is true THEN
logging tag := 1
ELSE
logging tag := 0
END IF
The steps:
- Create a numeric tag for the report; an Int is enough, because it represents both literals. Keep the original Boolean for control — the numeric tag exists only for the log.
- Implement the conditional in the PLC or in an HMI calculation or script, wherever the source tag lives and updates. Write both branches: a missing false branch leaves the previous 1 in the tag after the bit drops, and the log then shows a stuck 1 for a false state.
- In the data log configuration, replace the archived Boolean with the numeric tag. Keeping the original Boolean column alongside helps diagnosis; distinguish the two columns so an unchanged Boolean column cannot make a working mapping look ineffective.
- Order the execution. A cyclic mapping must run at least as often as the logging cycle, so the tag is current when the log samples; an event-driven mapping must fire on both transitions, false to true and true to false.
- Handle startup. The tag's initial value can be recorded before the mapping first runs, so establish the numeric value before the first customer record is accepted.
Then run the three-state test: set the source false and read 0; set it true and read 1; return it to false and read 0 again. The third step is the one that proves both branches execute.
Verify the raw CSV, not the spreadsheet #
CSV, as described in RFC 4180, is a text format — fields separated by commas, one record per line — so the literal field content is the authoritative record of what the logging path wrote. A spreadsheet is an interpreter that applies its own settings at import. Read a copy of the file in a plain-text editor and check the column against the table below.
| Raw file reading | What it means | Next check |
|---|---|---|
| -1 exactly where the bit is on | The export uses the signed convention; the path is fine | Apply the conditional literal mapping |
| The numeric tag also reads -1 | A copy or a cast is still in the path | Replace it with literals in both branches |
| The tag reads 1 but the file reads -1 | The archive still references the Boolean | Point the data log at the numeric tag |
| The file reads 1 but the sheet shows otherwise | The discrepancy happens at import | Check the spreadsheet's import settings |
Hold this to the same standard as verifying HMI historical trend retention before committing: prove both states end to end before the customer sees a file. The raw-file-first rule holds on other runtimes too — reading a USB datalog from an HMI in the CODESYS runtime also starts from what the file contains rather than what an import shows. And a value that is stale from poor communication is not a fresh observation merely because it is formatted correctly.
When a string or a rewrite is the answer #
If the consumer of the file accepts words, conditionally assign the text true and false to a string tag and log that instead. Confirm first that the data log configuration supports string values; creating the string tag alone does not establish that the archive can record it. Words are a separate contract from numeric 0 and 1, so agree it with the customer before building it.
Rewriting the CSV after logging — a script that reads the file line by line and replaces values, or a find-and-replace in a spreadsheet — adds a parsing and file-handling step on top of the log, and a global replacement of -1 also hits every numeric column that legitimately holds -1: a deviation, an error count, a temperature offset. It is a reasonable one-off for your own diagnosis and the wrong routine to hand a customer.
The decision for this case #
The TP900 on TIA Portal V16 is logging correctly: -1 is the signed serialization of true, so leave the PLC logic and the wiring alone. Add a numeric tag, assign the literals 1 and 0 conditionally with both branches written, point the data log at that tag, and clear any competing writes. Prove it with the three-state test and a raw-file read before the next delivery. A conditionally assigned string is the only alternative worth building, and only if every consumer accepts words; everything downstream of the log stays as it is.
Questions asked
Does -1 in the CSV mean my PLC bit is inverted?
No. -1 is the signed reading of the all-ones bit pattern the export writes for a true bit, so the logical state is correct. Display the bit on the panel and compare it with the file record; when the two agree, fix the representation, not the machine logic.
Why did copying the Boolean into an Int still log -1?
Because the assignment converts through the source representation: the destination receives the same all-ones pattern and signs it the same way, whatever type is declared on it. Trace every step between the bit and the file column — source, intermediate tag, archive entry — because any direct copy on that path reintroduces the pattern. Only an expression that produces a literal writes 1 and 0.
Is find and replace in the spreadsheet an acceptable fix?
For a one-off check of your own, yes. As a routine for the customer, no: a global replacement of -1 also rewrites legitimate -1 values in other numeric columns, and it leaves the representation fault in the log path. Once the numeric tag carries 1 and 0, the exported file is correct as delivered and nobody edits it.
Do I need the ELSE branch if the tag starts at 0?
Yes. Without the false branch the tag keeps its previous 1 after the bit drops, and the log reports a false state as 1. A test that only toggles the bit once proves nothing; run all three steps — false reads 0, true reads 1, false again reads 0.
Can I log the state as the text true and false instead?
Only where every consumer of the file accepts words, and only after confirming that the data log configuration supports string values; creating a string tag alone does not prove the archive can record it. If any downstream step parses the column as a number, words break it, and 0 and 1 remain the contract.