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或類似的事情,則會發生此事件。