Modbus 데이터를 처리하는 데 도움이 되는 NodeJS 모듈입니다. 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
} )
} ) ; 모든 메소드는 원격 장치의 응답을 보려는 경우를 대비하여 기본 매개변수(예: address = 0 )와 콜백이 있는 객체 options 허용합니다. 지원되는 기능 코드와 해당 메소드 목록은 다음과 같습니다.
기본 읽기
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 = [] )FIFO
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 또는 이와 유사한 일이 기본 스트림에 발생하는 경우 이 이벤트입니다.