|
因公司业务需求,想要将电子称的数据能够通过COM3端口时时的传递的Web页面中。
方案是写一个客户端程序运行接收COM3传来的数据,同时将接收到的数据通过socket传递给页面。
本地node版本是v5.0.0,程序主入口是app.js,安装serialport,socket.io,代码如下:
var io;
function ioHandle() {
io = require('socket.io').listen(config.socketPort);
io.on('connection', function (socket) {
//监听客户端推送的消息事件
socket.on('connect', function () {
console.log('io.connect');
});
socket.on('weight_server', function (data) {
console.log('io.data : ' + data);
var reg = /\d+.\d/g;
var weight = data.match(reg);
// 广播给所有侦听者
socket.broadcast.emit('weight_client', (weight.length > 0 ? weight[0] : 0));
});
socket.on('disconnect', function () {
console.log('io.disconnect someone disconnect server');
});
});
io.on('error', function (err) {
console.log("io.error: " + err);
setTimeout(ioHandle, 1000);
});
}
ioHandle();
var ioClient = require("socket.io-client");
var ioConnectUrl = config.socketUrl + ':' + config.socketPort;
var socketClient = ioClient.connect(ioConnectUrl);
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var sp;
function serialportHandle() {
sp = new SerialPort("COM3", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
sp.on("open", function () {
console.log('sp.open');
sp.on('data', function (data) {
console.log('sp.data received: ' + data);
socketClient.emit('weight_server', data);
});
sp.on('disconnect', function(err) {
console.log('sp.disconnect');
});
sp.on('close', function() {
console.log("sp.close someone close the port");
});
});
sp.on('error', function (err) {
console.log("sp.error: " + err);
setTimeout(serialportHandle, 2000);
});
}
serialportHandle();
console.log('sscom3 running, god bless love...');
前端index.html,代码如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>电子称应用程序</title>
</head>
<body >
<script src="socket.io.js?v=1"></script>
<h2 id="weight">目前重量:无</h2>
<script>
//socket client connect
var url = "http://127.0.0.1:10025";
var socket = io.connect(url);
//socket连接情况
socket.on('connect',function(){
console.log('client.connect');
});
socket.on('error',function(){
console.log('client.connect error');
});
socket.on('disconnect',function(){
console.log('client.connect abort');
});
//自定义通知消息
socket.on('weight_client', function (data) {
console.log('client.weight_client data: ' + data);
document.getElementById('weight').innerHTML = '目前重量:' + data;
});
</script>
</body>
</html>
程序用node app.js,很快也很简单的成功执行了起来,但是。。。
还要将其用node-webkit封装成一个桌面应用程序,package.js内容如下:
{
"name": "sscom3",
"main": "index.html",
"nodejs": "true",
"node-main": "app.js",
"window": {
"toolbar": true,
"frame": true,
"width": 430,
"height": 450,
"resizable": true
},
"webkit": {
"plugin": true
}
}
下载 nwjs,我选择的版本是v0.12.3,环境为win7 x64
将程序打包之后放入nw.exe中执行,程序报错:
Cannot extract package
Failed to unzip the package file: C:\Users\Administrator\Desktop\nwjs-v0.12.3-win-x64\sscom3.zip
网上找了很多资料,有的说的是serialport的库编译有问题,要使用node-gyp 或 nw-gyp 编译!!!!
最后才发现这个问题是sscom3\node_modules\serialport\node_modules中有个node-pre-gyp-github导致的,直接删除node-pre-gyp-github文件夹即可。
此问题解决之后,新问题又出现了,程序闪现,执行之后直接退出,一直找不到原因。
经过一系列的尝试,去掉 package.json中 "nodejs": true,"node-main": "app.js", 然后将index.html改为如下
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>电子称应用程序</title>
</head>
<body>
<h2 id="weight" >目前重量:无</h2>
<script src="app.js?v=1"></script>
<script>
//socket client connect
var config = require('./config');
var client = require("socket.io-client");
var socketUrl = config.socketUrl + ':' + config.socketPort;
var socket = client.connect(socketUrl);
//socket连接情况
socket.on('connect',function(){
console.log('client.connect');
});
socket.on('error',function(){
console.log('client.connect error');
});
socket.on('disconnect',function(){
console.log('client.connect abort');
});
//自定义通知消息
socket.on('weight_client', function (data) {
console.log('client.weight_client data: ' + data);
document.getElementById('weight').innerHTML = '目前重量:' + data + ' g';
});
</script>
</body>
</html>
此时程序运行不会退出了,原以为这样解决了问题,但连接上电子称之后,页面收不到传来的值,查看调试器问题如下:
然后网上找解决问题方法,有两篇文章提供了解决方案
http://markding.github.io/2014/11/11/make-node-webkit-works-with-serialport.html
https://github.com/nwjs/nw.js/wiki/Build-native-modules-with-nw-gyp
node-pre-gyp configure --runtime=node-webkit --target=0.12.3
node-pre-gyp build --runtime=node-webkit --target=0.12.3
使用此方法后,程序可正常运行了!
现在开始打包程序为.exe文件,按网上教程,随意找一篇如 http://damoqiongqiu.iteye.com/blog/2010720
copy /b nw.exe+sscom3.zip sscom3.exe
此时蛋疼的问题来了,执行后,发现sscom3.exe打开后报生成的serialport.node模块找不到,报如上同样的错。
后来才发现这是nw底层代码中对原生态C++编译后的模块无法重命名导致的,如果程序命名为nw.exe,则可成功运行
又找了好久,在网上找了一个帖子 https://github.com/nwjs/nw.js/issues/199
这个帖子已经3-4年了,近两年才解决这个问题,方法是有个家伙写了一个小玩意,地址是 http://developers.ironsrc.com/rename-import-dll/
这东西用了之后,感谢 上帝,终于成功了!!!
总结,node-webkit与 node native module 兼容性真心不高,蛋疼的要命!
最后归纳:
1. 安装端口读写库 npm install serialport
2. npm install node-gyp -g
3. npm install nw-gyp -g
4. 2与3操作不执行好像也可以,最终还是依赖与以下操作
5. npm install node-pre-gyp -g
6. 定位到端口库中 cd node_modules\serialport
7. 执行 node-pre-gyp configure --runtime=node-webkit --target=0.12.3
8. 执行 node-pre-gyp build --runtime=node-webkit --target=0.12.3
9. 在 node_modules\serialport\build\Release中查看是否已经生成一个node-webkit-v0.12.3-win32-x64文件夹
10. 删除 serialport\node_modules 文件夹下 node-pre-gyp-github
11. 安装 npm install socket.io
12. 将app.js入口程序绑定到index.html中
13. 压缩整个文件夹命名sscom3.zip放到 nwj-v0.12.3 包中
14. 定位到 nwj-v0.12.3 文件夹下
15. 执行 copy /b nw.exe+sscom3.zip sscom3.exe
16. 蛋疼的问题来了,执行15步骤之后,发现sscom3.exe打开后报生成的serialport.node模块找不到,原因是nw底层代码中对原生态的C++编译后的模块无法重命名,只能命名为nw.exe
17. 针对16项目问题,网上找到一个解决方案,http://developers.ironsrc.com/rename-import-dll/,下载rid包
18. 定位到 cd node_modules\serialport\build\Release\node-webkit-v0.12.3-win32-x64
19. 先将rid.exe拷入此文件夹中,执行rid.exe serialport.node,再执行rid.exe serialport.node nw.exe sscom3.exe
20. 删除rid.exe,重新执行13,14,15
21. 打包之后,sscom3.exe可以成功运行
22. 使用 Enigma Virtual Box 软件打包sscom3.exe程序
23. 点击Add..,加入nwj-v0.12.3中ffm.pegsumo.dll,icudtl.dat,libEGL.dll,libGLESv2.dll,nw.pak,同时在Files Options中勾选Compress Files
24. 注:Enigma Virtual Box 打包之后,将打包文件的名字重命名为sscom3.exe
注: 无需在package.json中设置 "nodejs": true,"node-main": "app.js", 否则通不过 |
|
|