node.js - 与串行端口的异步通信提供混合数据

标签 node.js angular serial-port

我正在尝试与带有传感器的arduino进行通信。

所以我有一个名为主板的对象,它有传感器,每个传感器都有可能有阈值和/或轮询的指标,其中有一个名为 getValue 的方法,该方法将数据发送到 arduino 并返回带有 promise 的数据。问题是,如果我异步传感器来获取它们的值,所有传感器都会得到相同的值。

我不知道为什么会发生这种情况。我只用 javascript 编程了 1 年,用 Angular 编程了 5 个月。我查了帖子async/await using serialport in node.js但我检查了我的代码,并执行了帖子中建议的操作。

通信方法位于服务内部。

有人可以帮忙吗?

tl;博士: 发送数据到arduino并在 promise 中获取数据。 指标 A 和 B 得到相同的 promise 。 组件轮询也得到了阈值的 promise 。 (指标有阈值和轮询)

data mixed

通信.service.ts

communicate(cmd: string, serialPort: SerialPort, expectedResponse: string, notExpectedResponse){
    const parser = serialPort.pipe(new Delimiter({delimiter: '\n'}));
    return new Promise((resolve, reject) => {
      serialPort.write(cmd, () => {
        console.log('message written');
        parser.on('data', data => {
          const dataString = data.toString();
          if (dataString != null && dataString.includes(expectedResponse)) {
            let responseRemoved = dataString.replace(expectedResponse + ' ', '');
            resolve(responseRemoved);
          } else {
            let response;
            if (dataString != null && dataString.includes(notExpectedResponse)) {
              response = dataString.replace(notExpectedResponse + ' ', '');
            }
            reject(response);
          }
        });
        setTimeout(() => {
          reject('');
        }, this.timeOutTime);
      });
    });
}

阈值.组件.ts

 private getValuesThreshold(): void{
    console.log(this.metricId);
    this.motherboardInUse.getValues(this.metricId, GlobalVariableCMD.GET_THRESHOLD_VALUES,
      GlobalVariableResponse.GET_THRESHOLD_VALUES, GlobalVariableResponse.GET_THRESHOLD_VALUES).then(data => {
      let dataString = data.toString();
      if(dataString){
        console.log(dataString);
        let responseSplit = dataString.toString().split(' ');
        let minimumValue = parseInt(responseSplit[1]);
        let maximumValue = parseInt(responseSplit[2]);
        minimumValue < this.floor ? this.minimumThreshold = this.floor : this.minimumThreshold = minimumValue;
        maximumValue > this.ceil ? this.maximumThreshold = this.ceil : this.maximumThreshold = 90;
        this.enabled = responseSplit[0].includes('1');
        console.log(this.minimumThreshold);
        console.log(this.maximumThreshold);
        console.log(this.enabled);
      }
    }).catch(err => {
      let errString = err.toString();
      if(errString){
        console.log(errString);
      }
    });
  }

主板.组件.ts

getValuesThreshold(metricId: string, ATcmd: string, expectedResponse: string, notExpectedResponse: string) {
    let command = this.communicateBuilder.BuildCommandGetMetricValue(ATcmd, this.usedSensorId, metricId);
    console.log('motherboard get values' + command);
    let responseOk = this.commandBuilderService.respondsSuccess(expectedResponse);
    let responseNotOk = this.commandBuilderService.respondsFail(notExpectedResponse);
    return this.communicateService.communicate(command, this.motherboard.serialPort, responseOk, responseNotOk);
  }


最佳答案

也许你可以试试这个

async getValuesThreshold(metricId: string, ATcmd: string, expectedResponse: string, notExpectedResponse: string) {
    let command = this.communicateBuilder.BuildCommandGetMetricValue(ATcmd, this.usedSensorId, metricId);
    console.log('motherboard get values' + command);
    let responseOk = this.commandBuilderService.respondsSuccess(expectedResponse);
    let responseNotOk = this.commandBuilderService.respondsFail(notExpectedResponse);
    return await this.communicateService.communicate(command, this.motherboard.serialPort, responseOk, responseNotOk);
  }

关于node.js - 与串行端口的异步通信提供混合数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59922538/

相关文章:

javascript - 使用youtube API和node.js添加youtube评论

typescript - 访问父组件中的属性

c++ - Qt调试报错: "while parsing target library list: not well-formed (invalid token)"

javascript - 为什么 webpack 仍然生成到旧目录 bundle 文件?

node.js - 如何在 proxy.conf.json 或 proxy.conf.js 中动态定义端口

angular - : 'clr-icon' is not a known element: 中的错误

Angular 5 - 通过 ngModel 从子模板组件向父组件发送错误

c - RS 232 DB9 引脚切换代码的​​问题

C串口程序使用键盘和显示器代替串口/dev/ttyUSBX

javascript - 如何重定向到另一个页面并在reactjs中使用props