send message to the web application serial gates automatic face recognition floodgates

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/bowei026/article/details/90760729

The previous articles introduced the idea of ​​face recognition technology, this paper introduces web application technologies to achieve automatic gate opening machine face recognition after successful ideas.

First, the realization of ideas

To trigger the automatic gate opening gates, you must send a message to the gates of the gate opening. And the gates correspond generally used RS232 serial protocol standard protocols. That must send a message to the RS232 serial port gates, and web applications that can not communicate with local hardware, that web applications with Serial occurred message interaction needs to use the middle of the service provider to deliver the message, the following are the main technical ideas:

1, because the web can not directly access the RS232 serial port, so that the intermediate introduction WebSocket, web application implemented with RS232 serial communications gates.

2, web programs use js call websocket is the basic function of the web application, no special development

3, the use nodejs websocket technology, used in nodejs gates up websocket local computer is also simple. The module can nodejs serialport simultaneously communicate with serial RS232 gates

Second, the code

1, web program gate.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .kuang{text-align: center;margin-top:50px;}
        #mess{text-align: center}
        .value{width: 200px;height:200px;border:1px solid;text-align: center;line-height: 200px;display: inline-block;}
    </style>
</head>
<body>
    <div id="mess">正在连接...</div>
    <div class="kuang">
        <div class="value" id="value1">打开闸机</div>
    </div>

    <script>
        var mess = document.getElementById("mess");
        if(window.WebSocket){
            var ws = new WebSocket('ws://localhost:8001');

            ws.onopen = function(e){
                console.log("连接ws服务器成功");
                ws.send("gate");
            }
            ws.onclose = function(e){
                console.log("ws服务器关闭");
            }
            ws.onerror = function(){
                console.log("ws连接出错");
            }

            ws.onmessage = function(e){
                mess.innerHTML = "ws连接成功"
                document.querySelector(".value").onclick = function(e){
                    ws.send("open");
                }
            }
        }
    </script>
</body>
</html>

web application directly call the ws localhost interface does not require specially designated ip address. ws of nodejs realize just below.

2, websocket server code websocket.js

var ws = require("nodejs-websocket");
var SerialPort = require("serialport");

console.log("开始建立连接...")
var serialPort = new SerialPort("COM2", {
   baudRate: 19200
});

var server = ws.createServer(function(conn){
    conn.on("text", function (str) {
        console.log("收到的信息为:"+str)
        if(str==="open"){
            // 假设16进制的 1E 60 01 00 00 00 2F 是开闸需要的指令
            const hexBuf = new Buffer("1E60010000002F", "hex");
            serialPort.write(hexBuf, function(err, results) {
              if (err) {
                return console.log('Error on write: ', err.message)
              }
              console.log('message written ok')
            });
        }
    })
    conn.on("close", function (code, reason) {
        console.log("关闭连接")
    });
    conn.on("error", function (code, reason) {
        console.log("异常关闭")
    });
}).listen(8001)
console.log("WebSocket建立完毕")

This part of the code needs to node environment, you need to install nodejs, not described in detail here, assumed to have been installed nodejs.

The above code serialport and the need to rely nodejs nodejs-websocket module, Run npm install serialport nodejs-websocket

Third, run

Run websocket:

node websocket.js

Then open your browser and enter the address gate.html, click on "open gates" button, the gates will automatically open.

Fourth, the local analog RS232 serial debugging

If the program can be debugged locally before deploying code on the actual gates, you will save a lot of time.

1, vspd virtual serial port software to a virtual RS232 serial port locally. (Please use vspd of self-learning)

2, a program write port of the program responsible for listening port, serial received message (analog gates) real-time display, com_2.js code below

// 这是串口的接收端,打开串口COM2,对串口数据进行监听。当有数据发送到串口COM1就能被监听到并显示数据
var SerialPort = require("serialport");
//设置串口号,波特率,关闭自动开启
var port = new SerialPort("COM2", {
   baudRate: 19200,  //波特率
   dataBits: 8,    //数据位
   parity: 'none',   //奇偶校验
   stopBits: 1,   //停止位
   flowControl: false,
   autoOpen:false //不自动打开
});

//串口打开
port.open(function (err) {
  if ( err ) {
      return console.log('failed to open: ',arr.message);
  } else {
    console.log('open');
    //接受串口数据,并打印到终端
    port.on('data', function(data) {
      console.log('数据接收: ' + data);
    });
  }
});

This code is also dependent on the nodejs serialport module, if not the module execution command npm install serialport. Running this code node com_2.js

This code can also be used instead of serial debugging software, such as serial debugging assistant awe like. (Self-learning)

V. Summary

Combined with the previous article, after the success of face recognition, combined with the idea of ​​this paper can be realized automatically open the application after face recognition gates of success.

This concludes this article, it may be more concerned about the number of public and personal micro signal:

Guess you like

Origin blog.csdn.net/bowei026/article/details/90760729