IMDTouchKnowledge BaseShop main store

How-to

Send M221 PLC time to an HMI clock in Operator Terminal Expert

HMI and touchscreens Published 6 min read

Short answer

The M221 RTC reads out only as BCD system words and Operator Terminal Expert accepts only Unix seconds, so nothing converts between them automatically. Decode the BCD, compute Unix seconds in the PLC or an EOTE global script (the check value is 946684800 s for 2000-01-01), then write one 32-bit tag to the HMI Time/Date on a 30 s timer guarded by a 5 s drift threshold.

On this page
  1. Why nothing converts it for you
  2. Decode the BCD before any arithmetic
  3. The formula and its acceptance test
  4. Worked example: 2024-07-04 14:35:20 on the M221 RTC
  5. Write the HMI clock with a global script
  6. Prove it follows
  7. Questions asked

Convert the M221 BCD clock words to Unix seconds yourself, then write a single 32-bit tag into the HMI Time/Date from a global script on a timer. The controller gives you no shortcut: the M221 exposes its RTC only as BCD date and time words, while Operator Terminal Expert (EOTE) accepts only Unix seconds for the panel clock, and its datetime function takes number constants rather than tag values. Run the conversion in the M221 program when anything else also needs epoch time, or in an EOTE global script when the PLC program is frozen — both end with the same one-tag write.

Why nothing converts it for you #

The Modicon M221 Logic Controller Programming Guide (EIO0000001360.08) lists the RTC as system words holding date and time in BCD, with %SW49 documented as the day of the week (N = 1 for Monday) together with hour and minute as HHMM. Look up the remaining words in that guide for your firmware instead of trusting remembered addresses, and monitor the words online next to a known clock.

EOTE sits at the other end of the mismatch. The HMI Time/Date takes Unix seconds, and the datetime function in the expression editor types its inputs as number, so no expression can assemble a timestamp from live tags. A vendor FAQ covers syncing an HMIST6 clock from M241, M251 or M262 controllers; the M221 is the one left doing its own arithmetic. The gap closes with a global script that reads time fields and writes the HMI Time/Date — the approach reported working on a TM172, which lacks the Unix format in the same way.

Decode the BCD before any arithmetic #

BCD stores each decimal digit in one nibble, so decode byte by byte: tens digit times 10 plus units digit. An HHMM field reading hex 1435 is 14 hours 35 minutes, from hex 14 giving 1×10+4 = 14 and hex 35 giving 3×10+5 = 35. Confirm the year field width while you are online: if it carries two digits, add 2000 before the epoch calculation. The day-of-week digit in %SW49 is redundant for the math, because the weekday follows from the day count, so ignore it.

The formula and its acceptance test #

In words: Unix seconds = 86,400 × days since 1970-01-01, plus 3,600 × hour + 60 × minute + seconds. Days since 1970-01-01 = 365 × (year − 1970) + leap days + days already elapsed in the target year. Leap days = one per leap year from 1972 through the year before the target, where a leap year is every year divisible by 4 except century years not divisible by 400 — 2000 counts. Days elapsed = the cumulative month lengths (31, 28 or 29, 31, 30, 31, 30, 31, 31, 30, 31, 30) up to the previous month, plus day − 1, using 29 for February in leap years.

Hold the result in a 32-bit integer; a signed one overflows in January 2038, so use an unsigned tag where the platform allows it. The acceptance test is fixed: 2000-01-01 00:00:00 must produce 946,684,800, which is 10,957 days × 86,400. Code that fails that value is wrong no matter how reasonable the panel looks.

Worked example: 2024-07-04 14:35:20 on the M221 RTC #

Inputs, all civil time as the PLC holds it: year 2024, month 7, day 4, hour 14, minute 35, second 20 — an illustrative live reading of the RTC words, with the HHMM field in %SW49 reading hex 1435 and the remaining fields decoded from their words by the same nibble rule.

Rule: days = 365 × (2024 − 1970) + leap days + elapsed days in 2024; then seconds = days × 86,400 + 3,600 × 14 + 60 × 35 + 20.

Arithmetic:

  • 365 × 54 = 19,710 days from the 54 whole years.
  • Leap years 1972 through 2023: 1972, 1976, and so on to 2020 → (2020 − 1972)/4 + 1 = 13 days.
  • Elapsed in 2024 before 4 July: 31 + 29 + 31 + 30 + 31 + 30 = 182, plus 4 − 1 = 3 → 185 days.
  • Days = 19,710 + 13 + 185 = 19,908.
  • Midnight: 19,908 × 86,400 = 1,720,051,200 s.
  • Time of day: 3,600 × 14 + 60 × 35 + 20 = 50,400 + 2,100 + 20 = 52,520 s.
  • Total: 1,720,051,200 + 52,520 = 1,720,103,720 s.

Result: the tag must read 1,720,103,720 exactly while the RTC fields say 14:35:20 on 4 July 2024. Decision: accept the conversion only when this value and the 946,684,800 check both match to the second; treat any mismatch as a stop condition. An error that is an exact multiple of 86,400 sits in the day count, a smaller one in the time-of-day terms.

Write the HMI clock with a global script #

  1. Compute the epoch value in the M221 and expose it as one 32-bit tag, or, if the program is validated and frozen, read the six decoded fields into the script and compute there. Prefer the PLC placement when a second HMI or a SCADA link also wants epoch time, so every consumer reads the same word.
  2. Map an HMI tag onto the epoch tag and watch it online; it must advance by 1 each second.
  3. In the script editor of your installed EOTE version, locate the system Time/Date object and write to it from the script. Object names and syntax differ between versions — the write has even been demonstrated in a pre-release build — so work from what your version exposes rather than a remembered name.
  4. Trigger the script from a timer at 30 s. A clock needs no faster service, and every-scan writes are needless load on the panel RTC.
  5. Write through two guards only: the PLC value must exceed a floor just under your commissioning date — 1,700,000,000 s is mid-November 2023 — and the panel clock must differ from the tag by more than 5 s. The drift guard stops second-by-second rewrites and the visible jumps they cause.

This write is the M221-specific half of a wider habit; the routine side of keeping a panel clock aligned from a controller is covered in setting HMI clock time from PC and PLC daily.

Know what hangs off the panel clock before the first write: a shifted epoch does more than mislabel alarms — a routine like scheduling the PLC by day of week from the HMI clock moves with it.

Prove it follows #

  • Read the RTC words and the panel display at the same moment; they must agree within one script period, 30 s.
  • Step the PLC clock by a few minutes from the programming software; the panel must follow on the next script cycle.
  • Fix the time zone once. Unix seconds are UTC-based: treat the PLC civil fields as the displayed time and set the panel offset to zero; if the PLC clock is set to UTC instead, set the panel offset to your zone. Two owners of one offset guarantee an offset-sized error.
  • Power-cycle the panel and confirm the clock returns from the PLC with no manual entry.
  • Set the PLC to 29 February 2024 and check the tag against an independent converter; a table that always uses 28-day Februaries returns 29 February and 1 March one day early.

When the panel holds PLC time through a power cycle and follows a forced clock step, the link is done.

Questions asked

What goes wrong if I feed the BCD words straight into the arithmetic?

Every field misreads. BCD packs each decimal digit into a nibble, so 35 minutes stored as hex 35 is 53 as a plain integer, and 14 hours as hex 14 is 20. The converted clock lands minutes and hours off in a pattern that still looks plausible on the panel, which is why the 2000-01-01 acceptance test matters.

How do I tell whether the words are BCD or already binary?

Watch them online against a wall clock. The tell is October: a BCD month of 10 shows as hex 10, which reads 16 in an unsigned view. If every field tracks the clock directly instead, the words are already binary and you skip the decode step entirely.

Can I set the M221 clock from the panel instead?

Yes, and it needs no epoch math: you write the BCD fields from screens. A published M221 diagnostic template for Operator Terminal Expert demonstrates that direction with ready-made date and time contents. It solves panel-set time only, though, and does not push PLC time into the panel clock, which is the case this article covers.

What happens to this tag in 2038?

A signed 32-bit integer tops out at 2,147,483,647 s, reached on 19 January 2038, and then wraps negative. Use an unsigned 32-bit tag where the platform allows it, which buys you to 2106, and remember that 2100 is not a leap year if a machine must genuinely run that long.

Does the same method work on other controllers without Unix words?

Yes. A TM172 was handled the same way, with a global script reading the controller time fields and writing the HMI Time/Date, because it lacks the Unix format just as the M221 does. Any controller that can publish epoch seconds into one readable tag works with an identical script.

Keep reading

  • How-to

    Setting HMI clock time from PC and PLC daily

    Connecting a PC or PLC to an HMI synchronizes nothing. Set the HMI clock once from the PC, then let a daily script triggered by a PLC bit copy the HMI clock into the FX clock registers D8013–D8019 through the PLC's time-write instruction, and verify each hop with an independent read-back; on an A series CPU only the register numbers change.

    HMI and touchscreens

  • Troubleshooting

    Why extending HMI system event display time reveals comms faults

    Set the system event display period to 20 seconds under Alarms > System events > Display period and put an alarm viewer on the screen the HMI opens at startup. Start the HMI before the PLC or PLCSIM so a connection rejection stays on screen long enough to read and photograph. The longer period is diagnostic visibility, not a fix: verify the HMI and CPU addresses and the connection configuration separately.

    HMI and touchscreens

  • Troubleshooting

    EcoStruxure Operator Terminal Expert download fails to HMI

    A failed Operator Terminal Expert download has three separate causes: Windows firewall blocking the transfer executable, fixable with inbound and outbound allow rules scoped to EcoStruxure Operator Terminal Expert.exe; a VMware virtual USB controller that needs its compatibility set to USB 1.1; or a terminal still running Vijeo Designer firmware, which no Operator Terminal Expert project can load until a manufacturer recovery converts it. Match the fix to the runtime you read on the terminal.

    HMI and touchscreens