项目作者: flosse

项目描述 :
Node.js模块连接到PLC
高级语言: C
项目地址: git://github.com/flosse/node-plc.git
创建时间: 2013-10-14T11:01:52Z
项目社区:https://github.com/flosse/node-plc

开源协议:

下载


node-plc

Node.js module to communicate with PLCs.
At the moment only the Siemens LOGO! PLC (0BA7and 0BA8) is supported.

Build Status
Dependency Status
NPM version

Usage

  1. npm install --save plc
  1. var plc = require("plc");

Logo class

  1. var myLogo = new plc.Logo("192.168.0.1", {
  2. markers: 6, // default is 8
  3. inputs: 4, // default is 8
  4. timeout: 500 // socket timeout in milliseconds
  5. });
  6. myLogo.on("error", function(err){
  7. console.error(err.message);
  8. });
  9. myLogo.on("connect", function(){
  10. var result = myLogo.getInputs();
  11. if (result instanceof Error){
  12. return console.error(result.message);
  13. }
  14. console.log(result); // [true, false, false, true, false, true]
  15. result = myLogo.getMarkers();
  16. if (result instanceof Error){
  17. return console.error(result.message);
  18. }
  19. console.log(result); // [true, false, true, true]
  20. result = myLogo.setMarker(2);
  21. if (result instanceof Error){
  22. return console.error(result.message);
  23. }
  24. myLogo.disconnect();
  25. });
  26. myLogo.connect();

Simulation

  1. var plc = require("plc");
  2. var myVirtualLogo = new plc.Logo("192.168.0.1", { simulate: true });
  3. myLogo.on("connect", function(){
  4. /**
  5. * Since we cannot manipulate the inputs of a real PLCs
  6. * there is no "setInput" method. But within the simulation
  7. * mode we can use the special methods "setSimulatedInput"
  8. * and "clearSimulatedInput".
  9. */
  10. myVirtualLogo.setSimulatedInput(2);
  11. myLogo.getInput(2); // true
  12. myVirtualLogo.clearSimulatedInput(2);
  13. myLogo.getInput(2); // false
  14. /**
  15. * Markers can be used as usual.
  16. */
  17. myVirtualLogo.setMarker(1);
  18. myVirtualLogo.getMarker(1); // true
  19. myVirtualLogo.clearMarker(1);
  20. myVirtualLogo.getMarker(1); // false
  21. });
  22. myVirtualLogo.connect();

Comfort features

The LOGO! can be configured with state and action schemata.
A states could be described like this:

  1. var myStates = {
  2. stateOne: { input: 0 },
  3. stateTwo: { marker: 2 },
  4. stateThree: { input: 2 }
  5. };

An action consists of an array with defined desired states:

  1. var actions = {
  2. actionOne:
  3. [
  4. { type: 'clear', marker: 1 },
  5. { type: 'set', marker: 3 }
  6. ],
  7. actionTwo:
  8. [ { type: 'set', marker: 2 } ],
  9. actionThree:
  10. [ { type: 'alias', actions: ['actionOne','actionTwo'] } ]
  11. };

This is a full example:

  1. var config = {
  2. timeout: 500 // connection timeout
  3. interval: 250 // read state interval
  4. states: {
  5. x: { input: 0 },
  6. y: { input: 2 },
  7. foo: { marker: 0 },
  8. bar: { input: 1 }
  9. actions: {
  10. switchOne:
  11. [
  12. { type: 'set', marker: 3 }
  13. ],
  14. switchTwo:
  15. [
  16. { type: 'set', marker: 1 },
  17. { type: 'alias', switches: ['switchOne'] }
  18. ]
  19. }
  20. }
  21. };
  22. var dev1 = new Device("192.168.0.201", config);
  23. dev1.on("connect", function(){
  24. console.log("Device 1 connected");
  25. });
  26. dev1.on("timeout", function(){
  27. console.log("Device 1 connection timeout occoured");
  28. });
  29. dev1.on("disconnect", function(){
  30. console.log("Device 1 disconnected");
  31. });
  32. dev1.on("error", function(err){
  33. console.error("something went wrong: ", err.message);
  34. });
  35. dev.on('state-change', function(state){
  36. console.log(state);
  37. // { x: true, y: false, foo: true, bar: false }
  38. });
  39. dev1.connect();
  40. dev1.startWatching();
  41. // ...
  42. dev1.stopWatching();
  43. dev1.disconnect();

API

Constructor

  1. new require("plc").Logo(ipAddress, options);

Following options are available

  • inputs - number of used inputs
  • markers - number of used markers
  • simulate - simulation mode
  • timeout - the socket timeout

Properties

  • ip
  • isConnected

Methods

  • connect()
  • disconnect()
  • setMarker(nr)
  • clearMarker(nr)
  • getMarker(nr)
  • getMarkers()
  • getInput(nr)
  • getInputs()
  • setSimulatedInput(nr)
  • clearSimulatedInput(nr)
  • getState()
  • setSimulatedState(stateName, value)
  • setVirtualState(stateName, value)
  • triggerAction(action)
  • startWatching
  • stopWatching

Events

  • error
  • connect
  • disconnect
  • timeout
  • state
  • state-change

Test

  1. npm test

License

This project is licensed under the LGPL license.