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
| Form | Meaning | Example |
*sName | The True/False value of a RAM semaphore. It must have been defined in a defSEM. | #HMI Print(s(*sMySem, "True", "False")) |
=x | Contents of the X register. Any non-zero value is treated as True. | #HMI Print(s(=x, "NonZero", "Zero")) |
=y | Contents of the Y register. Any non-zero value is treated as True. | #HMI Print(s(=y, "NonZero", "Zero")) |
=h | Contents of the H register. Any non-zero value is treated as True. | #HMI Print(s(=h, "NonZero", "Zero")) |
=m | Contents of the M register. Any non-zero value is treated as True. | #HMI Print(s(=m, "NonZero", "Zero")) |
=s | Contents of the S register. Any non-zero value is treated as True. | #HMI Print(s(=s, "NonZero", "Zero")) |
Constant | Contents of an EQUated constant. Any non-zero value is treated as True. | Foo EQU 56...#HMI Print(s(Foo, "NonZero", "Zero")) |
| A number 0 – 255 | An 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
| Form | What is sourced | Example code |
*+ss | Whatever is in RAM semaphore ss, with IasJ: applied | #HMI Print(s(*+sMySem, "True", "False")) |
*-ss | Whatever 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")))