Knowledge Base

Semaphore formatting function s(Source, TrueOutput, FalseOutput)

The semaphore formatting function provides a convenient way of converting a semaphore value to something readable. It “enables” one of two alternative “messages”, depending on whether a semaphore value is true or false. As we shall see, it extends that idea quite a bit further than just semaphores and just text messages. For example:

#HMI Print(s(*sHeater, "Heater is on ", "Heater is off")

If the semaphore sHeater is True, this will display Heater is on . Otherwise it will display Heater is off.

Source specifiers

The following are the allowable source specifiers

FormMeaningExample
*sNameThe True/False value of a RAM semaphore. It must have been defined in a defSEM.#HMI Print(s(*sMySem, "True", "False"))
=xContents of the X register. Any non-zero value is treated as True.#HMI Print(s(=x, "NonZero", "Zero"))
=yContents of the Y register. Any non-zero value is treated as True.#HMI Print(s(=y, "NonZero", "Zero"))
=hContents of the H register. Any non-zero value is treated as True.#HMI Print(s(=h, "NonZero", "Zero"))
=mContents of the M register. Any non-zero value is treated as True.#HMI Print(s(=m, "NonZero", "Zero"))
=sContents of the S register. Any non-zero value is treated as True.#HMI Print(s(=s, "NonZero", "Zero"))
ConstantContents of an EQUated constant. Any non-zero value is treated as True.Foo EQU 56...#HMI Print(s(Foo, "NonZero", "Zero"))
A number 0 – 255An immediate numeric value. Any non-zero value is treated as True.#HMI Print(s(165, "NonZero", "Zero"))
index/jndex modifiers

RAM semaphore references can also have index/jndex modifiers

FormWhat is sourcedExample code
*+ssWhatever is in RAM semaphore ss, with IasJ: applied#HMI Print(s(*+sMySem, "True", "False"))
*-ssWhatever is in RAM semaphore ss, with NoJ: applied#HMI Print(s(*-sMySem, "True", "False"))

NOTE: The modifier (+ or -) must be after the asterisk. jndexing applies only to code running inside a MultiTrack task.

TrueOutput, FalseOutput arguments

The arguments defining what is to be displayed for the true and false alternatives can be any of:

  • A literal text string in quotes;
  • Any other formatting function.

The second of the above masks a great deal of power in this formatting function.

Examples:

  • “Plain vanilla” selection of two text alternatives:
#HMI Print(s(sAlive, "Life is good", "Farewell, oh cruel world")
  • Switch between number and text. In this example, if the sSensorFail semaphore is true, a fault message is displayed. Otherwise, the value of a floating point variable is displayed.
#HMI Print(s(*sSensorFail, "Bad sensor!", f(fTemperature, 5, 1))
  • Nested semaphore formatting functions. This line effectively checks two fault conditions and displays an alarm message if either is tripped, otherwise displays “All good”.
#HMI Print(s(*sSensorFail, "Bad sensor", s(*sOverflow, "Overflow", "All good")))