Skip to content

Commit

Permalink
Minor changes, add 2 more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
msvargas committed Oct 2, 2019
1 parent bd2ed44 commit d308304
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 44 deletions.
5 changes: 3 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
.git
__pycache__
.vscode
examples/*
examples
yarn-error.log
*.py

**/*.ts
!**/*.d.ts
doc
manuals
*.workspace
3 changes: 2 additions & 1 deletion examples/01_write_read_simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/
/**
* more @examples : https://github.com/dmroeder/pylogix/tree/master/pylogix/examples
*/
*/

const PLC = require("../index").default;
PLC.defaultOptions.Micro800 = true;
const comm = new PLC("192.168.100.174");
Expand Down
10 changes: 9 additions & 1 deletion examples/05_arduino_mode_pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ comm
);
})
.catch(console.error);
[0, 1, 2, 3, 4, 5, 6].forEach(p => {
comm
.analogWrite(0, 127)
.then(res => {
console.log(comm.knownTags);
console.log(res);
})
.catch(console.error);
const pins = [0, 1, 2, 3, 4, 5, 6];
pins.forEach(p => {
comm.digitalWrite(p, p % 2).catch(e => console.error("Error write:", e));
});
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.11",
"author": "Michael Vargas",
"scripts": {
"prettier": "prettier --write src/**/*.js examples/*.js",
"prettier": "prettier --write src/**/*.js examples/*.js src/**/*.ts",
"publish:npm": "npm publish --access public",
"tsc": "tsc",
"clean": "tsc --build --clean"
Expand Down
87 changes: 66 additions & 21 deletions src/PLC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export class Pin {
type IPAddress = string;

export interface IOptions {
id?: string;
arduinoMode?: boolean;
autoClose?: boolean;
}

export interface IPingMappings {
Expand All @@ -88,8 +88,23 @@ export interface ICustomSocket {
idTimeout?: NodeJS.Timeout;
}

export interface IDeviceProps extends ILGXDevice {
arduinoMode?: boolean;
export interface IDeviceProps extends ILGXDevice, IOptions {}

export enum CIPTypesValues {
STRUCT = 160,
BOOL = 193,
SINT = 194,
INT = 195,
DINT = 196,
LINT = 197,
USINT = 198,
UINT = 199,
UDINT = 200,
LWORD = 201,
REAL = 202,
LREAL = 203,
DWORD = 211,
STRING = 218
}

declare interface PLC {
Expand Down Expand Up @@ -133,6 +148,7 @@ class PLC extends EIPSocketPool implements IDeviceProps {
serialNumber?: string | number;
productName?: string;
state?: number;
autoClose?: boolean = true;

protected _closing: boolean = false;
protected _pingMapping?: IPingMappings;
Expand All @@ -152,13 +168,7 @@ class PLC extends EIPSocketPool implements IDeviceProps {
/**
* @description get CIPTypes number foreach avaible dataType
*/
public static CIPTypes = Object.keys(CIPTypes)
.map(key => ({ [(CIPTypes as any)[key][1]]: key }))
.reduce(function(result: any, item) {
const key = Object.keys(item)[0];
result[key] = parseInt(item[key as any]);
return result;
}, {});
public static CIPTypes = CIPTypesValues;

public static defaultOptions = {
allowHalfOpen: true,
Expand All @@ -183,17 +193,26 @@ class PLC extends EIPSocketPool implements IDeviceProps {
* @param {IEIPContextOptions} options Options to set if IPAddress typeof string
*/
constructor(
host: IPAddress | IEIPContextOptions,
host: IPAddress | (IEIPContextOptions & IOptions),
options: IEIPContextOptions & IOptions
) {
super(
typeof host === "string"
? Object.assign(PLC.defaultOptions, options, { host })
: Object.assign(PLC.defaultOptions, options)
: Object.assign(PLC.defaultOptions, options, host)
);
try {
if (typeof host === "object" && "autoClose" in host) {
this.autoClose = host.autoClose;
this.arduinoMode = host.arduinoMode;
} else if (typeof options === "object") {
this.autoClose = options.autoClose;
this.arduinoMode = options.arduinoMode;
}
} catch {}
if (typeof host === "string" && !host.length)
throw new Error("Check empty IPAddress");
else if (!isIP(host as string))
else if (typeof host === "string" && !isIP(host))
throw new Error("IP Address no valid, check IP: " + host);

if (this.Micro800 && this.arduinoMode) {
Expand All @@ -208,6 +227,14 @@ class PLC extends EIPSocketPool implements IDeviceProps {
}
});
}
if (!!this.autoClose) {
const onExit = () => {
this.close();
};
process.once("SIGINT", onExit); // Ctrl + C
process.once("SIGKILL", onExit); // kill process
process.once("beforeExit", onExit); // on process.exit
}
}
/**
* @property check if socket is connected
Expand Down Expand Up @@ -391,7 +418,9 @@ class PLC extends EIPSocketPool implements IDeviceProps {
digitalWrite(
pin: number,
value: boolean | number,
options: ITagWriteOptions
options: ITagWriteOptions = {
dataType: CIPTypesValues.BOOL
}
) {
return Bluebird.try(() => {
this._checkPin(pin, value);
Expand All @@ -403,7 +432,7 @@ class PLC extends EIPSocketPool implements IDeviceProps {
).then(tag => {
return tag
? this.write(tag, Number(value), options).then(
value => new Pin(tag, value)
_ => new Pin(tag, value)
)
: undefined;
});
Expand All @@ -413,7 +442,10 @@ class PLC extends EIPSocketPool implements IDeviceProps {
* @description read digital output using pin mapping
* @param {Number} pin
*/
digitalOutRead(pin: number, options: ITagReadOptions) {
digitalOutRead(
pin: number,
options: ITagReadOptions = { dataType: CIPTypesValues.BOOL }
) {
return Bluebird.try(() => {
this._checkPin(pin);
return (
Expand All @@ -433,7 +465,10 @@ class PLC extends EIPSocketPool implements IDeviceProps {
* @description read digital input using pin mapping
* @param {Number} pin
*/
digitalRead(pin: number, options: ITagReadOptions) {
digitalRead(
pin: number,
options: ITagReadOptions = { dataType: CIPTypesValues.BOOL }
) {
return Bluebird.try(() => {
this._checkPin(pin);
return (
Expand All @@ -454,7 +489,11 @@ class PLC extends EIPSocketPool implements IDeviceProps {
* @param {Number} pin
* @param {Number} value
*/
analogWrite(pin: number, value: number, options: ITagWriteOptions) {
analogWrite(
pin: number,
value: number,
options: ITagWriteOptions = { dataType: CIPTypesValues.UINT }
) {
return Bluebird.try(() => {
this._checkPin(pin, value);
return (
Expand All @@ -466,7 +505,7 @@ class PLC extends EIPSocketPool implements IDeviceProps {
return !tag
? undefined
: this.write(tag, Number(value), options).then(
value => new Pin(tag, value)
_ => new Pin(tag, value)
);
})
);
Expand All @@ -476,7 +515,10 @@ class PLC extends EIPSocketPool implements IDeviceProps {
* @description read analog output pin using pin mapping
* @param {Number} pin
*/
analogOutRead(pin: number, options: ITagReadOptions) {
analogOutRead(
pin: number,
options: ITagReadOptions = { dataType: CIPTypesValues.UINT }
) {
return Bluebird.try(() => {
this._checkPin(pin);
return (
Expand All @@ -496,7 +538,10 @@ class PLC extends EIPSocketPool implements IDeviceProps {
* @description read analog input pin using pin mapping
* @param {Number} pin
*/
analogRead(pin: number, options: ITagReadOptions) {
analogRead(
pin: number,
options: ITagReadOptions = { dataType: CIPTypesValues.UINT }
) {
return Bluebird.try(() => {
this._checkPin(pin);
return (
Expand Down
12 changes: 6 additions & 6 deletions src/eip-socket.js

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions src/eip-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ export default class EIPSocket extends Socket {
private async _getUDT() {
if (!this.context.tagList) return;
//get only tags that are a struct
const struct_tags = this.context.tagList.filter(x => x.struct === 1);
const struct_tags = this.context.tagList.filter(x => x.struct);
// reduce our struct tag list to only unique instances
const seen = new Set();
const unique = struct_tags.filter(obj => {
Expand Down Expand Up @@ -1802,15 +1802,12 @@ function parseLgxTag(packet: Buffer, programName?: string) {
if (programName) t.tagName = String(programName + "." + name);
else t.tagName = String(name);
t.instanceId = unpackFrom("<H", packet, true, 0)[0] as number;

const val = unpackFrom("<H", packet, true, length + 6)[0] as number;

t.symbolType = val & 0xff;
t.dataTypeValue = val & 0xfff;
t.array = (val & 0x6000) >> 13;
t.struct = (val & 0x8000) >> 15;

if (t.array) t.size = unpackFrom("<H", packet, true, length + 8)[0] as number;
t.array = Boolean((val & 0x6000) >> 13);
t.struct = Boolean((val & 0x8000) >> 15);
if (t.array) t.size = <number>unpackFrom("<H", packet, true, length + 8)[0];
else t.size = 0;
return t;
}
Expand All @@ -1821,8 +1818,8 @@ export class LgxTag {
symbolType: number = 0x00;
dataTypeValue: number = 0x00;
dataType: string = "";
array: number = 0x00;
struct: number = 0x00;
array: boolean = false;
struct: boolean = false;
size: number = 0x00;
constructor() {}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lgxDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { RemoteInfo } from "dgram";
import devices from "../resources/devices.json";
import vendors from "../resources/vendors.json";

export interface ILGXDevice{
/**
export interface ILGXDevice {
/**
* @description Vendor Identification
*/
vendorId?: number | string;
Expand Down Expand Up @@ -60,7 +60,7 @@ export default class LGXDevice implements ILGXDevice {
serialNumber?: string | number;
productName?: string;
state?: number;

constructor(rinfo?: RemoteInfo) {
if (!!rinfo) this.address = () => rinfo;
}
Expand Down

0 comments on commit d308304

Please sign in to comment.