WebSPS is a JavaScript-based implementation of a SPS (Programmable Logic Controller), providing a flexible and extensible platform for executing industrial automation scripts directly in the browser. This library allows you to load and execute instruction list (IL) code (AWL in german), manage input/output registers, and simulate PLC operations with JavaScript.
Tip
Example page for WebSPS.js is available at: oliverweinhold.de/websps
- Installation
- Usage
- API Reference
- Supported IL Commands
- Examples
- License
- Author
- Support
- Contributions
To include WebSPS.js in your project, you can download the source code from GitHub and include it as a module in your JavaScript environment.
git clone https://github.com/OliverWeinhold/WebSPS.gitImport the SPS class into your JavaScript file:
import { SPS } from './path/to/WebSPS.js';You can create an SPS instance by initializing it with the desired number of input, output, and flag registers. By default, each of these is set to 64.
const mySPS = new SPS(64, 64, 64);To load IL code into the SPS, use the loadCode method. This method converts the IL code into JavaScript that can be executed by the SPS.
const ILCode = `
U E1
= A1
UN E2
= A2
`;
const result = mySPS.loadCode(ILCode);
if (result === 0) {
console.log('Code loaded successfully');
} else {
console.error('Failed to load code');
}To start the SPS, call the start method. You can also pause, resume, or stop the SPS using the corresponding methods.
mySPS.start();
mySPS.pause();
mySPS.stop();Inputs and outputs can be set and retrieved using the provided setter and getter methods.
mySPS.setInputs([1, 0, 1, 1, ...]);
const outputs = mySPS.getOutputs();
console.log(outputs);new SPS(Inputs = 64, Outputs = 64, Flags = 64)- Inputs: Number of input registers (default: 64).
- Outputs: Number of output registers (default: 64).
- Flags: Number of flag registers (default: 64).
-
loadCode(ImputCode, errorCallback): Loads IL code into the SPS.- ImputCode: A string or array of IL code strings.
- errorCallback: A function to handle errors during code loading.
- Returns:
0if the code is valid,-1otherwise.
-
start(): Starts the SPS.- Returns:
0if the SPS starts successfully,-1if already running.
- Returns:
-
stop(): Stops the SPS and resets all registers.- Returns:
0on successful stop.
- Returns:
-
pause(): Pauses the SPS.- Returns:
0on successful pause.
- Returns:
-
setCycleTime(delay): Sets the cycle time in milliseconds (default: 10ms).- delay: The time in milliseconds between each SPS cycle (10-1000ms).
- Returns:
0if the cycle time is set successfully,-1otherwise.
-
setFlags(flags): Sets the internal flag registers.- flags: An array of flag values.
- Returns:
0on successful update.
-
setInputs(inputs): Sets the input registers.- inputs: An array of input values.
- Returns:
0on successful update.
-
getInputs(): Retrieves the current state of the input registers.- Returns: An array of input states.
-
getFlags(): Retrieves the current state of the flag registers.- Returns: An array of flag states.
-
getOutputs(): Retrieves the current state of the output registers.- Returns: An array of output states.
-
getState(): Gets the current state of the SPS.- Returns:
0for stopped,1for running,2for paused.
- Returns:
-
getRuntimeCounter(): Gets the current runtime counter.- Returns: The number of times the runtime has been called.
-
dataAvailable(): Checks if the output registers were updated after an input change.- Returns:
trueif the output registers were updated.
- Returns:
The following IL commands are currently supported by the SPS implementation:
-
Logical Operations:
U: AND operationO: OR operationX: XOR operationNOT: NOT operationUN: AND NOT operationON: OR NOT operation
-
Instructions:
=: Assigns the result of the logic operation to the specified operandS: Sets the specified operand to trueR: Resets the specified operand to false
-
Operands:
E: Input registerA: Output registerM: Flag register
-
Brackets:
(: Opens a new logical block): Closes a logical block
Further commands (like timers, XN) are planned
const mySPS = new SPS(8, 8, 8);
const ILCode = `
U E1
= A1
UN E2
S A2
`;
mySPS.loadCode(ILCode);
mySPS.setInputs([1, 0, 1, 0, 0, 0, 0, 0]);
mySPS.start();
console.log(mySPS.getOutputs());You can handle more complex automation tasks by combining multiple networks of IL code.
const ILCodeArray = [
`U E1
= A1
\`,
\`
UN E2
R A1
`
,
`S M3`];
mySPS.loadCode(ILCodeArray);
mySPS.setInputs([1, 1, 0, 0, 0, 0, 0, 0]);
mySPS.start();
console.log(mySPS.getOutputs());This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.
Please note that the maintenance of this library is not continuous and not guaranteed. For support, feature requests, or bug reporting, please use GitHub.
Contributions are welcome! Feel free to open a pull request or issue on GitHub!