我正在尝试与带有传感器的arduino进行通信。
所以我有一个名为主板的对象,它有传感器,每个传感器都有可能有阈值和/或轮询的指标,其中有一个名为 getValue 的方法,该方法将数据发送到 arduino 并返回带有 promise 的数据。问题是,如果我异步传感器来获取它们的值,所有传感器都会得到相同的值。
我不知道为什么会发生这种情况。我只用 javascript 编程了 1 年,用 Angular 编程了 5 个月。我查了帖子async/await using serialport in node.js但我检查了我的代码,并执行了帖子中建议的操作。
通信方法位于服务内部。
有人可以帮忙吗?
tl;博士: 发送数据到arduino并在 promise 中获取数据。 指标 A 和 B 得到相同的 promise 。 组件轮询也得到了阈值的 promise 。 (指标有阈值和轮询)
我
通信.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/