Echats 与serialport、websocket

通信
前端

  • <title>ECharts</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    </head> <body>
    <div id="main" style="height:400px;"></div>
    <script src="http://echarts.baidu.com/build/dist/echarts-all.js"></script>
    <script src="js/jquery-1.12.3.min.js"></script>
    <script>
        var myChart;
        window.onload=function(){
            myChart = echarts.init(document.getElementById('main'));
            wssk();
        }
        
        function setOptionn(data,data1){
               //根据需求进行相应的设置
        var option = {
           
           title: { 
             text: '数据方式'
         },
         backgroundColor: '#FFFFFF',
         tooltip : {
             trigger: 'axis'
         },
         legend: {
             data:['加入数据']
         },
         calculable : true,
         xAxis : [{
                 type : 'category',// 坐标轴类型,横轴默认为类目型'category',纵轴默认为数值型'value'
                 name : '时间',
                 data :  (function (){
                var now = new Date();
                var res = [];
                var len = 10;
                while (len--) {
                    res.unshift(now.toLocaleTimeString().replace(/^\D*/,''));
                    now = new Date(now - 2000);
                }
                return res;
            })()
             }
            ],
         yAxis : [ ],
         series : [ {
                        name: '直接访问',
                        type: 'line',
                        barWidth: '60%',
                        data: data
                    }
                 ]
         };
         myChart.setOption(option);
         }
    
        function wssk(){
            var ws = new WebSocket('ws://localhost:8080/ws'); 			ws.onmessage=function(e){
                console.log(e.data)
                var dat=e.data;
                var n1=dat.length;
                var n2=dat.indexOf("=");
                var idd=dat.substr(n2+1,n1-n2);
                console.log(idd); 
    
                
                setOptionn(idd);
    
             };
           
         ws.onerror=function(err){
     	
     	           console.log(err);
             };
         ws.onopen=function(){
     	     
             };
         ws.onclose=function(){
     	        console.log('_close');
             };
        }
        
    
       // 初次加载图表
       
    </script> </body> </html>
    

后端

// Websocket
var webSocketsServerPort = 8080;
var webSocketServer = require(‘websocket’).server;
var http = require(‘http’);
var server = http.createServer(function(request, response) {
// Not important for us. We’re writing WebSocket server, not HTTP server
});
var clients = [];

server.listen(webSocketsServerPort, function() {
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
});

var wsServer = new webSocketServer({
// WebSocket server is tied to a HTTP server. WebSocket request is just
// an enhanced HTTP request. For more info http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
});

wsServer.on(‘request’, function(request) {
console.log((new Date()) + ’ Connection from origin ’ + request.origin + ‘.’);
var connection = request.accept(null, request.origin);
console.log((new Date()) + ’ Connection accepted.’);

var index = clients.push(connection) - 1;

// user sent some message
connection.on('message', function(message) {
  onReceive(message);
});

// user disconnected
connection.on('close', function(connection) {
    
        console.log((new Date()) + " Peer "
            + connection.remoteAddress + " disconnected.");
        // remove user from the list of connected clients
        clients.splice(index, 1);
    
});

});

function onReceive(msg)
{
console.log(“ws msg:” + msg);
serialPort.write(msg);
}

function onSerial(msg)
{
console.log(“uart msg:” + msg);
for (var i=0; i < clients.length; i++)
clients[i].sendUTF(msg);
}

// Serial port
var SerialPort = require(“serialport”)
var portName = ‘COM6’;
var buffer = “”;

var serialPort = new SerialPort(portName, {
baudRate: 9600,
// defaults for Arduino serial communication
dataBits: 8,
parity: ‘none’,
stopBits: 1,
flowControl: false
});

serialPort.on(“open”, function () {
console.log(‘open serial communication’);
// Listens to incoming data
serialPort.on(‘data’, function(data) {

  buffer += new String(data);
  var lines = buffer.split("\n");
  while ( lines.length > 1 )
    onSerial( lines.shift() );
  buffer = lines.join("\n");

});
});

其实还有点没懂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值