Skip to content

Commit 703db54

Browse files
authored
Merge pull request #3 from Sinclair81/main
v1.2.3 Added LOGO! examples. Bug fix in snap7_client.cpp
2 parents bfbc46e + 631045c commit 703db54

9 files changed

+153
-39
lines changed

examples/async.js

-14
This file was deleted.

examples/async_LOGO.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const snap7 = require("napi-snap7");
2+
3+
const client = new snap7.S7Client();
4+
5+
const time = (ms) => console.log("| Execution time : %d ms", ms);
6+
7+
const res_p = client.SetConnectionParams("192.168.57.12", 0x2000, 0x2100);
8+
9+
const res_c = client.Connect();
10+
11+
// Read out the outputs Q1 - Q8 of a LOGO 0BA8
12+
// DB number (always 0 for LOGO!s)
13+
// | Offset to start
14+
// | | Size to read (bytes)
15+
// | | |
16+
client.DBRead(0, 1064, 1, (err, data) => {
17+
if (err) return console.log(client.ErrorText(err));
18+
console.log("Data", data);
19+
time(client.ExecTime());
20+
client.Disconnect();
21+
});
22+
23+
/* OR */
24+
25+
// Write a 1 in the bit of the VB address V300.5
26+
const buffer = Buffer.from([0b00100000]);
27+
// DB number (always 0 for LOGO!s)
28+
// | Offset to start
29+
// | | Size to write (bytes)
30+
// | | | User buffer
31+
// | | | |
32+
client.DBWrite(0, 300, 1, buffer, (err, data) => {
33+
if (err) return console.log(client.ErrorText(err));
34+
console.log("Data", data);
35+
time(client.ExecTime());
36+
client.Disconnect();
37+
});

examples/async_S7.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const snap7 = require("napi-snap7");
2+
3+
const client = new snap7.S7Client();
4+
5+
const time = (ms) => console.log("| Execution time : %d ms", ms);
6+
7+
const res = client.ConnectTo("192.168.57.12", 0, 1);
8+
9+
// Reading 100 bytes from DB 510 starts at 0
10+
// Area identifier (0x84 - S7AreaDB)
11+
// | DB number if area = S7AreaDB, otherwise ignored
12+
// | | Offset to start
13+
// | | | Amount of words to read
14+
// | | | | Word size (0x02 - Byte (8 bit))
15+
// | | | | |
16+
client.ReadArea(0x84, 510, 0, 100, 0x02, (err, data) => {
17+
if (err) return console.log(client.ErrorText(err));
18+
console.log("Data", data);
19+
time(client.ExecTime());
20+
client.Disconnect();
21+
});
22+
23+
/* OR */
24+
25+
// Write 42 to address 0 of DB 510
26+
const buffer = Buffer.from([42]);
27+
// Area identifier (0x84 - S7AreaDB)
28+
// | DB number if area = S7AreaDB, otherwise ignored
29+
// | | Offset to start
30+
// | | | Amount of words to write
31+
// | | | | Word size (0x02 - Byte (8 bit))
32+
// | | | | | User buffer
33+
// | | | | | |
34+
client.WriteArea(0x84, 510, 0, 1, 0x02, buffer, (err, data) => {
35+
if (err) return console.log(client.ErrorText(err));
36+
console.log("Data", data);
37+
time(client.ExecTime());
38+
client.Disconnect();
39+
});

examples/sync.js

-16
This file was deleted.

examples/sync_LOGO.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const snap7 = require('napi-snap7')
2+
3+
const client = new snap7.S7Client()
4+
5+
const time = (ms) => console.log('| Execution time : %d ms', ms)
6+
7+
const res_p = client.SetConnectionParams("192.168.57.12", 0x2000, 0x2100);
8+
9+
const res_c = client.Connect();
10+
console.log('ConnectTo', res_c)
11+
time(client.ExecTime())
12+
console.log('Connected', client.GetConnected())
13+
14+
// Read out the outputs Q1 - Q8 of a LOGO 0BA8
15+
// DB number (always 0 for LOGO!s)
16+
// | Offset to start
17+
// | | Size to read (bytes)
18+
// | | |
19+
const data_r = client.DBReadSync(0, 1064, 1)
20+
console.log('Data', data_r)
21+
time(client.ExecTime())
22+
23+
// Write a 1 in the bit of the VB address V300.5
24+
const buffer = Buffer.from([0b00100000]);
25+
// DB number (always 0 for LOGO!s)
26+
// | Offset to start
27+
// | | Size to write (bytes)
28+
// | | | User buffer
29+
// | | | |
30+
const data_w = client.DBWriteSync(0, 300, 1, buffer)
31+
32+
client.Disconnect()

examples/sync_S7.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const snap7 = require('napi-snap7')
2+
3+
const client = new snap7.S7Client()
4+
5+
const time = (ms) => console.log('| Execution time : %d ms', ms)
6+
7+
const res = client.ConnectTo('192.168.20.55', 0, 1)
8+
console.log('ConnectTo', res)
9+
time(client.ExecTime())
10+
console.log('Connected', client.GetConnected())
11+
12+
// Reading 20 words from DB 510 starts at 0
13+
// Area identifier (0x84 - S7AreaDB)
14+
// | DB number if area = S7AreaDB, otherwise ignored
15+
// | | Offset to start
16+
// | | | Amount of words to read
17+
// | | | | Word size (0x04 - Word (16 bit))
18+
// | | | | |
19+
const data_r = client.ReadAreaSync(0x84, 510, 0, 20, 0x04)
20+
console.log('Data', data_r)
21+
time(client.ExecTime())
22+
23+
// Write 42 to address 0 of DB 510
24+
const buffer = Buffer.from([42]);
25+
// Area identifier (0x84 - S7AreaDB)
26+
// | DB number if area = S7AreaDB, otherwise ignored
27+
// | | Offset to start
28+
// | | | Amount of words to write
29+
// | | | | Word size (0x02 - Byte (8 bit))
30+
// | | | | | User buffer
31+
// | | | | | |
32+
const data_w = client.WriteAreaSync(0x84, 510, 0, 1, 0x02, buffer)
33+
console.log('Data', data_w)
34+
time(client.ExecTime())
35+
36+
client.Disconnect()

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "napi-snap7",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "Node-API addon for Snap7 library",
55
"keywords": [
66
"Snap7",

src/snap7_client.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ Napi::Object S7Client::Init(Napi::Env env, Napi::Object exports)
1515
InstanceMethod("ReadArea", &S7Client::ReadArea),
1616
InstanceMethod("WriteAreaSync", &S7Client::WriteAreaSync),
1717
InstanceMethod("WriteArea", &S7Client::WriteArea),
18-
InstanceMethod("DBReadSync", &S7Client::ReadAreaSync),
19-
InstanceMethod("DBRead", &S7Client::ReadArea),
20-
InstanceMethod("DBWriteSync", &S7Client::WriteAreaSync),
21-
InstanceMethod("DBWrite", &S7Client::WriteArea),
18+
InstanceMethod("DBReadSync", &S7Client::DBReadSync),
19+
InstanceMethod("DBRead", &S7Client::DBRead),
20+
InstanceMethod("DBWriteSync", &S7Client::DBWriteSync),
21+
InstanceMethod("DBWrite", &S7Client::DBWrite),
2222
InstanceMethod("GetConnected", &S7Client::GetConnected),
2323
InstanceMethod("GetLastError", &S7Client::GetLastError),
2424
InstanceMethod("ErrorText", &S7Client::ErrorText),
@@ -222,7 +222,7 @@ Napi::Value S7Client::WriteArea(const Napi::CallbackInfo &info)
222222

223223
Napi::Uint8Array arr = info[5].As<Napi::Uint8Array>();
224224
// size_t length = arr.ElementLength(); // == size
225-
uint8_t *Buffer = reinterpret_cast<uint8_t *>(arr.ArrayBuffer().Data());
225+
uint8_t *Buffer = reinterpret_cast<uint8_t *>(arr.Data());
226226

227227
if (info[6].IsFunction())
228228
{
@@ -324,7 +324,7 @@ Napi::Value S7Client::DBWrite(const Napi::CallbackInfo &info)
324324

325325
Napi::Uint8Array arr = info[3].As<Napi::Uint8Array>();
326326
// size_t length = arr.ElementLength(); // == size
327-
uint8_t *Buffer = reinterpret_cast<uint8_t *>(arr.ArrayBuffer().Data());
327+
uint8_t *Buffer = reinterpret_cast<uint8_t *>(arr.Data());
328328

329329
if (info[4].IsFunction())
330330
{

0 commit comments

Comments
 (0)