Until now, we have received data from the outside world.
Now we will learn to send instructions and read specific values from GPSS-Plus to an OPC-UA, MQTT, or any other device connected through middleware.js.
Writing to a Device
Giving a command to an actuator is as simple as assigning a value and sending it:
ASSIGN values, { Device_Motor_Enabled: 1 }
BRIDGE_WRITE { name:bridge1, VALUES:V$values ,TRIGGER:on_write}
This writes to the Device_Motor_Enabled variable of the device, turning the motor on (1 = ON, 0 = OFF).
The structure of the command is always the same:
- VALUES: A key-value set with the variables to be modified.
- TRIGGER: The procedure that will execute when the middleware confirms delivery.
Reading a Variable State
Reading is performed in an analogous way:
ASSIGN values, ["Device_Motor_Enabled"]
BRIDGE_READ {name:bridge1, trigger:on_read, VALUES:V$values}
In this case, the list ["Device_Motor_Enabled"] indicates which variables we want to query.
The value will arrive asynchronously, and will be received within the corresponding trigger (on_read).
Asynchronous vs. Synchronous Execution
By default, READ and WRITE operations are asynchronous:
the GPSS-Plus engine continues its execution without stopping to wait for the device's response.
This is standard for physical systems, where communication can take several hundred milliseconds.
If the resource is immediate (e.g., a local file or an internal database), you can force a synchronous operation with:
BRIDGE_READ { name:bridge1, VALUES:V$values, SYNC:1, trigger:on_read }
In this mode, the entity and the engine block until the response is received.
This should only be used in controlled contexts, as stopping the simulator's time affects the entire system.
Example
SYSTEM {TYPE:OPTIONS, REAL_TIME:1}
Graphic {Type:TEXT,Name:Text1,X:653,Y:294}
Graphic {Type:TEXT,Name:Text2,X:653,Y:194}
Graphic {Type:TEXT,Name:TextError,X:440,Y:329,Text:"On error...."}
Initial options,{user:"user", pass:"1234"}
BRIDGER {NAME:bridge1,X:443,Y:405
,SERVER:"wss://bridger.gpss-plus.com:3000"
,CLIENT:"opc.tcp://opcua.gpss-plus.com:4840"
,OPTIONS:V$options
,on_error:bridge1_on_error
}
POSITION {NAME:POS1,X:209,Y:347}
POSITION {NAME:POS2,X:409,Y:347}
START 100
;*****************************************************
GENERATE 3,0,0,10 {NAME:GEN1,X:141,Y:351}
ASSIGN values, ["Device_Motor_Enabled"]
BRIDGE_READ {name:bridge1, trigger:on_read, VALUES:V$values}
ADVANCE 5,0 {TO:POS1}
if (D$N %2 == 0 )
ASSIGN values, { Device_Motor_Enabled: 0 }
BRIDGE_WRITE { name:bridge1, VALUES:V$values ,TRIGGER:on_write}
else
ASSIGN values, { Device_Motor_Enabled: 1 }
BRIDGE_WRITE { name:bridge1, VALUES:V$values ,TRIGGER:on_write}
endif
ADVANCE 5,0 {TO:POS2}
ENDGENERATE 1
;----------------------------------------------------------------------------
procedure on_write
move {name:Text1,TEXT:"AC1$ WRITE P$(PARAM_A)"}
TERMINATE_VE
endprocedure
;----------------------------------------------------------------------------
procedure on_read
move {name:Text2,TEXT:"AC1$ READ P$(PARAM_A) \n:: Value: P$(PARAM_A.Device_Motor_Enabled)"}
TERMINATE_VE
endprocedure
;----------------------------------------------------------------------------
procedure bridge1_on_error
move {name:TextError,TEXT:"AC1$ P$PARAM_A"}
TERMINATE_VE
endprocedure