IMDTouchKnowledge BaseShop main store

How-to

How to Build an Event Timestamp String in Crimson 3

HMI and touchscreens Published 7 min read

Short answer

Build the timestamp inside the program that handles the event, reading the clock once at detection, so the string records event time rather than the 1-second On Tick refresh time. Keep the format fixed-width at 19 characters, MM/DD/YYYY HH:MM:SS, with the field order defined in the file contract before any code is written.

On this page
  1. Decide the record contract before writing code
  2. Build the string where the event is handled
  3. Why an On Tick string stamps the wrong moment
  4. Worked example: 1-second tick, 19-character stamp
  5. Splitting the record back apart
  6. Before you deploy
  7. Questions asked

Build the timestamp inside the program that handles the event, read the device clock once at the moment of detection, and write the result as a fixed-width 19-character string in MM/DD/YYYY HH:MM:SS order. An On Tick program that refreshes a time string every 1 second keeps a current-time label available, but it stamps an event with the refresh time, not the event time — a time up to just under 1 second old at that interval. The Time and Date Primitive on a display page proves nothing about the string your file I/O commands use; that string has to be built in code. The steps below build it, keep it attached to the event text, and split the finished records back apart. The tested application behind them ran on a Data Station Plus DSPSX000 with Crimson 3 version 579.003 and produced stamps like 02/01/2003 08:53:46.

Decide the record contract before writing code #

The string is an interface between your program and whatever reads the file later, so settle the contract first:

Decision Commit to Reason
Field order MM/DD/YYYY or DD/MM/YYYY, identical in displays, strings and the importer 02/01/2003 is February 1 or January 2 depending on who reads it
Width 19 characters, leading zeros in every field Fixed positions let a consumer split by offset instead of searching
Resolution Whole seconds Two events inside one second get identical text
Clock basis One time zone and one clock-setting policy The text carries no offset information
Meaning Event detection time, or a current-time label This decides where the clock is read

If the goal is tag values with times, use the built-in data logger instead of writing code: it logs tags at programmable rates, stamps them automatically, and stores them in open CSV format. Build the manual string for the case the logger does not cover — descriptive event text assembled in a program and written with the file I/O commands.

One limit worth stating plainly: these strings are operational records. Whole-second resolution and a device clock that can be set by hand make them unusable as a sequence-of-events record for a safety investigation.

Build the string where the event is handled #

The work happens in a Crimson program, written in the same C-like language and editor you would use for building a drop-down product list in Crimson 3.

  1. Call the capture from the event-handling path, not from a timer. Crimson 3.0 gives user programs four Execution Task options — Same as Caller, Background 1, Background 2 and Background 3 — and the capture has to run on the event's timing, not on a background cadence.
  2. Read the clock once, when the event is detected, and derive the Year, Month, Date, Hour, Minute and Second from that single read; the time and date functions in the Crimson programming reference expose those components. Reading the date and the time separately can straddle midnight and pair one day with another day's time.
  3. Pad every field to its width with leading zeros — two digits for month, day, hour, minute and second, four for the year — so the string never changes length.
  4. Concatenate in the contract order with fixed separators: a slash between the date fields, one space, a colon between the time fields.
  5. Assemble the descriptive text around the captured stamp immediately, and keep the finished string with the event through any queueing. A message that takes its timestamp when the file is written records the write time; a message that reads a shared refreshed string can pick up a later time.
  6. Write the record with the file I/O commands into a destination sized for the whole record — stamp, one separator and the message text — not for the stamp alone.

Once records accumulate in the file, displaying PLC CSV data logs on an HMI screen is a separate piece of work with its own setup.

Why an On Tick string stamps the wrong moment #

The tested program ran On Tick, executing every 1 second, and refreshed a shared time string to that cadence. That makes a good current-time label: between refreshes its age runs from 0 up to just under 1 second, and execution delays push it further. Three failure shapes come out of misusing it:

Symptom Mechanism Check
Event carries the previous second Message used the string last refreshed by the tick program Compare the order of event detection and string refresh
Timestamp is later than the event Time was assigned during deferred processing or the file write Find the clock read in the event path
Date looks valid but imports wrong Month/day order differs between writer and importer Test with a record whose day is above 12
Text field converts to a wrong number TextToInt ran before the token was validated Inspect the complete token before conversion

Shortening the tick interval narrows the age window; it never turns a refreshed string into an event-time capture. A correct display does not help either: the Time and Date Primitive on the panel and the string consumed by the file I/O commands are different objects, so inspect the string actually passed to the write, not the screen.

Worked example: 1-second tick, 19-character stamp #

Inputs:

  • Tick interval of the source program: 1 second, the tested configuration.
  • Required stamp: MM/DD/YYYY HH:MM:SS; the tested output was 02/01/2003 08:53:46.
  • Field widths: month 2, day 2, year 4, hour 2, minute 2, second 2 digits; separators are two slashes, one space, two colons.

Rule in words: the stamp's length is the sum of its digit fields and its separators, and the timestamp error of a periodically refreshed string equals that string's age at the moment the event reads it.

Arithmetic:

  • Date block: 2 + 1 + 2 + 1 + 4 = 10 characters.
  • Space: 1 character.
  • Time block: 2 + 1 + 2 + 1 + 2 = 8 characters.
  • Stamp length: 10 + 1 + 8 = 19 characters.
  • Age of the tick-refreshed string when an event reads it: from 0 s up to just under 1 s, because the interval is 1 s.

Result: every record is exactly 19 characters wide, with the month, day, year and each time field at fixed offsets, and a stamp taken from the tick string can lag the event by as much as just under 1 second — more if execution delays the refresh.

Decision: when the record must place the event within its actual second — any application where two events can land inside one second, or where order within 1 s matters — capture the clock in the event-handling path as the steps above describe. When a whole-second current-time label is genuinely enough, the On Tick string is acceptable. Either way, size the destination string to 19 characters plus one separator plus the message text; a destination that fits the stamp alone will truncate the message.

Splitting the record back apart #

The same string tools split a record for parsing. The calls below use the documented Crimson string functions Find, Left, Right and Len; message, index, tok1 and tok2 are illustrative variables — message is a string holding one record, index is an int initialized to 0, and tok1 and tok2 are strings:

index = Find(message, 32, 0);    // 32 is the character code of the space
tok1 = Left(message, index);
tok2 = Right(message, Len(message) - index - 1);

Test it on one record, with message set to "Pump 42". Len(message) is 7, and Find returns 4, the position of the space counting from zero. tok1 = Left(message, 4) = "Pump", and tok2 = Right(message, 7 - 4 - 1) = Right(message, 2) = "42". The test that proves the split: the two tokens plus the one separator must account for every character, and Len(tok1) + Len(tok2) + 1 = 4 + 2 + 1 = 7 does exactly that.

Before calling TextToInt on tok2, prove the token is numeric: check that every character of the complete token is a digit, allowing a leading minus, and treat the token as text otherwise. Conversion before validation is what turns a text field into a quietly wrong number.

A fixed-width stamp also splits without searching: Left(record, 19) returns the timestamp from any record that starts with one, where record is a string holding the full line.

Before you deploy #

The tested combination is a Data Station Plus DSPSX000 running Crimson 3 version 579.003. Treat that as what was verified, not as a compatibility claim: before deploying to another controller or another build, confirm the function names and their conversion behavior against the Crimson programming reference for the version installed, and take code and documentation from Red Lion's official channels rather than unofficial copies.

Questions asked

The clock on the panel displays correctly, so why do my event strings show the wrong time?

The Time and Date Primitive on the display and the string used by the file-writing program are separate objects. A correct display proves only that the device clock is right, not that an event string exists or that the file I/O uses it. Inspect the string actually passed to the write command. If that string is refreshed On Tick, it can also carry the previous second's time even when everything on screen looks correct.

Can I fix the timing by changing the On Tick interval?

No. The interval only narrows the gap. At 1 second the refreshed string is between 0 and just under 1 second old when an event reads it, and shortening the interval shrinks that window without ever making a refreshed string an event-time capture. The clock must be read once, inside the program that handles the event, if the string has to identify when the event occurred.

How do I stop whatever imports the file from reading the date day-first?

The stamp 02/01/2003 is ambiguous on its face, so settle the field order in the file contract before any code exists and keep it identical in displays, event strings and the importing software. To prove the reader has it right, test with a record stamped on the 13th or later in the month: a day-first reader takes 13 as the month and fails visibly instead of silently swapping month and day.

Two events came out with identical timestamps. Is the string broken?

No. The format resolves whole seconds, so two events detected inside the same second produce identical text, and no parser can order them afterward. If order matters, write your own sequence number alongside the stamp when the event is captured, because sorting identical strings cannot reconstruct which came first.

Which hardware and Crimson version was this tested on?

The application behind these steps ran on a Data Station Plus DSPSX000 with Crimson 3 version 579.003. Treat that as the tested combination, not a compatibility limit: before deploying to another controller or build, confirm the function names and conversion behavior against the programming reference for the version you have installed.

Keep reading

  • How-to

    Keep a PanelView Plus Trend Logging After Screen Changes

    A PanelView Plus Trend object only records while its screen is visible. Configure a Data Log model to start at power-up, show it through an XY trend with pen 2 as the X axis, and give the log a bounded record limit so it overwrites the oldest records instead of growing without bound.

    HMI and touchscreens

  • How-to

    Verifying HMI Historical Trend Retention Before Committing

    An 8-hour trend sampled every 5 minutes spans 96 intervals — 97 stored points if both endpoints are kept — so the panel needs a historical buffer of at least 97 samples per tag that repopulates the trend after screen changes. Verify the buffer depth, the interval setting, and restoration behavior on the bench before you build the screens.

    HMI and touchscreens

  • How-to

    How to run a Crimson 3.1 background task for raw serial data

    Put the parser in a Crimson 3.1 program whose Execution Task is set to Background 1, wrap it in a while(true){} loop with a blocking LF-terminated PortInput() read, and start it from the global On Startup action; a page OnTick parser only runs while its page is displayed and drops records at the captured worst case of 11 messages per second.

    HMI and touchscreens