Siemens node S7 PLC
npm install siemensplcnodes7 library. This project demonstrates how to connect, read, and write data to a Siemens PLC.
bash
npm i nodes7
`
2. Step2 - Install siemens plc
`bash
npm i siemensplc
`
---
Usage
$3
Update the config object with your PLC's connection details:
`javascript
const config = {
port: 102,
host: '192.168.1.151',
rack: 0,
slot: 1,
debug: false
};
`
$3
Create a TagList.js file with the desired tag definitions:
`javascript
// Danh sách tags
const tags = {
tag1: 'MR100',
tag2: 'MR104',
tag3: 'M10.0',
tag4: 'M10.1',
tag5: 'M10.2',
tag6: 'M10.3',
tag7: 'DB1,X0.0',
};
// Xuất module tags để sử dụng trong các file khác
module.exports = tags;
// Examples:
// MR30 - MD30 as REAL
// DB10,LR32 - LREAL at byte offset 32 in DB10, for 1200/1500 only
// DB10,INT6 - DB10.DBW6 as INT
// DB10,I6 -same as above
// DB10,INT6.2 - DB10.DBW6 and DB10.DBW8 in an array with length 2
// DB10,X14.0 - DB10.DBX14.0 as BOOL
// DB10,X14.0.8 - DB10.DBB14 as an array of 8 BOOL
// PIW30 - PIW30 as INT
// DB10,S20.30 - String at offset 20 with length of 30 (actual array length 32 due to format of String type, length byte will be read/written)
// DB10,S20.30.3 - Array of 3 strings at offset 20, each with length of 30 (actual array length 32 due to format of String type, length byte will be read/written)
// DB10,C22.30 - Character array at offset 22 with length of 30 (best to not use this with strings as length byte is ignored)
// DB10,DT0 - Date and time
// DB10,DTZ0 - Date and time in UTC
// DB10,DTL0 - DTL in newer PLCs
// DB10,DTLZ0 - DTL in newer PLCs in UTC
`
$3
The main script initializes the connection, reads tag values, and writes updates as shown:
`javascript
const SiemensPLC = require('SiemensPLC');
const tags = require('./TagList');
const tagscan = 1000; // Scanning interval (1 second)
const plc = new SiemensPLC();
plc.initiateConnection(config, tags, () => {
console.log("PLC connection initialized.");
});
setInterval(() => {
plc.readTags((values) => {
console.log("PLC Values:", values);
});
}, tagscan);
// Example: Write a value after 2 seconds
setTimeout(() => {
plc.writeTag('tag5', true, () => {
console.log("Write operation completed.");
});
}, 2000);
`
---
Configuration
$3
| Key | Description | Default Value |
|------------|-------------------------------------|---------------------|
| port | Port number for the PLC connection | 102 |
| host | IP address of the PLC | '192.168.1.151' |
| rack | Rack number of the PLC | 0 |
| slot | Slot number of the PLC | 1 |
| debug | Enable/Disable debug logs | false |
$3
Update the tagscan variable to change the scanning frequency:
`javascript
const tagscan = 1000; // Time in milliseconds (1 second)
`
---
Examples
$3
PLC values are read periodically based on the tagscan interval:
`javascript
plc.readTags((values) => {
console.log("PLC Values:", values);
});
`
$3
Write a value to a specific tag:
`javascript
plc.writeTag('tag5', true, () => {
console.log("Write operation completed.");
});
`
$3
Write a value to multipe tags:
`javascript
let tagwrite = {
tag1: "true", // bool tag
tag2: 10, // integer or real tag
tag3: "someValue" // String tag
};
writeMultipleTags(tagwrite, () => {
console.log("All values have been written successfully.");
});
`
Full Examples
`javascript
const SiemensPLC = require('SiemensPLC');
const tags = require('./TagList');
// //////////////// CONFIGURATION SETTINGS ////////////////
const tagscan = 1000; // Tag scan interval --- 1s = 1000ms
const config = {port: 102, host: '192.168.1.151', rack: 0, slot: 1, debug: false};
// //////////////// SCAN TIMER ////////////////
function fn_Timer() {
// 1. Read tag values
plc.readTags((values) => {
console.log("PLC Values:", values);
});
}
// //////////////// SYSTEM FUNCTIONS - NO NEED TO MODIFY ////////////////
// Initialize PLC
const plc = new SiemensPLC();
plc.initiateConnection(config, tags, () => {
console.log("PLC connection initialized.");
});
// Read PLC values every 1 second
setInterval(() => {
fn_Timer();
}, tagscan);
// Example of writing a value after 2 seconds
setTimeout(() => {
plc.writeTag('tag5', true, () => {
console.log("Write operation completed.");
});
}, 2000);
``