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

Async communicating with serialport gives mixed data

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

所以我有一个名为 motherboard 的对象,它有传感器,每个传感器都有可能有阈值的度量 and/or 轮询,它有一个名为 getValue 的方法,该方法将数据发送到 arduino 和 returns 数据一个承诺。问题是,如果我异步传感器以获取它们的值,则所有传感器都将获得相同的值。

我不知道为什么会这样。我只用 javascript 编程了 1 年,用 angular 编程了 5 个月。我检查了 post async/await using serialport in node.js 但我检查了我的代码并且我做了 post.

中建议的事情

通信方法在服务内部。

有人能帮忙吗?

tl;博士: 向 arduino 发送数据以承诺取回数据。 指标 A 和 B 得到相同的承诺。 组件轮询也得到了阈值的承诺。 (指标有阈值和轮询)

communication.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);
      });
    });
}

threshold.component.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);
      }
    });
  }

motherboard.component.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);
  }

问题是使用串行端口发送异步数据。 Serialport 节点无法知道什么响应与发送的数据相关联。 所以它根据收到的第一个响应采取行动并将其返回。 解决这个问题的唯一方法是让他们同步。