我正在尝试为Node js中的PLC写一个简单的scada。在同一个应用程序中,它分别监听端口9007和9001的tcp和http服务器。从plc发送的值应在网络中更新。 这是我的代码
'use strict';
var socket = require('socket.io');
var express = require('express');
var app = express();
app.use(express.static('public'));
var gid = require('uniqid');
var server = app.listen(9001, function () {
console.log("server listening port 9001");
});
var io = socket(server);
io.on('connection', function (socket) {
console.log("new web client connected", socket.id);
socket.on('chat', function (dataobj) {
var buffr = Buffer.from(GetSocketByPLCID(dataobj.station_id).plc.data);
var prv_data ={
station_id: "oooo",
batt_stat: ((buffr[5]+(buffr[6] << 8))*0.0032).toFixed(2).toString(),
turb_vol: ((buffr[7]+(buffr[8] << 8))/10).toFixed(2).toString(),
inlet_pr: ((buffr[9]+(buffr[10] << 8))*0.00175).toFixed(2).toString(),
outlet_pr: ((buffr[11]+(buffr[12] << 8))*0.00175).toFixed(2).toString(),
flowrate: ((buffr[11]+(buffr[12] << 8))*6.387).toFixed(2).toString(),
opening: ((buffr[11]+(buffr[12] << 8))*0.08516).toFixed(2).toString(), // flowrate*0.62
set_val: ((buffr[15]+(buffr[16] << 8))/100).toFixed(2).toString(),
total_flow: (buffr[17]+(buffr[18] << 8)+(buffr[19] << 16)+(buffr[20] << 24)).toString()
}
var flashed = socket.emit('chat', prv_data);
if(flashed){
console.log(buffr)
} else{socket.pause();}
});
socket.on('drain', function(){socket.resume();})
socket.on('disconnect',function()
{
console.log("webclient disconnected");
});
});
/////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
///// TCP server
var plc_socket_info ={};
var plc_socket_arr=[];
var net = require('net');
var TCPserver = new net.createServer();
TCPserver.listen(9007, function () {
console.log("TCP server running @ 9007");
});
function GetSocketByPLCID(PLCstationID)
{
function searchs(plc_socket_arr) {
try
{
return plc_socket_arr.plc.stn_id === PLCstationID;
}
catch (err){return}
}
return plc_socket_arr.find(searchs);
}
TCPserver.on('connection', function (tcpsocket) {
console.log("New client connected");
TCPserver.getConnections(function(err,count_connections){
console.log(count_connections.toString() + " PLC(s) are connected");
})
tcpsocket.plc={stn_id: 'xxxx', data: 'hxxxx0000000000000000y'}
plc_socket_arr.push(tcpsocket);
tcpsocket.setEncoding = 'utf8';
let tcpdata;
tcpsocket.on("data", function (data)
{
var txt_data = data.toString('utf8');
console.log(data);
var buffr = new Buffer(data);
if (txt_data.substr(0,1)==='h')
{
var stn_id = txt_data.substr(1,4);
try{plc_socket_arr[plc_socket_arr.indexOf(tcpsocket)].plc = {stn_id: stn_id, data: txt_data}} catch (err){if(err){console.log(err.toString())}};
});
function terminatePLC(){
tcpsocket.end(function()
{
plc_socket_arr.splice(plc_socket_arr.indexOf(tcpsocket),1);
console.log("socket destroyed for time out");
console.log(plc_socket_arr);
}
);
}
tcpsocket.on("error", function (err) {
console.log('error: ',err.message);
terminatePLC();
});
tcpsocket.setTimeout(3000,function() {
console.log("socket will end..")
terminatePLC();
});
});
在这里,我将传入的plc保存到具有适当ID的对象数组中,并保持更新。当我从网络上打电话时,index.html不断将请求发送到服务器,并使用该ID在该数组上进行搜索并发出。我的问题是,在网站上停止4-5分钟的值更新后。我观察到plc(tcp 9007)可以被ping通,但停止发送数据。重新启动plc客户端只能解决该问题...我不确定为什么会这样。请提出任何解决方案。