Giter VIP home page Giter VIP logo

netvar's Introduction

Changes to original package by alexander98

  • openList supports an additional boolean packed. Needed if the PLC has pack variables activated. In this mode, a set(), setMore() or cyclic send will always send all variables in one packed message, instead of sending each variable in an individual UDP packet. Also receive detects if message is packed and handles it correctly
  • send port not hardcoded to 1202, can be defined as an optional parameter in client().
  • debug: log sent and received messages
  • UDP message parts are encoded properly so listId can be larger than 0xF and the message counters increment correctly

CoDeSys NetworkVariableLists

To exchange global variables with a CoDeSys-PLC is very easy. Just create a NetworkVariableList (NVL (Sender)) and add your variables to the list. With this package you can receive this UDP-packages and parse them.

To send data to a CoDeSys-PLC, you define a new list with this node-module and use the generated definition to define the network variable list in your PLC as NVL (receiver).

How to do

The package is type-save and will hint you on your inputs. You can use it in Typescript and JavaScript. Please be careful with the types when you use it with JavaScript.

Create a new client

Create a new client with the netvar package

import { client } from 'netvar'

const netVar = client()
const netVar1 = client('192.168.0.123') //defaults to port 1202
const netVar2 = client('192.168.0.255', {port: 1202}) //uses the same port for send and receive
const netVar3 = client('192.168.0.123', {port: 1202, send_port: 1302}) //receive from port 1202, send to port 1302
const netVar4 = client('192.168.0.123', {debug: true}) //enable debug messages

Define a new or connect to existing list

Define a new variable list or enter the definition of an existing network variable list. The names do not matter for the communication and can be adjusted to your naming conventions.

Note: If you add a onChange callback you get all updates on this list live from the network.

const list1 = netVar.openList(
  { listId: 1 },
  {
    emergency: t.boolean(0),
    working: t.word(1),
    counter: t.dWord(2, 4242),
  },
)

// more lists
const list2 = netVar.openList({
  {
    listId: 2,
    onChange: (name, value) => console.log(name, value),
    cyclic: true,
    cycleInterval: 2000,
    packed: true, //pack all variables on send (when set() or setMore()). Some PLCs only support this mode
  },
  {
    Active: t.boolean(0),
    Next_Task: t.string(1, 'nut'),
    Speed: t.real(2, Math.PI),
  },
)

Get value

Read the value of a property from the list.

This value will be your initial values until you change it or you get different information over the network.

const value = list1.get('working')
console.log(value)

Set value

Set a one new value to a specific property from the list or set a number of new values.

The new values are send to the other peers (PLC).

list1.set('working', false)
list1.setMore({
  working: false,
  counter: 42,
})

Get Definition

Build the Definition to set the network variable list configuration on a PLC (receiver)

import fs from 'fs'
fs.writeFileSync('definiting.gvl', list1.definition)

Available data types

Type Node PLC
t.boolean boolean BOOLEAN
t.word number WORD
t.string string STRING
t.wString string WSTRING
t.byte number BYTE
t.dWord number DWORD
t.time number TIME
t.real number REAL
t.lReal number LREAL

Example:

import { t } from 'netvar'
const vars = {
  test: t.real(1, Math.PI),
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.