ts-communication 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +198 -0
- package/dist/XCommunication.d.ts +11 -0
- package/dist/XCommunication.js +27 -0
- package/dist/core/Core.d.ts +95 -0
- package/dist/core/Core.js +267 -0
- package/dist/inovance/InovanceTcpNet.d.ts +23 -0
- package/dist/inovance/InovanceTcpNet.js +93 -0
- package/dist/melsec/MelsecMcNet.d.ts +55 -0
- package/dist/melsec/MelsecMcNet.js +271 -0
- package/dist/modbus/ModbusAscii.d.ts +4 -0
- package/dist/modbus/ModbusAscii.js +8 -0
- package/dist/modbus/ModbusAsciiOverTcp.d.ts +4 -0
- package/dist/modbus/ModbusAsciiOverTcp.js +8 -0
- package/dist/modbus/ModbusRtu.d.ts +15 -0
- package/dist/modbus/ModbusRtu.js +27 -0
- package/dist/modbus/ModbusRtuOverTcp.d.ts +4 -0
- package/dist/modbus/ModbusRtuOverTcp.js +8 -0
- package/dist/modbus/ModbusTcpNet.d.ts +55 -0
- package/dist/modbus/ModbusTcpNet.js +192 -0
- package/dist/modbus/ModbusUdpNet.d.ts +4 -0
- package/dist/modbus/ModbusUdpNet.js +8 -0
- package/dist/omron/OmronFinsNet.d.ts +57 -0
- package/dist/omron/OmronFinsNet.js +95 -0
- package/dist/siemens/SiemensS7Net.d.ts +69 -0
- package/dist/siemens/SiemensS7Net.js +276 -0
- package/package.json +39 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ModbusTcpNet = void 0;
|
|
4
|
+
exports.parseModbusAddress = parseModbusAddress;
|
|
5
|
+
const Core_1 = require("../core/Core");
|
|
6
|
+
function parseModbusAddress(input, defaultStation = 1, defaultCode = 3) {
|
|
7
|
+
try {
|
|
8
|
+
let text = input.trim(), station = defaultStation, code = defaultCode, writeCode, format;
|
|
9
|
+
for (;;) {
|
|
10
|
+
const m = text.match(/^([^;=]+)=([^;]+);(.*)$/);
|
|
11
|
+
if (!m)
|
|
12
|
+
break;
|
|
13
|
+
const key = m[1].toLowerCase(), value = m[2];
|
|
14
|
+
text = m[3];
|
|
15
|
+
if (key === 's')
|
|
16
|
+
station = Number(value);
|
|
17
|
+
else if (key === 'x')
|
|
18
|
+
code = Number(value);
|
|
19
|
+
else if (key === 'w')
|
|
20
|
+
writeCode = Number(value);
|
|
21
|
+
else if (key === 'format')
|
|
22
|
+
format = value.toUpperCase();
|
|
23
|
+
}
|
|
24
|
+
const [addrText, bitText] = text.split('.');
|
|
25
|
+
const address = Number(addrText);
|
|
26
|
+
if (!Number.isInteger(address) || address < 0)
|
|
27
|
+
throw new Error(`Invalid address: ${input}`);
|
|
28
|
+
const bit = bitText === undefined ? undefined : Number(bitText);
|
|
29
|
+
if (bit !== undefined && (bit < 0 || bit > 15))
|
|
30
|
+
throw new Error(`Invalid bit address: ${input}`);
|
|
31
|
+
if (![1, 2, 3, 4, 5, 6, 15, 16, 22].includes(code))
|
|
32
|
+
throw new Error(`Unsupported Modbus function code: ${code}`);
|
|
33
|
+
return Core_1.OperateResult.CreateSuccessResult({ station, code, address, bit, writeCode, format });
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
return new Core_1.OperateResult(e.message);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function crc16(data) { let crc = 0xffff; for (const b of data) {
|
|
40
|
+
crc ^= b;
|
|
41
|
+
for (let i = 0; i < 8; i++)
|
|
42
|
+
crc = (crc & 1) ? (crc >>> 1) ^ 0xa001 : crc >>> 1;
|
|
43
|
+
} return Buffer.from([crc & 0xff, crc >>> 8]); }
|
|
44
|
+
class ModbusTcpNet extends Core_1.DeviceClient {
|
|
45
|
+
Station = 1;
|
|
46
|
+
AddressStartWithZero = true;
|
|
47
|
+
IsStringReverse = false;
|
|
48
|
+
StationCheckMatch = true;
|
|
49
|
+
BroadcastStation = -1;
|
|
50
|
+
WordReadBatchLength = 120;
|
|
51
|
+
DisableFunctionCode06 = false;
|
|
52
|
+
EnableWriteMaskCode = true;
|
|
53
|
+
DataFormat = Core_1.DataFormat.CDAB;
|
|
54
|
+
MessageId = 0;
|
|
55
|
+
constructor(ipAddress, port = 502, station = 1) { super(ipAddress, ipAddress === undefined ? undefined : port); this.Port = ipAddress === undefined ? 5000 : port; this.Station = station; this.ByteTransform = new Core_1.ByteTransform(this.DataFormat); }
|
|
56
|
+
nextId() { const id = this.MessageId; this.MessageId = (this.MessageId + 1) & 0xffff; return id; }
|
|
57
|
+
frame(pdu, station) { const id = this.nextId(); const out = Buffer.alloc(7 + pdu.length); out.writeUInt16BE(id, 0); out.writeUInt16BE(0, 2); out.writeUInt16BE(pdu.length + 1, 4); out[6] = station; pdu.copy(out, 7); return out; }
|
|
58
|
+
async transact(pdu, station) {
|
|
59
|
+
const id = this.MessageId;
|
|
60
|
+
const result = await this.request(this.frame(pdu, station), data => {
|
|
61
|
+
if (data.length < 7)
|
|
62
|
+
throw new Error('incomplete Modbus TCP response');
|
|
63
|
+
const len = data.readUInt16BE(4);
|
|
64
|
+
if (data.length < 6 + len)
|
|
65
|
+
throw new Error('incomplete Modbus TCP response');
|
|
66
|
+
if (data.readUInt16BE(0) !== id)
|
|
67
|
+
throw new Error('Modbus transaction id mismatch');
|
|
68
|
+
return data.subarray(6, 6 + len);
|
|
69
|
+
});
|
|
70
|
+
if (!result.IsSuccess)
|
|
71
|
+
return result;
|
|
72
|
+
const body = result.Content;
|
|
73
|
+
if (body.length < 2)
|
|
74
|
+
return new Core_1.OperateResult('Invalid Modbus response');
|
|
75
|
+
if ((body[1] & 0x80) !== 0)
|
|
76
|
+
return new Core_1.OperateResult(`Modbus exception ${body[2]}`, body[2]);
|
|
77
|
+
return Core_1.OperateResult.CreateSuccessResult(body);
|
|
78
|
+
}
|
|
79
|
+
addr(input, code) { const p = parseModbusAddress(input, this.Station, code); if (p.IsSuccess && !this.AddressStartWithZero)
|
|
80
|
+
p.Content.address--; return p; }
|
|
81
|
+
async Read(address, length) {
|
|
82
|
+
const a = this.addr(address, 3);
|
|
83
|
+
if (!a.IsSuccess)
|
|
84
|
+
return Core_1.OperateResult.CreateFailedResult(a);
|
|
85
|
+
const p = a.Content;
|
|
86
|
+
const fn = p.code === 1 || p.code === 2 || p.code === 4 ? p.code : 3;
|
|
87
|
+
const req = Buffer.alloc(5);
|
|
88
|
+
req[0] = fn;
|
|
89
|
+
req.writeUInt16BE(p.address, 1);
|
|
90
|
+
req.writeUInt16BE(length, 3);
|
|
91
|
+
const result = await this.transact(req, p.station);
|
|
92
|
+
if (!result.IsSuccess)
|
|
93
|
+
return result;
|
|
94
|
+
const body = result.Content;
|
|
95
|
+
if (body.length < 3 || body[1] !== fn)
|
|
96
|
+
return new Core_1.OperateResult('Invalid Modbus read response');
|
|
97
|
+
const byteCount = body[2];
|
|
98
|
+
if (body.length < 3 + byteCount)
|
|
99
|
+
return new Core_1.OperateResult('Invalid Modbus read response');
|
|
100
|
+
return Core_1.OperateResult.CreateSuccessResult(body.subarray(3, 3 + byteCount));
|
|
101
|
+
}
|
|
102
|
+
async ReadBool(address, length) {
|
|
103
|
+
const p = this.addr(address, 1);
|
|
104
|
+
if (!p.IsSuccess)
|
|
105
|
+
return Core_1.OperateResult.CreateFailedResult(p);
|
|
106
|
+
const a = p.Content;
|
|
107
|
+
const count = length ?? 1;
|
|
108
|
+
const req = Buffer.alloc(5);
|
|
109
|
+
req[0] = a.code;
|
|
110
|
+
req.writeUInt16BE(a.address, 1);
|
|
111
|
+
req.writeUInt16BE(count, 3);
|
|
112
|
+
const r = await this.transact(req, a.station);
|
|
113
|
+
if (!r.IsSuccess)
|
|
114
|
+
return Core_1.OperateResult.CreateFailedResult(r);
|
|
115
|
+
const out = [];
|
|
116
|
+
for (let i = 0; i < count; i++)
|
|
117
|
+
out.push((r.Content[3 + (i >> 3)] & (1 << (i & 7))) !== 0);
|
|
118
|
+
return Core_1.OperateResult.CreateSuccessResult(length === undefined ? out[0] : out);
|
|
119
|
+
}
|
|
120
|
+
async ReadCoil(address, length) { return this.ReadBool(address, length); }
|
|
121
|
+
async ReadDiscrete(address, length) { const p = this.addr(address, 2); if (!p.IsSuccess)
|
|
122
|
+
return Core_1.OperateResult.CreateFailedResult(p); return this.ReadBool(address.replace(/^((?:s=\d+;)?)/, '$1x=2;'), length); }
|
|
123
|
+
async readTyped(address, length, width, fn) { const r = await this.Read(address, (0, Core_1.wordsFor)('x', length) * width / 2); if (!r.IsSuccess)
|
|
124
|
+
return Core_1.OperateResult.CreateFailedResult(r); const transform = this.ByteTransform; const values = Array.from({ length }, (_, i) => fn(r.Content, i * width)); return Core_1.OperateResult.CreateSuccessResult(length === 1 ? values[0] : values); }
|
|
125
|
+
async ReadInt16(address, length) { return this.readTyped(address, length ?? 1, 2, (b, o) => this.ByteTransform.TransInt16(b, o)); }
|
|
126
|
+
async ReadUInt16(address, length) { return this.readTyped(address, length ?? 1, 2, (b, o) => this.ByteTransform.TransUInt16(b, o)); }
|
|
127
|
+
async ReadInt32(address, length) { return this.readTyped(address, length ?? 1, 4, (b, o) => this.ByteTransform.TransInt32(b, o)); }
|
|
128
|
+
async ReadUInt32(address, length) { return this.readTyped(address, length ?? 1, 4, (b, o) => this.ByteTransform.TransUInt32(b, o)); }
|
|
129
|
+
async ReadFloat(address, length) { return this.readTyped(address, length ?? 1, 4, (b, o) => this.ByteTransform.TransSingle(b, o)); }
|
|
130
|
+
async ReadDouble(address, length) { return this.readTyped(address, length ?? 1, 8, (b, o) => this.ByteTransform.TransDouble(b, o)); }
|
|
131
|
+
async ReadInt64(address, length) { return this.readTyped(address, length ?? 1, 8, (b, o) => this.ByteTransform.TransInt64(b, o)); }
|
|
132
|
+
async ReadUInt64(address, length) { return this.readTyped(address, length ?? 1, 8, (b, o) => this.ByteTransform.TransUInt64(b, o)); }
|
|
133
|
+
async ReadByte(address) { const r = await this.Read(address, 1); return r.IsSuccess ? Core_1.OperateResult.CreateSuccessResult(r.Content[0]) : Core_1.OperateResult.CreateFailedResult(r); }
|
|
134
|
+
async ReadString(address, length, encoding = 'utf8') { const r = await this.Read(address, length); return r.IsSuccess ? Core_1.OperateResult.CreateSuccessResult(this.ByteTransform.TransString(r.Content, encoding)) : Core_1.OperateResult.CreateFailedResult(r); }
|
|
135
|
+
async Write(address, value) {
|
|
136
|
+
const p = this.addr(address, Array.isArray(value) && typeof value[0] === 'boolean' ? 15 : typeof value === 'boolean' ? 5 : 16);
|
|
137
|
+
if (!p.IsSuccess)
|
|
138
|
+
return p;
|
|
139
|
+
const a = p.Content;
|
|
140
|
+
let fn = p.Content.writeCode ?? p.Content.code;
|
|
141
|
+
let data;
|
|
142
|
+
if (typeof value === 'boolean') {
|
|
143
|
+
const req = Buffer.alloc(5);
|
|
144
|
+
req[0] = fn || 5;
|
|
145
|
+
req.writeUInt16BE(a.address, 1);
|
|
146
|
+
req.writeUInt16BE(value ? 0xff00 : 0, 3);
|
|
147
|
+
const r = await this.transact(req, a.station);
|
|
148
|
+
return r.IsSuccess ? Core_1.OperateResult.CreateSuccessResult() : r;
|
|
149
|
+
}
|
|
150
|
+
if (typeof value === 'string')
|
|
151
|
+
data = Buffer.from(value, 'utf8');
|
|
152
|
+
else if (Buffer.isBuffer(value))
|
|
153
|
+
data = value;
|
|
154
|
+
else if (Array.isArray(value) && typeof value[0] === 'boolean') {
|
|
155
|
+
data = Buffer.alloc(Math.ceil(value.length / 8));
|
|
156
|
+
value.forEach((v, i) => { if (v)
|
|
157
|
+
data[i >> 3] |= 1 << (i & 7); });
|
|
158
|
+
}
|
|
159
|
+
else if (Array.isArray(value))
|
|
160
|
+
data = this.ByteTransform.TransByte(value, 2);
|
|
161
|
+
else
|
|
162
|
+
data = this.ByteTransform.TransByte(value, 2);
|
|
163
|
+
const words = Math.ceil(data.length / 2);
|
|
164
|
+
const useSingle = words === 1 && !this.DisableFunctionCode06 && (fn === 6 || fn === 16);
|
|
165
|
+
const req = useSingle ? Buffer.alloc(5) : Buffer.alloc(6 + data.length);
|
|
166
|
+
req[0] = useSingle ? 6 : (fn || 16);
|
|
167
|
+
req.writeUInt16BE(a.address, 1);
|
|
168
|
+
if (useSingle)
|
|
169
|
+
data.copy(req, 3);
|
|
170
|
+
else {
|
|
171
|
+
req.writeUInt16BE(words, 3);
|
|
172
|
+
req[5] = data.length;
|
|
173
|
+
data.copy(req, 6);
|
|
174
|
+
}
|
|
175
|
+
const r = await this.transact(req, a.station);
|
|
176
|
+
return r.IsSuccess ? Core_1.OperateResult.CreateSuccessResult() : r;
|
|
177
|
+
}
|
|
178
|
+
async WriteMask(address, andMask, orMask) { const p = this.addr(address, 22); if (!p.IsSuccess)
|
|
179
|
+
return p; const req = Buffer.alloc(7); req[0] = 22; req.writeUInt16BE(p.Content.address, 1); req.writeUInt16BE(andMask, 3); req.writeUInt16BE(orMask, 5); const r = await this.transact(req, p.Content.station); return r.IsSuccess ? Core_1.OperateResult.CreateSuccessResult() : r; }
|
|
180
|
+
WriteByte(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Byte(v))); }
|
|
181
|
+
WriteInt16(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Int16(v))); }
|
|
182
|
+
WriteUInt16(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.UInt16(v))); }
|
|
183
|
+
WriteInt32(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Int32(v))); }
|
|
184
|
+
WriteUInt32(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.UInt32(v))); }
|
|
185
|
+
WriteFloat(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Float(v))); }
|
|
186
|
+
WriteDouble(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Double(v))); }
|
|
187
|
+
WriteInt64(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Int64(v))); }
|
|
188
|
+
WriteUInt64(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.UInt64(v))); }
|
|
189
|
+
async ConnectServerAsync() { return this.ConnectServer(); }
|
|
190
|
+
async ConnectCloseAsync() { return this.ConnectClose(); }
|
|
191
|
+
}
|
|
192
|
+
exports.ModbusTcpNet = ModbusTcpNet;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ModbusUdpNet = void 0;
|
|
4
|
+
const ModbusTcpNet_1 = require("./ModbusTcpNet");
|
|
5
|
+
/** Modbus UDP client. */
|
|
6
|
+
class ModbusUdpNet extends ModbusTcpNet_1.ModbusTcpNet {
|
|
7
|
+
}
|
|
8
|
+
exports.ModbusUdpNet = ModbusUdpNet;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { DeviceClient, OperateResult } from '../core/Core';
|
|
2
|
+
export declare enum OmronPlcType {
|
|
3
|
+
CSCJ = 1,
|
|
4
|
+
CV = 2
|
|
5
|
+
}
|
|
6
|
+
export interface OmronAddress {
|
|
7
|
+
BitCode: number;
|
|
8
|
+
WordCode: number;
|
|
9
|
+
AddressStart: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function parseOmronAddress(input: string): OperateResult<OmronAddress>;
|
|
12
|
+
export declare class OmronFinsNet extends DeviceClient {
|
|
13
|
+
ICF: number;
|
|
14
|
+
RSV: number;
|
|
15
|
+
GCT: number;
|
|
16
|
+
DNA: number;
|
|
17
|
+
DA1: number;
|
|
18
|
+
DA2: number;
|
|
19
|
+
SNA: number;
|
|
20
|
+
SA1: number;
|
|
21
|
+
SA2: number;
|
|
22
|
+
SID: number;
|
|
23
|
+
ReadSplits: number;
|
|
24
|
+
PlcType: OmronPlcType;
|
|
25
|
+
ReceiveUntilEmpty: boolean;
|
|
26
|
+
constructor(ipAddress?: string, port?: number);
|
|
27
|
+
private pack;
|
|
28
|
+
private transact;
|
|
29
|
+
private command;
|
|
30
|
+
Read(address: string, length: number): Promise<OperateResult<Buffer<ArrayBufferLike>>>;
|
|
31
|
+
ReadBool(address: string, length?: number): Promise<OperateResult<boolean | boolean[]>>;
|
|
32
|
+
private typedRead;
|
|
33
|
+
ReadInt16(a: string, l?: number): Promise<OperateResult<any>>;
|
|
34
|
+
ReadUInt16(a: string, l?: number): Promise<OperateResult<any>>;
|
|
35
|
+
ReadInt32(a: string, l?: number): Promise<OperateResult<any>>;
|
|
36
|
+
ReadUInt32(a: string, l?: number): Promise<OperateResult<any>>;
|
|
37
|
+
ReadFloat(a: string, l?: number): Promise<OperateResult<any>>;
|
|
38
|
+
ReadDouble(a: string, l?: number): Promise<OperateResult<any>>;
|
|
39
|
+
ReadInt64(a: string, l?: number): Promise<OperateResult<any>>;
|
|
40
|
+
ReadUInt64(a: string, l?: number): Promise<OperateResult<any>>;
|
|
41
|
+
Write(address: string, value: Buffer | number | boolean | string | number[] | boolean[]): Promise<OperateResult<any>>;
|
|
42
|
+
WriteByte(a: string, v: number): Promise<OperateResult<any>>;
|
|
43
|
+
WriteInt16(a: string, v: number): Promise<OperateResult<any>>;
|
|
44
|
+
WriteUInt16(a: string, v: number): Promise<OperateResult<any>>;
|
|
45
|
+
WriteInt32(a: string, v: number): Promise<OperateResult<any>>;
|
|
46
|
+
WriteUInt32(a: string, v: number): Promise<OperateResult<any>>;
|
|
47
|
+
WriteFloat(a: string, v: number): Promise<OperateResult<any>>;
|
|
48
|
+
WriteDouble(a: string, v: number): Promise<OperateResult<any>>;
|
|
49
|
+
WriteInt64(a: string, v: bigint | number): Promise<OperateResult<any>>;
|
|
50
|
+
WriteUInt64(a: string, v: bigint | number): Promise<OperateResult<any>>;
|
|
51
|
+
Run(): Promise<OperateResult<void> | OperateResult<Buffer<ArrayBufferLike>>>;
|
|
52
|
+
Stop(): Promise<OperateResult<void> | OperateResult<Buffer<ArrayBufferLike>>>;
|
|
53
|
+
ConnectServerAsync(): Promise<OperateResult<any>>;
|
|
54
|
+
ConnectCloseAsync(): Promise<OperateResult<any>>;
|
|
55
|
+
}
|
|
56
|
+
export declare class OmronFinsUdp extends OmronFinsNet {
|
|
57
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OmronFinsUdp = exports.OmronFinsNet = exports.OmronPlcType = void 0;
|
|
4
|
+
exports.parseOmronAddress = parseOmronAddress;
|
|
5
|
+
const Core_1 = require("../core/Core");
|
|
6
|
+
var OmronPlcType;
|
|
7
|
+
(function (OmronPlcType) {
|
|
8
|
+
OmronPlcType[OmronPlcType["CSCJ"] = 1] = "CSCJ";
|
|
9
|
+
OmronPlcType[OmronPlcType["CV"] = 2] = "CV";
|
|
10
|
+
})(OmronPlcType || (exports.OmronPlcType = OmronPlcType = {}));
|
|
11
|
+
const area = { D: [0x02, 0x82], DM: [0x02, 0x82], C: [0x30, 0xB0], CIO: [0x30, 0xB0], W: [0x31, 0xB1], WR: [0x31, 0xB1], H: [0x32, 0xB2], HR: [0x32, 0xB2], A: [0x33, 0xB3], AR: [0x33, 0xB3], E: [0x20, 0xA0], EM: [0x20, 0xA0], TIM: [0x09, 0x89], CNT: [0x09, 0x89] };
|
|
12
|
+
function parseOmronAddress(input) { try {
|
|
13
|
+
const m = input.trim().match(/^([A-Za-z]+)([0-9A-Fa-f]+)(?:\.([0-9]+))?$/);
|
|
14
|
+
if (!m)
|
|
15
|
+
throw new Error(`Invalid Omron address: ${input}`);
|
|
16
|
+
const key = m[1].toUpperCase(), a = area[key];
|
|
17
|
+
if (!a)
|
|
18
|
+
throw new Error(`Unsupported Omron address: ${input}`);
|
|
19
|
+
const word = Number.parseInt(m[2], key === 'E' || key === 'EM' ? 16 : 10);
|
|
20
|
+
const bit = Number(m[3] ?? 0);
|
|
21
|
+
if (bit < 0 || bit > 15)
|
|
22
|
+
throw new Error('Invalid bit index');
|
|
23
|
+
return Core_1.OperateResult.CreateSuccessResult({ BitCode: a[0], WordCode: a[1], AddressStart: word * 16 + bit });
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
return new Core_1.OperateResult(e.message);
|
|
27
|
+
} }
|
|
28
|
+
class OmronFinsNet extends Core_1.DeviceClient {
|
|
29
|
+
ICF = 0x80;
|
|
30
|
+
RSV = 0;
|
|
31
|
+
GCT = 2;
|
|
32
|
+
DNA = 0;
|
|
33
|
+
DA1 = 0;
|
|
34
|
+
DA2 = 0;
|
|
35
|
+
SNA = 0;
|
|
36
|
+
SA1 = 1;
|
|
37
|
+
SA2 = 0;
|
|
38
|
+
SID = 0;
|
|
39
|
+
ReadSplits = 500;
|
|
40
|
+
PlcType = OmronPlcType.CSCJ;
|
|
41
|
+
ReceiveUntilEmpty = false;
|
|
42
|
+
constructor(ipAddress, port = 9600) { super(ipAddress, ipAddress === undefined ? undefined : port); this.Port = ipAddress === undefined ? 5000 : port; this.ByteTransform = new Core_1.ByteTransform(Core_1.DataFormat.CDAB); this.ByteTransform.IsStringReverseByteWord = true; }
|
|
43
|
+
pack(core) { const h = Buffer.alloc(26 + core.length); Buffer.from('FINS').copy(h); h.writeUInt32BE(18 + core.length, 4); h[11] = 2; h[16] = this.ICF; h[17] = this.RSV; h[18] = this.GCT; h[19] = this.DNA; h[20] = this.DA1; h[21] = this.DA2; h[22] = this.SNA; h[23] = this.SA1; h[24] = this.SA2; this.SID = (this.SID + 1) & 255; h[25] = this.SID; core.copy(h, 26); return h; }
|
|
44
|
+
async transact(core) { const frame = this.pack(core); const r = await this.request(frame, data => { if (data.length < 30)
|
|
45
|
+
throw new Error('incomplete FINS response'); const n = data.readUInt32BE(4); if (data.length < 8 + n)
|
|
46
|
+
throw new Error('incomplete FINS response'); return data.subarray(0, 8 + n); }); if (!r.IsSuccess)
|
|
47
|
+
return r; const b = r.Content; const status = b.readUInt32BE(12); if (status !== 0)
|
|
48
|
+
return new Core_1.OperateResult(`FINS TCP error ${status}`, status); const end = b.length; return Core_1.OperateResult.CreateSuccessResult(b.subarray(30, end)); }
|
|
49
|
+
command(address, length, isBit = false, write) { const a = parseOmronAddress(address); if (!a.IsSuccess)
|
|
50
|
+
return Core_1.OperateResult.CreateFailedResult(a); const d = a.Content; const c = Buffer.alloc(8 + (write?.length ?? 0)); c[0] = 1; c[1] = write ? 2 : 1; c[2] = isBit ? d.BitCode : d.WordCode; c[3] = (d.AddressStart >> 16) & 255; c[4] = (d.AddressStart >> 8) & 255; c[5] = d.AddressStart & 255; c[6] = (length >> 8) & 255; c[7] = length & 255; if (write)
|
|
51
|
+
write.copy(c, 8); return Core_1.OperateResult.CreateSuccessResult(c); }
|
|
52
|
+
async Read(address, length) { const c = this.command(address, length, false); if (!c.IsSuccess)
|
|
53
|
+
return c; return this.transact(c.Content); }
|
|
54
|
+
async ReadBool(address, length) { const n = length ?? 1, c = this.command(address, n, true); if (!c.IsSuccess)
|
|
55
|
+
return Core_1.OperateResult.CreateFailedResult(c); const r = await this.transact(c.Content); if (!r.IsSuccess)
|
|
56
|
+
return Core_1.OperateResult.CreateFailedResult(r); const vals = Array.from(r.Content, x => x !== 0).slice(0, n); return Core_1.OperateResult.CreateSuccessResult(length === undefined ? vals[0] : vals); }
|
|
57
|
+
async typedRead(address, length, width, fn) { const r = await this.Read(address, length * width / 2); if (!r.IsSuccess)
|
|
58
|
+
return Core_1.OperateResult.CreateFailedResult(r); const vals = Array.from({ length }, (_, i) => fn(r.Content, i * width)); return Core_1.OperateResult.CreateSuccessResult(length === 1 ? vals[0] : vals); }
|
|
59
|
+
async ReadInt16(a, l) { return this.typedRead(a, l ?? 1, 2, (b, o) => this.ByteTransform.TransInt16(b, o)); }
|
|
60
|
+
async ReadUInt16(a, l) { return this.typedRead(a, l ?? 1, 2, (b, o) => this.ByteTransform.TransUInt16(b, o)); }
|
|
61
|
+
async ReadInt32(a, l) { return this.typedRead(a, l ?? 1, 4, (b, o) => this.ByteTransform.TransInt32(b, o)); }
|
|
62
|
+
async ReadUInt32(a, l) { return this.typedRead(a, l ?? 1, 4, (b, o) => this.ByteTransform.TransUInt32(b, o)); }
|
|
63
|
+
async ReadFloat(a, l) { return this.typedRead(a, l ?? 1, 4, (b, o) => this.ByteTransform.TransSingle(b, o)); }
|
|
64
|
+
async ReadDouble(a, l) { return this.typedRead(a, l ?? 1, 8, (b, o) => this.ByteTransform.TransDouble(b, o)); }
|
|
65
|
+
async ReadInt64(a, l) { return this.typedRead(a, l ?? 1, 8, (b, o) => this.ByteTransform.TransInt64(b, o)); }
|
|
66
|
+
async ReadUInt64(a, l) { return this.typedRead(a, l ?? 1, 8, (b, o) => this.ByteTransform.TransUInt64(b, o)); }
|
|
67
|
+
async Write(address, value) { const isBit = typeof value === 'boolean' || (Array.isArray(value) && typeof value[0] === 'boolean'); let data; if (typeof value === 'boolean')
|
|
68
|
+
data = Buffer.from([value ? 1 : 0]);
|
|
69
|
+
else if (typeof value === 'string')
|
|
70
|
+
data = Buffer.from(value);
|
|
71
|
+
else if (Buffer.isBuffer(value))
|
|
72
|
+
data = value;
|
|
73
|
+
else if (Array.isArray(value) && typeof value[0] === 'boolean')
|
|
74
|
+
data = Buffer.from(value.map(x => x ? 1 : 0));
|
|
75
|
+
else
|
|
76
|
+
data = this.ByteTransform.TransByte(value, 2); const c = this.command(address, isBit ? data.length : data.length / 2, isBit, data); if (!c.IsSuccess)
|
|
77
|
+
return c; const r = await this.transact(c.Content); return r.IsSuccess ? Core_1.OperateResult.CreateSuccessResult() : r; }
|
|
78
|
+
WriteByte(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Byte(v))); }
|
|
79
|
+
WriteInt16(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Int16(v))); }
|
|
80
|
+
WriteUInt16(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.UInt16(v))); }
|
|
81
|
+
WriteInt32(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Int32(v))); }
|
|
82
|
+
WriteUInt32(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.UInt32(v))); }
|
|
83
|
+
WriteFloat(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Float(v))); }
|
|
84
|
+
WriteDouble(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Double(v))); }
|
|
85
|
+
WriteInt64(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.Int64(v))); }
|
|
86
|
+
WriteUInt64(a, v) { return this.Write(a, this.ByteTransform.TransByte(new Core_1.UInt64(v))); }
|
|
87
|
+
async Run() { const r = await this.transact(Buffer.from([4, 1, 255, 255, 4])); return r.IsSuccess ? Core_1.OperateResult.CreateSuccessResult() : r; }
|
|
88
|
+
async Stop() { const r = await this.transact(Buffer.from([4, 2, 255, 255])); return r.IsSuccess ? Core_1.OperateResult.CreateSuccessResult() : r; }
|
|
89
|
+
async ConnectServerAsync() { return this.ConnectServer(); }
|
|
90
|
+
async ConnectCloseAsync() { return this.ConnectClose(); }
|
|
91
|
+
}
|
|
92
|
+
exports.OmronFinsNet = OmronFinsNet;
|
|
93
|
+
class OmronFinsUdp extends OmronFinsNet {
|
|
94
|
+
}
|
|
95
|
+
exports.OmronFinsUdp = OmronFinsUdp;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { DeviceClient, OperateResult } from '../core/Core';
|
|
2
|
+
export declare enum SiemensPLCS {
|
|
3
|
+
S1200 = 0,
|
|
4
|
+
S300 = 1,
|
|
5
|
+
S400 = 2,
|
|
6
|
+
S1500 = 3,
|
|
7
|
+
S200Smart = 4,
|
|
8
|
+
S200 = 5
|
|
9
|
+
}
|
|
10
|
+
export interface S7Address {
|
|
11
|
+
area: number;
|
|
12
|
+
db: number;
|
|
13
|
+
byteAddress: number;
|
|
14
|
+
bit: number;
|
|
15
|
+
}
|
|
16
|
+
export declare function parseS7Address(input: string): OperateResult<S7Address>;
|
|
17
|
+
export declare class SiemensS7Net extends DeviceClient {
|
|
18
|
+
Port: number;
|
|
19
|
+
Rack: number;
|
|
20
|
+
Slot: number;
|
|
21
|
+
ConnectionType: number;
|
|
22
|
+
LocalTSAP: number;
|
|
23
|
+
DestTSAP: number;
|
|
24
|
+
PDULength: number;
|
|
25
|
+
PlcType: SiemensPLCS;
|
|
26
|
+
private initialized;
|
|
27
|
+
private initializing?;
|
|
28
|
+
constructor(siemens?: SiemensPLCS, ipAddress?: string, port?: number);
|
|
29
|
+
private static parseTpkt;
|
|
30
|
+
private item;
|
|
31
|
+
private packet;
|
|
32
|
+
private messageId;
|
|
33
|
+
private nextMessageId;
|
|
34
|
+
ConnectServer(): Promise<OperateResult<any>>;
|
|
35
|
+
ConnectClose(): Promise<OperateResult<any>>;
|
|
36
|
+
private connectionSetupPacket;
|
|
37
|
+
private setupCommunicationPacket;
|
|
38
|
+
private transact;
|
|
39
|
+
Read(address: string, length: number): Promise<OperateResult<Buffer<ArrayBufferLike>>>;
|
|
40
|
+
ReadBool(address: string, length?: number): Promise<OperateResult<boolean | boolean[]>>;
|
|
41
|
+
private typedRead;
|
|
42
|
+
ReadInt16(a: string, l?: number): Promise<OperateResult<any>>;
|
|
43
|
+
ReadUInt16(a: string, l?: number): Promise<OperateResult<any>>;
|
|
44
|
+
ReadInt32(a: string, l?: number): Promise<OperateResult<any>>;
|
|
45
|
+
ReadUInt32(a: string, l?: number): Promise<OperateResult<any>>;
|
|
46
|
+
ReadFloat(a: string, l?: number): Promise<OperateResult<any>>;
|
|
47
|
+
ReadDouble(a: string, l?: number): Promise<OperateResult<any>>;
|
|
48
|
+
ReadInt64(a: string, l?: number): Promise<OperateResult<any>>;
|
|
49
|
+
ReadUInt64(a: string, l?: number): Promise<OperateResult<any>>;
|
|
50
|
+
ReadString(address: string, length?: number, encoding?: BufferEncoding): Promise<OperateResult<string>>;
|
|
51
|
+
Write(address: string, value: Buffer | number | boolean | string | number[] | boolean[]): Promise<OperateResult<any>>;
|
|
52
|
+
WriteByte(a: string, v: number): Promise<OperateResult<any>>;
|
|
53
|
+
WriteInt16(a: string, v: number): Promise<OperateResult<any>>;
|
|
54
|
+
WriteUInt16(a: string, v: number): Promise<OperateResult<any>>;
|
|
55
|
+
WriteInt32(a: string, v: number): Promise<OperateResult<any>>;
|
|
56
|
+
WriteUInt32(a: string, v: number): Promise<OperateResult<any>>;
|
|
57
|
+
WriteFloat(a: string, v: number): Promise<OperateResult<any>>;
|
|
58
|
+
WriteDouble(a: string, v: number): Promise<OperateResult<any>>;
|
|
59
|
+
WriteInt64(a: string, v: bigint | number): Promise<OperateResult<any>>;
|
|
60
|
+
WriteUInt64(a: string, v: bigint | number): Promise<OperateResult<any>>;
|
|
61
|
+
ReadOrderNumber(): Promise<OperateResult<string>>;
|
|
62
|
+
HotStart(): Promise<OperateResult<string>>;
|
|
63
|
+
ColdStart(): Promise<OperateResult<string>>;
|
|
64
|
+
Stop(): Promise<OperateResult<string>>;
|
|
65
|
+
ForceBool(): Promise<OperateResult<string>>;
|
|
66
|
+
CancelAllForce(): Promise<OperateResult<string>>;
|
|
67
|
+
ConnectServerAsync(): Promise<OperateResult<any>>;
|
|
68
|
+
ConnectCloseAsync(): Promise<OperateResult<any>>;
|
|
69
|
+
}
|