Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino wireless和Node.js_Node.js_Serial Port_Usb_Arduino_Wireless - Fatal编程技术网

Arduino wireless和Node.js

Arduino wireless和Node.js,node.js,serial-port,usb,arduino,wireless,Node.js,Serial Port,Usb,Arduino,Wireless,我在做一个Arduino项目,控制马达和读取传感器。我决定使用web视图作为介质通道,使用任意库(SerialPort和SerialPort2)从串行端口读/写到浏览器 当我使用导线将Arduino直接连接到USB设备时,这两种方法都可以正常工作,但当我通过无线适配器**()将Arduino连接到USB设备时,Node.js似乎无法读取任何内容,即使我可以使用Arduino串行监视器读取其上接收到的所有内容 我检查了所有可能的原因;我检查了用于Arduino与wirelss串行和APC220以及

我在做一个Arduino项目,控制马达和读取传感器。我决定使用web视图作为介质通道,使用任意库(SerialPort和SerialPort2)从串行端口读/写到浏览器

当我使用导线将Arduino直接连接到USB设备时,这两种方法都可以正常工作,但当我通过无线适配器**()将Arduino连接到USB设备时,Node.js似乎无法读取任何内容,即使我可以使用Arduino串行监视器读取其上接收到的所有内容

我检查了所有可能的原因;我检查了用于Arduino与wirelss串行和APC220以及桥接器(USB到串行转换器)通信的波特率。它们都有相同的设置:9600波特率,无奇偶校验/流量控制,数据位:8,停止位:1

行为如下。它毫无问题地连接到,然后我尝试打印错误,但两个SerialPort库似乎都无法识别错误。然后没有读取事件(数据),这意味着它(Node.js)没有与serialport交互,即使它是打开的

注: 我知道我可以使用另一个Arduino作为USB端口和无线适配器之间的媒介,但我想了解这个问题,并在没有这种解决方法的情况下干净地解决它

有什么问题吗

服务器[node.js]:

var SerialPort  = require('serialport2').SerialPort;
var portName = 'COM15';

var io = require('socket.io').listen(8000); // Server listens for socket.io communication at port 8000
io.set('log level', 1); // Disables debugging. This is optional. You may remove it if desired.

var sp = new SerialPort(); // Instantiate the serial port.
sp.open(portName, { // portName is instatiated to be COM3, replace as necessary
   baudRate: 9600, // This is synchronised to what was set for the Arduino code
   dataBits: 8, // This is the default for Arduino serial communication
   parity: 'none', // This is the default for Arduino serial communication
   stopBits: 1, // This is the default for Arduino serial communication
   flowControl: false // This is the default for Arduino serial communication
});

io.sockets.on('connection', function (socket) {
    // If socket.io receives message from the client browser then
    // this call back will be executed.
    socket.on('message', function (msg) {
        console.log(msg);
    });
    // If a web browser disconnects from Socket.IO then this callback is called.
    socket.on('disconnect', function () {
        console.log('disconnected');
    });
});

var cleanData = ''; // This stores the clean data
var readData = '';  // This stores the buffer
sp.on('data', function (data) { // Call back when data is received
    readData = data.toString(); // Append data to buffer.
    // If the letters '[' and ']' are found on the buffer then isolate what's in the middle
    // as clean data. Then clear the buffer.
    console.log(readData); // **Here you should be able to print the data if you receive any**
     if (readData.indexOf(']') >= 0 && readData.indexOf('[') >= 0) {
        cleanData = readData.substring(readData.indexOf('[') + 1, readData.indexOf(']'));
        readData = '';
        console.log("-- "+cleanData);
        io.sockets.emit('message', cleanData);
     }else if(readData.indexOf('[') >= 0){
        cleanData = readData.substring(readData.indexOf('[') + 1, readData.length);
        readData = '';
     }else if(readData.indexOf(']') >= 0){
        cleanData += readData.substring(0, readData.indexOf(']'));
        readData = '';
        console.log("-- "+cleanData);
        io.sockets.emit('message', cleanData);
     }else{
        cleanData += readData;
        readData = '';
     }
    //console.log(readData);
    //io.sockets.emit('message', readData);
});

当监视器运行时,没有其他程序可以读取串行端口

如果你不能同时打开这两个文件,那么事情就更棘手了。我的建议是监视电线。即:安装并查看串行连接/USB总线上的数据


您可能还想检查APC220和Arduino的串行端口在串行/USB转换器方面的差异。另一个想法是在Linux下分析这个问题,因为这可能会让我们更深入地了解芯片组/USB活动的低级差异。当然,如果你没有Linux的经验,这是很难做到的,但是也许你知道一些Linux爱好者。

嗯,你的代码看起来都不错,所以我很确定你的问题是显而易见的(比如你脸上的鼻子),因为你太关注细节了,所以你看不到。这是我首先要做的清单:

  • 您是否确定您的串行接口是
    COM15
    ,并且从不更改
  • 您确定两个
    APC
    设备都配置了正确的波特率吗
  • 你有没有试着让你的Arduino发送一个简单的代码,通过通道发送相同的东西
比如:

在您的主机上:

sp.on('data', function (data) {
    console.log(data.toString());
});
当您的系统中出现错误时,请尝试构建该错误部分的最简单用例,这样您就可以确定代码中没有任何其他内容会干扰这一点。你不需要让你的Arduino在GPS上工作,也不需要让你的Node.js在web上工作

尽你所能让它变得最简单。(别忘了在Arduino循环中添加延迟,否则您可能难以对芯片进行再灰化)

您可能还想在代码中添加
serialport2
的错误捕获部分:

port.on('error', function(err) {
  console.log("ERROR receiving serial data: ", err);
});
对于您的
open()
语句:

sp.open(portName, portConfig, function (err) {
    console.log("ERROR opening serial port: ", err);
});

因为您可能缺少主机端的错误报告

我没有在服务器上同时使用串行监视器,我知道它会导致冲突(被其他应用占用/使用…)。这是一个好主意,我将尝试分析一些详细的串行端口读取器不久的数据包。但我已经检查了转换器的设置,它们很好。事实上,由于它正在处理串行监视器和java的串行读取器(我在另一个java项目中使用了它),这意味着问题存在于node js中的串行端口库中。根据分析结果,可能需要进行一些操作。谢谢,考虑到你在评论中所说的,你应该给出你用javascript编写的代码来打开串口,以便我们在这方面为你提供帮助。你用的是哪一个库,如何打开串行线?您是否进行了正确的termios设置?你有没有试着用最简单的程序把一个“你好世界”从arduino发送到主机并返回?@zmo代码补充道,但我相信这会让我的问题看起来复杂得多;代码没有问题,因为它可以通过有线方式工作,但当我使用无线时,它可以在任何serialport读取器上工作,例如(java、arduino…),而不是节点js serialport库。。如果我必须经历它,这将是暴力解决方案,这将需要我的时间。这就是为什么我发布了这个问题。我做了你刚刚想到的每件事,甚至在你提到它之前做了更多。我是一个开发者,我知道我在做什么。我碰巧在问题中说了这些,也许是一个明显的障碍挡住了你的视线?!当然,我不是在评判你的能力,但是作为其他开发者,你可能错过了一些非常明显的东西!特别是在基于arduino的项目中,除了那个让我发疯的愚蠢的小东西,我不知道我检查了多少次。我已经读了一遍(两遍)你写的所有东西,很明显你已经完成了我本应该做的和在调试阶段可能做的所有事情(不带逻辑分析仪——它可以是另一个arduino设备,以查看无线设备和FTDI芯片之间是否真的出现了问题,或者是否使用wireshark…).现在,我过去在使用基于射频的无线加密狗时遇到过一些问题,我在处理毫无意义的代码时遇到了一些倒退。我当时唯一的解决方案是从头开始重写新的最短代码,然后尝试围绕这些代码重新构建软件系统,试图避免这些倒退情况。你有没有想过你有没有用最少的设置让它以你想要的方式工作?(你没有告诉我你是否尝试过这样做-我的第三个问题)
sp.open(portName, portConfig, function (err) {
    console.log("ERROR opening serial port: ", err);
});