| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Test on: LADDER-EDITOR
The Ladder PLC Editor is a web-based application designed for creating, editing, and simulating ladder logic diagrams for Programmable Logic Controller (PLC) programming. It leverages WebSockets for real-time communication with a server, enabling seamless program management, uniform network dimensions, and dynamic visual feedback through cell state updates.
The code was created entirely with AI, so I don't consider it my own creation and therefore release it without any restrictions for the use of all mankind.
The editor connects to a server at ws://<IP>/ws when is from webeditor was loaded, using WebSockets, facilitating real-time data exchange for program persistence and simulation. Key functionalities include:
Below, we explore each feature, detailing their implementation and interaction with WebSockets.
The connection status is tracked via the wsStatus variable, which can be:
The updateIndicator function reflects this status in the user interface by changing the color of a circular indicator (#ws-indicator):
| Status | Indicator Color | Description |
|---|---|---|
| Disconnected | Red | No server connection. |
| Connected, Not Running | Yellow | Connected, server not simulating. |
| Connected, Running | Green | Connected, server actively simulating. |
The saveNetworks function handles program saving, adapting to the connection status:
The initiateLoad function manages program loading:
The validateNetworkData function ensures loaded data is compatible:
This robust validation prevents errors from malformed data, whether loaded from the server or a file.
The editor supports uniform network dimensions, controlled by the sameDimensionsFlag boolean, received from the server in ws.onmessage when data.flag === "sameDimensions".
Adding Networks (addNetwork):
Resizing Networks (resizeNetwork):
When sameDimensionsFlag is enabled, the editor ensures all networks maintain identical dimensions, simplifying program design for applications requiring consistent layouts. This is particularly useful in PLC programming, where uniform network sizes can align with hardware constraints or design standards.
The editor provides real-time visual feedback during simulation by highlighting active cells, controlled by server updates.
Server Updates:
Styling:
Reset on Status Change:
This feature enhances the simulation experience by visually representing the ladder logic’s execution, allowing users to monitor program behavior in real time. The yellow highlight is intuitive, aligning with common conventions in PLC programming interfaces.
While not directly tied to WebSockets, the editor includes a blinking feature for cell selection:
The following table summarizes the WebSocket event handlers and their roles:
| Event | Function | Description |
|---|---|---|
| onopen | Sends { action: "get_flag" } | Initiates connection, requests server flags, sets status to "connected_not_running". |
| onmessage | Processes flags, loads, or states | Handles sameDimensions flag, load_response, and status/cell state updates. |
| onclose | Resets status and UI | Sets wsStatus to "disconnected", updates indicator, clears cell highlights. |
| onerror | Resets status and UI | Same as onclose, handling connection errors. |
The following functions are central to WebSocket integration:
| Function | Description |
|---|---|
| saveNetworks | Saves network data to the server or locally based on connection status. |
| initiateLoad | Requests data from the server or triggers local file upload. |
| addNetwork | Creates new networks, respecting sameDimensionsFlag for uniform sizes. |
| resizeNetwork | Resizes one or all networks based on sameDimensionsFlag. |
| applyCellStates | Updates cell highlights based on server-provided state updates. |
| validateNetworkData | Ensures loaded data is valid, correcting symbols and data entries. |
Connection Establishment:
Message Handling:
Connection Closure:
Sent when the WebSocket connection opens to request the "sameDimensions" flag, which likely indicates if the server expects networks with consistent dimensions.
JSON Structure:
{
"action": "get_flag"
}Sent to save the current network configurations to the server. The data field contains an array of network objects.
JSON Structure:
{
"action": "save",
"data": [network objects]
}Example:
{
"action": "save",
"data": [
{
"id": 0,
"rows": 2,
"cols": 2,
"networkData": [
[
{
"symbol": "NO",
"bar": false,
"data": [
{
"name": "value",
"type": "I",
"value": "0.0"
}
]
},
{
"symbol": "CONN",
"bar": false,
"data": []
}
],
[
{
"symbol": "NOP",
"bar": true,
"data": []
},
{
"symbol": "Q",
"bar": false,
"data": [
{
"name": "value",
"type": "Q",
"value": "0.0"
}
]
}
]
]
}
]
}Sent to request network configurations from the server.
JSON Structure:
{
"action": "load"
}Response to the "get_flag" action, providing the value of the "sameDimensions" flag.
JSON Structure:
{
"flag": "sameDimensions",
"value": boolean
}Example:
{
"flag": "sameDimensions",
"value": true
}Response to the "load" action, providing an array of network objects. The structure mirrors the save message's data field.
JSON Structure:
{
"action": "load_response",
"data": [network objects]
}Example:
{
"action": "load_response",
"data": [
{
"id": 0,
"rows": 2,
"cols": 2,
"networkData": [
[
{
"symbol": "NO",
"bar": false,
"data": [
{
"name": "value",
"type": "I",
"value": "0.0"
}
]
},
{
"symbol": "CONN",
"bar": false,
"data": []
}
],
[
{
"symbol": "NOP",
"bar": true,
"data": []
},
{
"symbol": "Q",
"bar": false,
"data": [
{
"name": "value",
"type": "Q",
"value": "0.0"
}
]
}
]
]
}
]
}The server sends status updates to indicate whether the simulation is running or not.
When the simulation is running, the server sends cell state updates to reflect the active/inactive status of cells.
JSON Structure:
{
"status": "running",
"cell_states": [
{
"networkId": number,
"row": number,
"col": number,
"state": number
},
...
]
}Example:
{
"status": "running",
"cell_states": [
{
"networkId": 0,
"row": 0,
"col": 0,
"state": 1
},
{
"networkId": 0,
"row": 0,
"col": 1,
"state": 0
},
{
"networkId": 0,
"row": 1,
"col": 0,
"state": 0
},
{
"networkId": 0,
"row": 1,
"col": 1,
"state": 1
}
]
}When the simulation is not running, the server sends a simple status message.
JSON Structure:
{
"status": "not_running"
}Represents a single network in the ladder logic editor.
| Field | Type | Description |
|---|---|---|
| id | Number | Unique identifier for the network. |
| rows | Number | Number of rows (default 8, max 100). |
| cols | Number | Number of columns (default 8, max 100). |
| networkData | Array | 2D array of cell objects representing the grid. |
Each cell in networkData is an object with:
| Field | Type | Description |
|---|---|---|
| symbol | String | Symbol type (e.g., "NOP", "CONN", "NO", "Q"). |
| bar | Boolean | Indicates presence of a vertical bar. |
| data | Array | Array of data entries for the symbol. |
Each data entry in data is an object with:
| Field | Type | Description |
|---|---|---|
| name | String | Name of the data entry (e.g., "value"). |
| type | String | Data type (e.g., "I", "Q", "M"). |
| value | String | Value of the data entry (e.g., "0.0"). |
Represents the state of a cell during simulation.
| Field | Type | Description |
|---|---|---|
| networkId | Number | Identifier of the network. |
| row | Number | Row index of the cell. |
| col | Number | Column index of the cell. |
| state | Number | State of the cell (e.g., 1 for active, 0 for inactive). |
The editor supports various ladder logic symbols, each with specific data entry requirements. Examples include:
| Symbol | Cells | Data Entries (Name, Allowed Types) | Pins (Left, Right) |
|---|---|---|---|
| NOP | 1 | [] | [false, false] |
| CONN | 1 | [] | [true, true] |
| NO | 1 | [value, I,Q,M,D,T,C,IW,K,QW] | [true, true] |
| Q | 1 | [value, Q] | [true, true] |
Supported data types include: NONE, M, Q, I, Cd, Cr, Td, Tr, IW, QW, C, T, D, CSTR, REAL, MS, 10MS, 100MS, SEC, MIN, K.
| Back | FazBrowse Home | New Git URL |