Scripting

The simulator can be automated from a script, the simulator uses the Microsoft Script Host, only the VB language is currently selectable.

The primary function of a script is to allow emulation of normal PLC activity. Activity can be based on SCADA/HMI controls, and be in the form of a response action, or be totally autonomous. To enable the execution of a script, type in the name of the script VBS file in the space provided in the "Training" settings dialog. Typical to this kind of scripting, some 'host-automation' functions have been provided. The functions are accessible in your script and let you read and modify modbus registers in the simulator/host.

Script function reference:

  • GetRegisterValue (REG_TYPE as long , ADDRESS as long) As int
    Returns the current value in the modbus/AB register specified.
    REG_TYPE = register type 0=input, 1=output,2=analog in, 3=holding reg. 4=extended reg. (For AB, this is the file #.)
    ADDRESS = register or I/O point number (Zero always corresponds to the first item.)
    E.g. 0 is similar to 40001 if REG_TYPE is 3
    E.g. 63 would be 64th output coil if REG_TYPE=1
  • SetRegisterValue (REG_TYPE as long, ADDRESS as long, REGISTERVALUE as long) As none
    Set the value in the modbus/AB register specified.
    REG_TYPE = register type 0=input, 1=output,2=analog in, 3=holding reg. 4=extended reg. (For AB, this is the file #.)
    ADDRESS = register or I/O point number (Zero always corresponds to the first item.)
       E.g. 0 is similar to 40001 if REG_TYPE is 3
       E.g. 63 would be 64th output coil if REG_TYPE=1
    REGISTERVALUE = register value, permitted range (-32767 to +32767) other values are clipped, and thus undefined
  • DisableStation (STATIONID as int) none
    Disable a modbus slave station. This function can be used to take the station out of the ring, as if it was not present in the system/network
    STATIONID = modbus station number (valid values 0 to 255; 0 = broadcast station)
  • EnableStation (STATIONID as int) None
    Enable a modbus slave station. This function can be used to restore a station to the ring. see DisableStation
    STATIONID = modbus station number (valid values 0 to 255; 0 = broadcast station)
  • DisplayAbout( ) None
    Display the application about box, the script is suspended until the "About" window is closed by the user.
  • GetLastRunTime( ) As long
    Return the duration in milliseconds that it took for the last execution of the script. This value will be -1 on the first script execution, or if the scrip fails to execute to completion without errors.
  • StationEnabled (STATIONID as int) As long
    Return the enable state of the station (0=disabled, 1=enabled)
  • TransmitRegisters (STATIONSRC as int, STATIONDEST as int, REG_TYPE as long, ADDRESS as long, REGISTERS as long) As long
    Sends a value to the desired slave PLC (Simulation must be in and support master mode.)
    Currently not supported on MODBUS.
    STATIONSRC = Allen-Bradley Source station (ignored for modbus protocol).
    STATIONDEST = Destination or target station ID register type 0=input, 1=output,2=analog in, 3=holding reg. 4=extended reg. (For AB, this is the file #)
    REG_TYPE = register type 0=input, 1=output,2=analog in, 3=holding reg. 4=extended reg. (For AB, this is the file #)
    ADDRESS = register or I/O point number (Zero always corresponds to the first item.)
       E.g. 0 is similar to 40001 if REG_TYPE is 3
       E.g. 63 would be 64th output coil if REG_TYPE=1
    REGISTERS = Number of registers to transmit, protocol specific framing or PDU limits are not tested before sending.
  • AddDebugString (STATIONID as string) As None
    Sends text to the communications debugger, the text will appear with a double hash ## in front of it.
  • TransactionBusy () As long
    Returns TRUE if this simulation device supports master-mode, and is busy with a transaction at this time.
Doxygen listing of the interface from C++ source for the curious.

Sample script code:

 
dim x
dim n
dim runtime
dim station

n=0
for n=0 to 240
x = getregistervalue(3,n)
SetRegisterValue 3, n, x+1
next
runtime = Getlastruntime
SetRegisterValue 3, 241, runtime

if (StationEnabled (station)) then
Disablestation station
else
enablestation station
end if

SetRegisterValue 3, 242, station

station = station + 1
if station > 12 then station = 0

The above sample code illustrates how to increment a few registers, and also turn the communication for the 1st 12 stations on and off continuously. The script runs on every animation update. 

A most-way decent sample script has been provided by Vikram Lele which generates a sawtooth, it's a really nice example. Simply right-click the link and save, then rename it to .VBS .

Language Help

The script language is a variant of VB-Script, so the Visual basic language reference is a reasonable place to start, but can be highly missleading. Un-fortunately VBA is not heavily documented, although anyone who has written over 2000 lines of classic Visual Basic, or tonnes of Excel VBA Macros will slip right into this. There are some fundamental differences, but do not worry, if I cannot compile your script, I do tell you rougly which line is illegal and why.

Warning: The scripting feature does not let you do whatever you please, it was never designed to let you run more than a few lines of code, communicate with external programs or code, or perform complex calculation nor manipulate large amounts of data. Hence it's limited to things like the sample above, and if you can take the CPU hit, longer programs of a few thousand lines.

The scripting function does not support Java, Java is do-able, if someone wants to add the few lines of needed code and dialog options needed please feel free to ask for directions.

Č
ċ
plcsim.txt
(3k)
Conrad Braam,
31 Mar 2010 12:14