我是网络开发和JS的新手,我一直在努力让一个节点js服务器托管一个网页,该网页将串行命令发送到连接到Windows 7"服务器&的设备#34 ;.它可以在localhost:8000上的服务器上运行,并将html传送到远程计算机,但它不会在服务器上发送串行命令的后端进程(从远程PC运行时)。
server.js代码:
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
url = require('url'),
SerialPort = require('serialport')//.SerialPort,
// initialize serialport using the /dev/cu.usbmodem1411 serial port
// remember to change this string if your arduino is using a different serial port
sp = new SerialPort('COM3',{
//'/dev/cu.usbmodem1411', {
baudRate: 9600,
parser:SerialPort.parsers.readline("\r\n")
}),
// this var will contain the message string dispatched by arduino
arduinoMessage = '',
/**
* helper function to load any app file required by client.html
* @param { String } pathname: path of the file requested to the nodejs server
* @param { Object } res: http://nodejs.org/api/http.html#http_class_http_serverresponse
*/
readFile = function(pathname, res) {
// an empty path returns client.html
if (pathname === '/')
pathname = 'client.html';
fs.readFile('client/' + pathname, function(err, data) {
if (err) {
console.log(err);
res.writeHead(500);
return res.end('Error loading client.html');
}
res.writeHead(200);
res.end(data);
});
},
/**
*
* This function is used as proxy to print the arduino messages into the nodejs console and on the page
* @param { Buffer } buffer: buffer data sent via serialport
* @param { Object } socket: it's the socket.io instance managing the connections with the client.html page
*
*/
sendMessage = function(buffer, socket) {
// concatenating the string buffers sent via usb port
arduinoMessage += buffer.toString();
// detecting the end of the string
if (arduinoMessage.indexOf('\r') >= 0) {
// log the message into the terminal
// console.log(arduinoMessage);
// send the message to the client
socket.volatile.emit('notification', arduinoMessage);
// reset the output string to an empty value
arduinoMessage = '';
}
};
// creating a new websocket
io.sockets.on('connection', function(socket) {
// listen all the serial port messages sent from arduino and passing them to the proxy function sendMessage
sp.on('data', function(data) {
sendMessage(data, socket);
});
// listen all the websocket "lightStatus" messages coming from the client.html page
socket.on('lightStatus', function(lightStatus) {
sp.write(lightStatus + '\r', function() {
// log the light status into the terminal
console.log('the light should be: ' + lightStatus);
});
});
});
// just some debug listeners
sp.on('close', function(err) {
console.log('Port closed!');
});
sp.on('error', function(err) {
console.error('error', err);
});
//sp.on('data', function(data){
// console.log("on Data ");
//});
sp.on('data', showSerialData);
function showSerialData(data){
console.log(data);}
sp.on('open', function() {
console.log('Port opened!');
});
// creating the server ( localhost:80 )
app.listen(8000);
// server handler
function handler(req, res) {
readFile(url.parse(req.url).pathname, res);
}

和app.js:
(function() {
'use strict';
// create a new websocket
var socket = io.connect('http://localhost:8000'),
// select all the DOM elements needed for this experiment
$body = $('body'),
$btn = $('button'),
$lightStatus = $('span', $btn),
// the light must be off by default
lightStatus = 'off',
// toggle the light status using the button on the page
toggleLightStatus = function() {
// switch the lightStatus var ...
lightStatus = lightStatus === 'off' ? 'on' : 'off';
// pass its value to the nodejs server via websocket
socket.emit('lightStatus', lightStatus);
},
onSocketNotification = function(data) {
// print all the messages coming from the arduino board
var $div = $('<center>');
$div.html(data);
$body.append($div);
$div.delay(1000).fadeOut(function() {
$div.remove();
});
// filter the light status notifications
if (/on|off/gi.test(data)) {
// update the text inside the button
$lightStatus.text(data);
}
};
// Set listeners
socket.on('notification', onSocketNotification);
$btn.on('click', toggleLightStatus);
// turn off the light by default on any new connection
socket.emit('lightStatus', lightStatus);
}());
&#13;
以下是调用应用的HTML
<html>
<head>
<!--
* Author: Gianluca Guarini
* Contact: gianluca.guarini@gmail.com
* Website: http://www.gianlucaguarini.com/
* Twitter: @gianlucaguarini
-->
<title>Arduino and Nodejs</title>
<style>
center {
font-size: 100px;
font-family:arial;
}
</style>
</head>
<body>
<button>
Turn the light
<span>on</span>
</button>
<script src="socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
&#13;