Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
471 stars 175 forks source link

What does the error mean? #113

Closed panyunyi closed 7 years ago

panyunyi commented 7 years ago

image image Hi,I use the websocket to read the data in real time and displayed on the page. There is an error when I refresh the page or open a new page.

panyunyi commented 7 years ago

It was worked for the first time.

var client = modbus.client.tcp.complete({ 
        'host'              : '192.168.1.8', 
        'port'              : 10000,
        'autoReconnect'     : false,
        'reconnectTimeout'  : 1000,
        'timeout'           : 5000,
        'unitId'            : 1
    });

//socket
io.sockets.on( "connection", function( socket ){
    console.log( "socket.io" );
    client.connect();
    client.on('connect', function () {
        console.log("modbus!");
        setInterval(function(){
            client.readInputRegisters(1,24).then(function(resp){
                let arr=resp.payload;
                let info=[];
                for(let i=0;i<arr.length/4;i++){
                    let arr1=arr.slice(i*4,i*4+4);
                    let temp1=arr1[0];
                    let temp2=arr1[1];
                    arr1[0]=arr1[2];
                    arr1[1]=arr1[3];
                    arr1[2]=temp1;
                    arr1[3]=temp2;
                    info.push(arr1.readFloatBE());
                }               
                socket.emit('info',info.join(","));
            },console.error);
        },1000);
    });
    client.on('error', function (err) {
        console.log(err);
    });
});

image

panyunyi commented 7 years ago

Sorry to bother you again.I edit the code and it's worked.Thank you! I don't know how to close the issue.

client.connect();
client.on('connect', function () {
    console.log("modbus!");
    io.sockets.on( "connection", function( socket ){
        console.log( "socket.io" );
        setInterval(function(){
        client.readInputRegisters(1,24).then(function(resp){
            let arr=resp.payload;
            let info=[];
            for(let i=0;i<arr.length/4;i++){
                let arr1=arr.slice(i*4,i*4+4);
                let temp1=arr1[0];
                let temp2=arr1[1];
                arr1[0]=arr1[2];
                arr1[1]=arr1[3];
                arr1[2]=temp1;
                arr1[3]=temp2;
                info.push(arr1.readFloatBE());
            }               
            socket.emit('info',info.join(","));
        },console.error);
    },1000);
    });
});