Node-RED 讀取與寫入西門子 S7 PLC 全攻略:node-red-contrib-s7 使用詳解
一、概述
node-red-contrib-s7用于與 Siemens S7 PLC 交互的 Node-RED 節(jié)點(diǎn)。S7讀數(shù)據(jù) S7寫數(shù)據(jù) S7高級(jí)讀取支持以S7協(xié)議讀寫西門子系列PLC的數(shù)據(jù),目前已經(jīng)測試過西門子PLC如下:
S7-200 Smart
注:僅支持網(wǎng)口通信。
二、安裝方法
Node-RED 界面中的 “Manage Palette” 菜單安裝此節(jié)點(diǎn)。
如果是開源的系統(tǒng),可以在 npm install node-red-contrib-s7
注:需要 NodeJS 版本 10 或更高版本以及 Node-RED 版本 1.0 或更高版本。
三、節(jié)點(diǎn)介紹
3.1 s7 in讀數(shù)據(jù)節(jié)點(diǎn)
從S7協(xié)議的PLC內(nèi)部讀取數(shù)據(jù)

l輸出參數(shù)
payload | 從PLC讀取到的值,輸出格式取決于配置的模式 |
topic | 當(dāng)輸出為單個(gè)變量時(shí),為消息的名稱 |
l模式詳解
從PLC中按照配置好的規(guī)則循環(huán)讀取所有數(shù)據(jù),有三種模式:
單個(gè)變量 (Single variable) | 可以從配置的變量中選擇單個(gè)變量,每個(gè)周期都會(huì)輸出一條消息。或者當(dāng)采集的值變化時(shí)在輸出。msg.payload是變量的值msg.topic是變量的名稱 |
所有變量,每條消息一個(gè) (All variables, one per message) | 每個(gè)變量輸出一條和單個(gè)變量模式類似,每個(gè)變量都會(huì)輸出一條消息 |
所有變量 (All variables) | 輸出一條所有消息通過一條消息輸出。msg.payload是一個(gè)包含了所有變量名稱和值的結(jié)構(gòu)體 |
3.2 s7 out 寫數(shù)據(jù)節(jié)點(diǎn)
向S7協(xié)議的PLC內(nèi)部寫入數(shù)據(jù)。注:向生產(chǎn)環(huán)境的PLC寫入數(shù)據(jù)時(shí)要慎重!

l輸入?yún)?shù)
payload | 要寫入的數(shù)據(jù),將其轉(zhuǎn)換為所需的數(shù)據(jù)類型 |
variable | PLC要寫入的變量,根據(jù)配置的變量表選擇。只有當(dāng)變量配置為空時(shí),此字段才有效 |
向節(jié)點(diǎn)配置的變量或消息指定的變量中寫入數(shù)據(jù),可以通過將msg.variable和msg.payload設(shè)置為數(shù)組實(shí)現(xiàn)一次寫入多個(gè)變量
3.3 s7 control 高級(jí)讀取節(jié)點(diǎn)
高級(jí)控制功能,此節(jié)點(diǎn)的功能取決于選擇的模式。
Cycle Time(循環(huán)) | 循環(huán)讀取變量,時(shí)間間隔可以更改。如果選擇此模式,payload必須是一個(gè)正數(shù),即可以通過傳入消息的payload字段來設(shè)置讀取間隔。如果是0則禁用循環(huán)讀取 |
Trigger read(觸發(fā)) | 手動(dòng)觸發(fā)讀取 |
四、使用方法
4.1配置PLC節(jié)點(diǎn)
配置connection屬性

S7讀數(shù)據(jù)S7寫數(shù)據(jù)S7高級(jí)讀取共用一個(gè)S7配置節(jié)點(diǎn),主要功能也集中在S7配置節(jié)點(diǎn)內(nèi),配置內(nèi)容如下:
接口(Transport):目前只支持以太網(wǎng),也就是S7協(xié)議
Address(IP):PLC的IP地址
Port(端口):S7通信端口,默認(rèn)102
Mode(模式):Rack/Slot(機(jī)架號(hào)/槽號(hào),通常選擇此模式,TIA Portal軟件查看地址見下圖)
TASP(S7-200 Smart選擇TASP,10.02 03.01)
Cycle time(采集周期):配置從PLC讀取變量的時(shí)間間隔,如果設(shè)置0則禁用自動(dòng)讀取
Timeout(超時(shí)時(shí)間):配置從PLC讀取變量的超時(shí)時(shí)間,如果網(wǎng)絡(luò)繁忙或者延遲很高,可能需要加大此 時(shí)間。
TIA Portal(博途)軟件架號(hào)槽號(hào)查看方法:

l配置Variables

變量地址即數(shù)據(jù)點(diǎn)地址,表示方式和Step7以及博圖軟件有一些不同,具體配置請(qǐng)參考下表進(jìn)行設(shè)置:
變量地址 | Step7 地址 | 數(shù)據(jù)類型 | 描述 |
DB5,X0.1 | DB5.DBX0.1 | Boolean | Bit 1 of byte 0 of DB 5 |
DB23,B1 or DB23,BYTE1 | DB23.DBB1 | Number | Byte 1 (0-255) of DB 23 |
DB100,C2 or DB100,CHAR2 | DB100.DBB2 | String | Byte 2 of DB 100 as a Char |
DB42,I3 or DB42,INT3 | DB42.DBW3 | Number | Signed 16-bit number at byte 3 of DB 42 |
DB57,WORD4 | DB57.DBW4 | Number | Unsigned 16-bit number at byte 4 of DB 57 |
DB13,DI5 or DB13,DINT5 | DB13.DBD5 | Number | Signed 32-bit number at byte 5 of DB 13 |
DB19,DW6 or DB19,DWORD6 | DB19.DBD6 | Number | Unsigned 32-bit number at byte 6 of DB 19 |
DB21,R7 or DB21,REAL7 | DB19.DBD7 | Number | Floating point 32-bit number at byte 7 of DB 21 |
DB2,S7.10* | - | String | String of length 10 starting at byte 7 of DB 2 |
I1.0 or E1.0 | I1.0 or E1.0 | Boolean | Bit 0 of byte 1 of input area |
Q2.1 or A2.1 | Q2.1 or A2.1 | Boolean | Bit 1 of byte 2 of output area |
M3.2 | QM3.2 | Boolean | Bit 2 of byte 3 of memory area |
IB4 or EB4 | IB4 or EB4 | Number | Byte 4 (0 -255) of input area |
QB5 or AB5 | QB5 or AB5 | Number | Byte 5 (0 -255) of output area |
MB6 | MB6 | Number | Byte 6 (0 -255) of memory area |
IC7 or EC7 | IB7 or EB7 | String | Byte 7 of input area as a Char |
QC8 or AC8 | QB8 or AB8 | String | Byte 8 of output area as a Char |
MC9 | MB9 | String | Byte 9 of memory area as a Char |
II10 or EI10 | IW10 or EW10 | Number | Signed 16-bit number at byte 10 of input area |
QI12 or AI12 | QW12 or AW12 | Number | Signed 16-bit number at byte 12 of output area |
MI14 | MW14 | Number | Signed 16-bit number at byte 14 of memory area |
IW16 or EW16 | IW16 or EW16 | Number | Unsigned 16-bit number at byte 16 of input area |
QW18 or AW18 | QW18 or AW18 | Number | Unsigned 16-bit number at byte 18 of output area |
MW20 | MW20 | Number | Unsigned 16-bit number at byte 20 of memory area |
IDI22 or EDI22 | ID22 or ED22 | Number | Signed 32-bit number at byte 22 of input area |
QDI24 or ADI24 | QD24 or AD24 | Number | Signed 32-bit number at byte 24 of output area |
MDI26 | MD26 | Number | Signed 32-bit number at byte 26 of memory area |
ID28 or ED28 | ID28 or ED28 | Number | Unsigned 32-bit number at byte 28 of input area |
QD30 or AD30 | QD30 or AD30 | Number | Unsigned 32-bit number at byte 30 of output area |
MD32 | MD32 | Number | Unsigned 32-bit number at byte 32 of memory area |
IR34 or ER34 | IR34 or ER34 | Number | Floating point 32-bit number at byte 34 of input area |
QR36 or AR36 | QR36 or AR36 | Number | Floating point 32-bit number at byte 36 of output area |
MR38 | MR38 | Number | Floating point 32-bit number at byte 38 of memory area |
DB1,DT0 | - | Date** | A timestamp in the DATE_AND_TIME format |
DB1,DTZ10 | - | Date** | A timestamp in the DATE_AND_TIME format, in UTC |
DB2,DTL2 | - | Date** | A timestamp in the DTL format |
DB1,DTLZ12 | - | Date** | A timestamp in the DTL format, in UTC |
DB57,RWORD4 | DB57.DBW4 | Number | Unsigned 16-bit number at byte 4 of DB 57, interpreted as Little-Endian |
DB13,RDI5 or DB13,RDINT5 | DB13.DBD5 | Number | Signed 32-bit number at byte 5 of DB 13, interpreted as Little-Endian |
MRW20 | MRW20 | Number | Unsigned 16-bit number at byte 20 of memory area, interpreted as Little-Endian |
注:在使用S7-1200/1500讀取數(shù)據(jù)時(shí)需要進(jìn)行以下配置:
l要訪問的數(shù)據(jù)塊必須禁用:“Optimized block access”
l在 CPU 屬性的“保護(hù)”部分中,啟用 Permit access with PUT/GET
