jsonWebSocketServer

The jsonWebSocketServer() function block implements a WebSocket server for reading and writing PLC variables. It is the only function block necessary for communicating between a PLC and a web-based HMI application. In many cases, HMI update rates of 60 frames per second are possible. This function block should typically be called in a low priority task class (Cyclic #7 or Cyclic #8) with a cycle time set to balance responsiveness and CPU load (2 - 50 ms). The cache for a single client is about 13.8 MB. Supporting 5 clients requires about 69 MB.

NOTE: Multi-client is not supported on systems with less than 256 MB of RAM.

Function Block Interface

I/O Parameter Description Data type Default Value
IN pCacheArray Address of the cache variable to use. Number of caches determines the number of clients UDINT (jsonCache_typ[])
IN maxClients Max number of concurrent clients. 0 will assume 1 UINT 0
IN ServerIP IP Address of the server - leave this empty to listen on all ethernet interfaces STRING Empty
IN ServerPort Server port on which to listen for communications UINT 8000
IN BufferSize Size of memory to allocate for internal data buffers UDINT 100000
IN MaxIterations Maximum number of iterations to perform while processing UDINT 10000
IN AcknowledgeError Acknowledge function block errors BOOL
OUT ClientInfo Information about Clients jsonWSS_client_info_typ[]
OUT Error An error has occurred BOOL
OUT ErrorID ID number of the error UINT
OUT ErrorString Description of the error STRING

ClientInfo

I/O Parameter Description Data type Default Value
OUT Connected Client connection status BOOL
OUT TimeSinceLastRequest_ms Time, in milliseconds, since the last request was received from client UDINT
OUT ClientIP IP Address of client STRING
OUT ClientPort Port of client UINT

Protocol

The WebSocket server responds to read and write requests using the following protocol.

{
  "type": "read",
  "data": [
    "globalVar", 
    "myTask:localVar"
  ]
}
{
  "type": "readresponse",
  "data": [
    {
      "globalVar": 25
    },
    {
      "myTask:localVar": {
        "elem1": "Hello",
        "elem2": 123.456
      }
    }
  ]
}
{
  "type": "write",
  "data": {
    "globalVar": 52,
    "myTask:localVar": {
      "elem1": "Hi!",
      "elem2": 654.321
    }
  }
}
{
  "type": "writeresponse",
  "data": {
    "globalVar": 52,
    "myTask:localVar": {
      "elem1": "Hi!",
      "elem2": 654.321
    }
  }
}

Note: Undefined or unsupported types will respond with "undefined"

Example Program

Variable Declaration

VAR
	jsonCache : jsonCache_typ[0..2] := (0);
	jsonWebSocketServer_0 : jsonWebSocketServer;
END_VAR

Structured Text Program

PROGRAM _INIT
  
  // pCache is required
	jsonWebSocketServer_0.pCache := ADR(jsonCache);
  jsonWebSocketServer_0.numClients := SIZEOF(jsonCache)/SIZEOF(jsonCache[0]);
	
	// ServerIP, ServerPort, BufferSize, and MaxIterations are optional
  //jsonWebSocketServer_0.ServerIP := '192.168.0.61';
	//jsonWebSocketServer_0.ServerPort := 8001;
	//jsonWebSocketServer_0.BufferSize := 1000000;
	//jsonWebSocketServer_0.MaxIterations := 100000;

	// Call jsonWebSocketServer once in the INIT routine to allocate internal memory
	jsonWebSocketServer_0();

END_PROGRAM

PROGRAM _CYCLIC

	// Call jsonWebSocketServer cyclically to serve variables
	jsonWebSocketServer_0();

END_PROGRAM