XScript Manual · Chapter 20

DIO — digital IO

IO["name"] accesses a digital input or output registered in the Io Editor. Used in a condition or on the right-hand side of an assignment, IO["name"] reads the current value (ReadBit); assigning to it, as in IO["name"] = ON;, writes the output (WriteBit). Analog inputs and outputs use the same IO["name"] — see AIO — analog IO.

Example

xscript
// OUTPUT control
IO["O_Blow1"] = OFF;
IO["O_Blow2"] = ON;
IO["O_Blow2"].ToggleBit();
 
// INPUT check
if( IO["I_IFReadyPrev"] == false )
{
   return false;
}
 
// Set a digital output
IO["O_DigitalOutput"] = ON;
 
// Read a digital input state
bool isDigitalInputOn = IO["I_DigitalInput"];
 
// Read an analog input value ( pass a variable — it is filled after the call )
double analogInputValue = 0.0;
IO["AI_AnalogInput"].GetAnalogVoltage(/*ref*/analogInputValue);
 
// Using a digital output in a condition
if( IO["O_DigitalOutput"] )
{
   // the output is ON
}
else
{
   // the output is OFF
}

Real-world — wait for conveyor bowl detection

Wait with WaitConti until the sensor has been ON for 2 continuous seconds; if it is not detected within the time limit (false), raise an error alarm with ShowError and end the step. WaitConti(waitOn, TimeOut, ContiTime) waits up to TimeOut until the signal stays in the target state without a break for ContiTime. If the signal breaks midway the hold time starts again from zero, so momentary chatter does not pass.

xscript
// Wait up to 10 s until the sensor has been ON for 2 s (2000 ms) without a break
if( IO[cnvSensorName].WaitConti(true, 10000, 2000) == false )
{
   // Timeout — bowl not detected. Raise an error alarm and end the step
   ShowError(EB_Reset, 100, "conveyor bowl detect timeout");
   return false;
}
 
Log($"{cnvSensorName} : bowl detected");

Note To check immediately, without waiting, whether it is "ON for 2 continuous seconds right now", use CheckContiOn.

ShowError(buttonSet, code, message) raises an error alarm and does not wait — see SYS — system functions.

Check and event functions

Two script functions can be linked to each IO.

Link propertyCalled whenReturn value
CheckFunctionNameRight before a value different from the current one is written to an output IO (interlock check)Returning false blocks the output
EventFunctionNameThe IO state changesNot used
  • The functions must be in the Event module. The Check Fn / Event Fn [+] buttons of the Io Editor create IOCheck_IOName · IOEvent_IOName in the Event module and link them right away.
  • A function takes 0 to 2 arguments — none / (string name) / (string name, bool state) 4.0+. state is the value about to be written for a check function, and the new state for an event function. With 3 or more arguments the function is not called, and a check function blocks the output.
  • To link from a script, set the names in OnPostBuild of the Event module. A build links the names to functions right after OnPostBuild finishes, so a name changed later takes effect at the next build. A function name that is not in the Event module gives a build warning and is not linked.
  • A dedicated IO monitoring thread checks the states about every 10 ms and calls the event function of each IO that changed. Sleep inside an event function does not freeze the screen, but other IO changes are detected late until the function returns, so keep long work out of it. Event functions are not called in maintenance mode or during a build.
xscript
// Event module — link the function names in OnPostBuild, which runs when a build finishes
FUNCTION OnPostBuild()
{
   IO["I_U1CnvBowlDetect"].EventFunctionName = "IOEvent_CnvBowlSens";
   IO["I_U2CnvBowlDetect"].EventFunctionName = "IOEvent_CnvBowlSens";
   IO["O_Clamp"].CheckFunctionName = "IOCheck_O_Clamp";
   return true;
}
 
// Event function — name : the IO whose state changed , state : the new state
FUNCTION bool IOEvent_CnvBowlSens(string name, bool state)
{
   if( state == ON )
   {
      Log($"{name} : bowl detected");
   }
   return true;
}
 
// Check function — setOn : the value about to be written. Return false to block the output
FUNCTION bool IOCheck_O_Clamp(string name, bool setOn)
{
   if( setOn == ON && IO["I_DoorClosed"] == OFF )
   {
      LogError($"{name} : door is open");
      return false;
   }
   return true;
}

Methods

Read · write

MethodDescription
bool ReadBit(void)Reads the current state of this IO. With Inverse on, the value is inverted. Returns false if the board is not initialised
bool WriteBit(bool SetOn)Changes the output to SetOn. When the value differs from the current one, the check function runs first; if it returns false, or the IO is an input, nothing is written and false is returned
bool ToggleBit(void)Changes the output to the opposite state. Returns false for an input IO
bool SetDelayedOn(int delay)Schedules the output to turn ON after delay ms and returns immediately. A schedule still pending on the same IO is replaced by the new one. Returns false for an input IO
bool SetDelayedOff(int delay)Schedules the output to turn OFF after delay ms. Otherwise the same as SetDelayedOn

Wait · hold check

MethodDescription
bool Wait(bool waitOn, int TimeOut)Waits up to TimeOut ms for the IO to reach the waitOn state. Returns true as soon as it does, false on timeout
bool WaitConti(bool waitOn, int TimeOut, int ContiTime)Waits up to TimeOut ms for the IO to hold the waitOn state for ContiTime ms without a break. The hold time restarts when the state changes. Returns false on timeout
bool CheckContiOn(int timeMsec)Checks immediately, without waiting — true if the IO is ON now and has stayed ON for at least timeMsec ms
bool CheckContiOff(int timeMsec)Checks immediately, without waiting — true if the IO is OFF now and has stayed OFF for at least timeMsec ms

Note CheckContiOn · CheckContiOff use the hold time measured by the IO monitoring thread. The first call switches on hold monitoring for that IO and starts timing from then, so the result is false right after the first call. To time from the build onward, switch it on in advance in OnPostBuild with IO["name"].UseContiCheck = true;.

Other

MethodDescription
bool Contains(string keyword)True if keyword appears in the name, ID, group name or description of this IO (case-insensitive)
bool InitBoard(void)Initialises the IO board and returns whether it succeeded. If it is already initialised, it is not initialised again and true is returned
void RunEventFuncion(bool onoff)Forcibly runs the linked event function with the state value onoff. Use the function name exactly as spelled ( Funcion ). It does not run in maintenance mode
void ClearLinkModule(void)Unlinks the check / event functions and restores the link names to the values set in the Io Editor (names changed by the script are removed). The restored names are linked again at the next build; until then neither function is called

Properties

PropertyTypeDescription
NamestringIO name
IDstringIO ID (e.g. X001)
DescriptionstringDescription entered in the Io Editor
BoardTypestringBoard type
GroupNamestringGroup name (read only)
ModuleintModule number (read / write)
IndexintIndex within the module (read / write)
InverseboolWhether the read value is inverted — B contact (read / write)
InitOkboolWhether the board this IO is fitted to has been initialised (read only)
InfostringInformation string in ID//Description format (read only)
MessagestringA message attached to this IO (read / write). Not saved; setting it writes a log entry
CheckFunctionNamestringCheck function name (read / write)
EventFunctionNamestringEvent function name (read / write)
UseContiCheckboolWhether hold monitoring (CheckContiOn · CheckContiOff) is used (read / write). Switched on automatically by the first call of either function
DriverConnectedboolWhether the driver (device) this IO belongs to is connected (read only). Only drivers that report a connection state give a value; otherwise false
FirmwareVersionstringFirmware version of the device this IO belongs to (read only). Only drivers that provide one, such as QMachineNet, give a value; otherwise an empty string

Notes

  • Inverse applies only to the read value (B-contact inputs). ReadBit · Wait · WaitConti · conditions · the state of event functions all see the inverted value, while output writes (WriteBit · assignment) are not inverted.
  • To wait for an input to change, do not build a ReadBit loop yourself — use Wait · WaitConti, which include the timeout and the decision.
  • To wait until a signal settles use WaitConti; to check immediately whether it has already settled use CheckContiOn · CheckContiOff.
  • SetDelayedOn · SetDelayedOff schedule a delayed output change; they do not filter input signals.
  • When an IO name is written as a string literal, the build checks that it is registered and warns about unknown names. Assigning a value to an input IO named that way is reported as a compile error — see Compile diagnostics.
Subscribe