stream
1.0.0
这是一个 NodeJS 模块,可帮助您处理 modbus 数据。它使用 pdu 构建核心 PDU,然后使用传输来扩展其余部分。
这是我当前的test.js
文件。它创建一个客户端和一个服务器网络套接字,一旦客户端连接,服务器就会请求线圈。
var modbus = require ( "modbus-stream" ) ;
modbus . tcp . server ( { debug : "server" } , ( connection ) => {
connection . readCoils ( { address : 5 , quantity : 8 } , ( err , info ) => {
console . log ( "response" , info . response . data ) ;
} ) ;
} ) . listen ( 12345 , ( ) => {
modbus . tcp . connect ( 12345 , { debug : "client" } , ( err , connection ) => {
connection . on ( "read-coils" , ( request , reply ) => {
reply ( null , [ 1 , 0 , 1 , 0 , 1 , 1 , 0 , 1 ] ) ;
} ) ;
} ) ;
} ) ;
要通过 TCP 连接到 modbus 设备,请使用:
var modbus = require ( "modbus-stream" ) ;
modbus . tcp . connect ( 502 , "134.2.56.231" , { debug : "automaton-2454" } , ( err , connection ) => {
// do something with connection
} ) ;
要侦听 TCP 上的连接,请使用:
var modbus = require ( "modbus-stream" ) ;
modbus . tcp . server ( { debug : "server" } , ( connection ) => {
// do something with connection
} ) . listen ( 502 , ( ) => {
// ready
} ) ;
要通过串行端口连接到 modbus 设备,请使用:
var modbus = require ( "modbus-stream" ) ;
modbus . serial . connect ( "/dev/ttyS123" , { debug : "automaton-123" } , ( err , connection ) => {
// do something with connection
} ) ;
建立连接后,您可以发送请求并侦听响应。
modbus . serial . connect ( "/dev/ttyS123" , { debug : "automaton-123" } , ( err , connection ) => {
if ( err ) throw err ;
connection . readCoils ( { address : 52 , quantity : 8 , extra : { unitId : 25 } } , ( err , res ) => {
if ( err ) throw err ;
console . log ( res ) ; // response
} )
} ) ;
每个方法都接受一个对象options
,该选项具有默认参数(例如address = 0
)和回调,以防您想查看远程设备的响应。以下是支持的功能代码和相应方法的列表:
碱基读数
readCoils
( address = 0
, quantity = 1
)readDiscreteInputs
( address = 0
, quantity = 1
)readHoldingRegisters
( address = 0
, quantity = 1
)readInputRegisters
( address = 0
, quantity = 1
)基础写入
writeSingleCoil
( address = 0
, value = 0
)writeSingleRegister
( address = 0
, value = <Buffer 0x00 0x00>
)writeMultipleCoils
( address = 0
, values = []
)writeMultipleRegisters
( address = 0
, values = [ <Buffer 0x00 0x00> ]
)文件记录
readFileRecord
( requests = []
)writeFileRecord
( requests = []
)先进先出
readFifoQueue
( address = 0
)先进的
maskWriteRegister
( address = 0
, andmask = 0xFFFF
, ormask = 0x0000
)readWriteMultipleRegisters
( read_address = 0
, read_quantity = 1
, write_address = 0
, values = [ <Buffer 0x00 0x00> ]
)readDeviceIdentification
( type = "BasicDeviceIdentification"
, id = "ProductName"
)readExceptionStatus
()getCommEventCounter
()getCommEventLog
()有关这些方法的更多信息,请查看用于构建数据包的 pdu 存储库。
要响应远程请求,请侦听事件。
modbus . serial . connect ( "/dev/ttyS123" , {
// except "debug", everything else is the default for serial
baudRate : 9600 ,
dataBits : 8 ,
stopBits : 1 ,
parity : "none" ,
debug : "automaton-123"
} , ( err , connection ) => {
if ( err ) throw err ;
connection . events . on ( "read-coils" , ( req , reply ) => {
console . log ( req ) ; // request
// ...
return reply ( null , [ data ] ) ;
} )
} ) ;
有一些事件从传输传播到流。您应该绑定一些事件侦听器,以防连接或串行设备错误或刚刚关闭。请记住,在 NodeJS 中,没有侦听器的发出错误事件将导致进程抛出uncaughtException
。
close
)当serialport
模块发出close
事件或套接字发出end
事件时,会发出此事件。
error
)如果底层流发生某些事情,例如ECONNRESET
或类似的事情,则会发生此事件。