Skip to content

Latest commit

 

History

History
326 lines (239 loc) · 11.6 KB

server.md

File metadata and controls

326 lines (239 loc) · 11.6 KB

S7Server

API - Administrative functions


S7Server.Start([callback])

Starts the server and binds it to the IP address specified in the previous call of StartTo(). If StartTo() was not previously called, 0.0.0.0 is assumed as IP address.

  • The optional callback parameter will be executed after completion

If callback is not set the function is blocking and returns true on success or false on error.
If callback is set the function is non-blocking and an error argument is given to the callback.

S7Server.StartTo(ip[, callback])

Starts the server and binds it to the specified IP address and the IsoTCP port.

  • ip PLC/Equipment IPV4 Address ex. “192.168.1.12”
  • The optional callback parameter will be executed after completion

If callback is not set the function is blocking and returns the CPU status on success or false on error.
If callback is set the function is non-blocking and an error argument is given to the callback.

S7Server.Stop([callback])

Stops the server, disconnects gracefully all clients, destroys al S7 workers and unbinds the listener socket from its address.

  • The optional callback parameter will be executed after completion

If callback is not set the function is blocking and returns the CPU status on success or false on error.
If callback is set the function is non-blocking and an error argument is given to the callback.

S7Server.GetParam(paramNumber)

Returns an internal server parameter.

  • paramNumber One from the parameter list below

Name Value Description
S7Server.LocalPort 1 Socket local port
S7Server.WorkInterval 6 Socket worker interval
S7Server.PDURequest 10 Initial PDU length request
S7Server.MaxClients 11 Max clients allowed

Returns the parameter value on success or false on error.

S7Server.SetParam(paramNumber, value)

Sets an internal server parameter.

  • paramNumber One from the parameter list above
  • value New parameter value

Returns true on success or false on error.

S7Server.SetResourceless(value)

Sets the server to resourceless mode.

  • value new value

Returns true on success or false on error.

API - Memory functions


S7Server.RegisterArea(areaCode[, index], buffer)

Registers a memory area in the server. That memory block will be visible by the clients.

  • areaCode Area identifier (see table below)
  • index DB number if areaCode equals srvAreaDB, otherwise ignored
  • buffer User buffer

Returns true on success or false on error.

S7Server.UnregisterArea(areaCode[, index])

Unregisters a memory area in the server.

  • areaCode Area identifier (see table below)
  • index DB number if areaCode equals srvAreaDB, otherwise ignored

Returns true on success or false on error.

S7Server.GetArea(areaCode[, index])

Gets the content of a previously registered memory area block.

  • areaCode Area identifier (see table below)
  • index DB number if areaCode equals srvAreaDB, otherwise ignored

Returns a buffer object.

S7Server.SetArea(areaCode[, index], buffer)

Sets the content of a previously registered memory area block.

  • areaCode Area identifier (see table below)
  • index DB number if areaCode equals srvAreaDB, otherwise ignored
  • buffer Buffer object

S7Server.LockArea(areaCode[, index])

Locks the memory area so that a server worker thread is blocked on access attempt until the lock is released with UnlockArea().

  • areaCode Area identifier (see table below)
  • index DB number if areaCode equals srvAreaDB, otherwise ignored

S7Server.UnlockArea(areaCode[, index])

Unlocks a previously locked memory area.

  • areaCode Area identifier (see table below)
  • index DB number if areaCode equals srvAreaDB, otherwise ignored

Area Value Description
S7Server.srvAreaPE 0 Process inputs
S7Server.srvAreaPA 1 Process outputs
S7Server.srvAreaMK 2 Merkers
S7Server.srvAreaCT 3 Counters
S7Server.srvAreaTM 4 Timers
S7Server.srvAreaDB 5 DB

API - Event functions


S7Server event: 'event'

Emitted on server events.

  • event Event object

Event object:

{
  EvtTime;    // <Date>   Date
  EvtSender;  // <String> Sender
  EvtCode;    // <Number> Event code
  EvtRetCode; // <Number> Event result
  EvtParam1;  // <Number> Param 1 (if available)
  EvtParam2;  // <Number> Param 2 (if available)
  EvtParam3;  // <Number> Param 3 (if available)
  EvtParam4;  // <Number> Param 4 (if available)
}

Example:

var s7server = new snap7.S7Server();

s7server.on("event", function(event) {
    console.log(s7server.EventText(event));
});

s7server.StartTo('127.0.0.1');

S7Server event: 'readWrite'

Emitted on every read/write event. Only available in resourceless mode.

  • sender IPv4 address of the sender
  • operation Operation type
  • tagObj Tag object
  • buffer Buffer object
  • callback Callback function

The server worker thread is blocked until callback is called. Therefore calling is crucial, to prevent a deadlock in the worker thread.
On a read event the callback expects a buffer as argument that is provided to the client. You can use the buffer argument which is an empty buffer of the correct size.

Operation type Value Description
S7Server.operationRead 0x00 Read operation
S7Server.operationWrite 0x01 Write operation

Tag object:

{
  Area;     // <Number> Area code (DB, MK,…)
  DBNumber; // <Number> DB number (if any or 0)
  Start;    // <Number> Offset start
  Size;     // <Number> Number of elements
  WordLen;  // <Number> Tag WordLength
}

Example:

var s7server = new snap7.S7Server();

s7server.SetResourceless(true);

s7server.on("readWrite", function(sender, operation, tagObj, buffer, callback) {
  console.log((operation === s7server.operationRead ? 'Read' : 'Write') + ' event from ' + sender);
  console.log('Area     : ' + tagObj.Area);
  console.log('DBNumber : ' + tagObj.DBNumber);
  console.log('Start    : ' + tagObj.Start);
  console.log('Size     : ' + tagObj.Size);
  console.log('WordLen  : ' + tagObj.WordLen);

  if (operation === s7server.operationRead) {
    buffer.fill(255);
    return callback(buffer);
  } else {
    console.log('Buffer   : ' + buffer);
    return callback();
  }
});

s7server.StartTo('127.0.0.1');

S7Server.GetEventMask()

Returns the server event filter mask.

S7Server.SetEventMask(mask)

Sets the server event filter mask.

  • mask Bit mask (see table below)

Event code Value
S7Server.evcAll 0xFFFFFFFF
S7Server.evcNone 0x00000000
S7Server.evcServerStarted 0x00000001
S7Server.evcServerStopped 0x00000002
S7Server.evcListenerCannotStart 0x00000004
S7Server.evcClientAdded 0x00000008
S7Server.evcClientRejected 0x00000010
S7Server.evcClientNoRoom 0x00000020
S7Server.evcClientException 0x00000040
S7Server.evcClientDisconnected 0x00000080
S7Server.evcClientTerminated 0x00000100
S7Server.evcClientsDropped 0x00000200
S7Server.evcPDUincoming 0x00010000
S7Server.evcDataRead 0x00020000
S7Server.evcDataWrite 0x00040000
S7Server.evcNegotiatePDU 0x00080000
S7Server.evcReadSZL 0x00100000
S7Server.evcClock 0x00200000
S7Server.evcUpload 0x00400000
S7Server.evcDownload 0x00800000
S7Server.evcDirectory 0x01000000
S7Server.evcSecurity 0x02000000
S7Server.evcControl 0x04000000

API - Miscellaneous functions


S7Server.LastError()

Returns the last job result.

S7Server.EventText(eventObj)

Returns a textual explanation of a given event.

  • eventObj Event object (example here)

S7Server.ErrorText(errNum)

Returns a textual explanation of a given error number.

  • errNum Error number

S7Server.ServerStatus()

Returns the server status. (see table below)

Status Value Description
S7Server.SrvStopped 0x00 The Server is stopped
S7Server.SrvRunning 0x01 The Server is Running
S7Server.SrvError 0x02 Server Error

S7Server.ClientsCount()

Returns the number of clients connected to the server.

S7Server.GetCpuStatus()

Returns the Virtual CPU status. (see table below)

S7Server.SetCpuStatus(cpuStatus)

Sets the Virtual CPU status.

  • cpuStatus Status value (see table below)

Status Value Description
S7Server.S7CpuStatusUnknown 0x00 The CPU status is unknown
S7Server.S7CpuStatusRun 0x08 The CPU is running
S7Server.S7CpuStatusStop 0x04 The CPU is stopped