我有一个由微处理器组成的系统,它通过串口连接到树莓派。raspberry pi使用node-serialport运行node.js,以提供基于web的用户界面。
当我尝试通过web接口写入串口时,在收到微控制器的回复(10秒到几分钟)之前,我经常遇到很长的延迟,这意味着serialport.write不知何故被延迟了。(我没有看到错误,端口也没有关闭。)在第6次串口写入之后,这种情况会持续发生。如果我重新加载浏览器,它将再次正常工作,直到第7个请求。
这是在node.js中打开数据并将数据推送到串口的例程:
var myPort = new SerialPort("/dev/ttyAMA0", {
baudrate: 38400,
parser: serialport.parsers.readline("\n")
});
app.post('/sendtoavr', function(req, res) {
myPort.write(req.body.cmd);
});
myPort.on('close', function(err) {
console.log('node-serialport closed!!!');
});
myPort.on('error', function(err) {
console.log('node-serialport error!!!');
});此jquery例程用于向rpi发送http post请求:
$("#btnSearchTempSensors").click(function() {
var strCMD = String.fromCharCode(84) + String.fromCharCode(83) + String.fromCharCode(13);
$.post('/sendtoavr', {cmd: strCMD});
});当在chrome中查看网络控制台时,它看起来http post请求立即被发送,并且当通过ssh通过minicom向微控制器发送命令时,没有任何问题。
因此,我怀疑是节点串口还是node.js,但是如何进一步挖掘呢?
有人有什么想法吗?
发布于 2014-03-08 17:53:45
找到了一个解决方法..我没有通过http post请求发送命令,而是使用了socket.io/websockets。
node.js:
socket.on('sendcmd', function(data) {
myPort.write(data);
});index.html:
$("#btnListTempSensors").click(function() {
var strCMD = String.fromCharCode(84) + String.fromCharCode(76) + String.fromCharCode(13);
socket.emit('sendcmd', strCMD);
});https://stackoverflow.com/questions/22263094
复制相似问题