siemensplc
v1.1.3
Published
Siemens node S7 PLC
Readme
SiemensPLC Integration
A Node.js project for interacting with Siemens PLCs using the nodes7 library. This project demonstrates how to connect, read, and write data to a Siemens PLC.
Table of Contents
Features
- Connect to Siemens PLC: Supports configuration of host, port, rack, and slot.
- Read Tags: Retrieve tag values from the PLC in real-time.
- Write Tags: Update tag values programmatically.
- Timer-Based Scanning: Automatically scan and log tag values at a specified interval.
Installation
- Step1 - Install node S7:
npm i nodes7 - Step2 - Install siemens plc
npm i siemensplc
Usage
1. Set up your PLC configuration:
Update the config object with your PLC's connection details:
const config = {
port: 102,
host: '192.168.1.151',
rack: 0,
slot: 1,
debug: false
};2. Define your tags:
Create a TagList.js file with the desired tag definitions:
// 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 UTC3. Connect and run the PLC:
The main script initializes the connection, reads tag values, and writes updates as shown:
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
PLC Connection
| 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 |
Scanning Interval
Update the tagscan variable to change the scanning frequency:
const tagscan = 1000; // Time in milliseconds (1 second)Examples
Reading PLC Values
PLC values are read periodically based on the tagscan interval:
plc.readTags((values) => {
console.log("PLC Values:", values);
});Writing a PLC Tag
Write a value to a specific tag:
plc.writeTag('tag5', true, () => {
console.log("Write operation completed.");
});Writing a Multipe tag
Write a value to multipe tags:
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
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);
License
This project is licensed under the MIT License. See the LICENSE file for details.
