如何解决使用 SerialTransfer 库通过 UART 接收从 nodemcu 发送到 Arduino UNO 的有效负载中的所有零
我使用 SerialTransfer.h
通过 UART 将 nodemcu 主控流传感器值传输到 Arduino Uno 从站。我已经使用 SoftwareSerial.h
在 Arduino 数字引脚 2、3 上为 Rx、Tx 设置了一个额外的串行端口。我已将 nodemcu 上的 Tx 连接到 Uno 上的 Rx,将 nodemcu 上的 Rx 连接到 Uno 上的 Tx。我有一个电平转换器来调整 3.3 V nodemcu 和 5 V Arduino。我确保提供一个共同点。
我从 nodemcu 传输一个带有传感器值(bool 和 int 类型,硬编码用于演示)的结构,但在 Arduino 上只接收零值,如串行监视器所示。我的代码如下。我很感激任何输入。
我已经尝试了以下没有区别。
- 在使用
SoftwareSerial.h
创建的 Uno 上有或没有额外的串行端口 - 使用 Arduino Uno 主站和 nodemcu 从站反转设置
- nodemcu Tx 和 Arduino Uno Rx 上有和没有电平转换器
这里是 nodemcu master 的代码。
#include <Wire.h>
#include <SerialTransfer.h>
SerialTransfer masterMCU;
struct PAYMASTER {
/*
water: instruction to switch pump on or off. Note the float sensor in pump's circuit will prevent overflow.
fan: instruction to control fan speed - LO,MED,HIGH. Note PC fan requires an int between 0 and 255.
led: instruction to control LED brightness. Note that the FastLED library requires an int between 0 and 255.
*/
bool water;
int fan;
int led;
} instructions = {
true,201,60
};
void setup() {
// put your setup code here,to run once:
Serial.begin(9600);
delay(999);
masterMCU.begin(Serial);
delay(999);
}
void debug() {
Serial.print("MASTER: ");
Serial.print(millis());
Serial.print(" Water: ");
Serial.print(instructions.water);
Serial.print(",Fan: ");
Serial.print(instructions.fan);
Serial.print(",LED: ");
Serial.println(instructions.led);
}
void loop() {
// put your main code here,to run repeatedly:
masterMCU.txObj(instructions,sizeof(instructions));
masterMCU.sendData(sizeof(instructions));
debug();
delay(999);
}
这是Arduino Uno slave的代码。
#include <Wire.h>
#include <SerialTransfer.h>
#include <SoftwareSerial.h>
SerialTransfer slaveMCU;
SoftwareSerial extra(2,3); // Rx 2,Tx 3
struct PAYMASTER {
/*
water: instruction to switch pump on or off. Note the float sensor in pump's circuit will prevent overflow.
fan: instruction to control fan speed - LO,HIGH. Note PC fan requires an int between 0 and 255.
led: instruction to control LED brightness. Note that the FastLED library requires an int between 0 and 255.
*/
bool water;
int fan;
int led;
} instructions;
void setup() {
// put your setup code here,to run once:
Serial.begin(9600);
delay(201);
extra.begin(9600);
delay(201);
slaveMCU.begin(extra);
delay(201);
}
void debug() {
Serial.print("SLAVE: ");
Serial.print(millis());
Serial.print(" Water: ");
Serial.print((bool)instructions.water);
Serial.print(",to run repeatedly:
if (slaveMCU.available()) {
slaveMCU.rxObj(instructions,sizeof(instructions));
debug();
} else if (slaveMCU.status < 0) {
Serial.print("ERROR: ");
if(slaveMCU.status == -1)
Serial.println(F("CRC_ERROR"));
else if(slaveMCU.status == -2)
Serial.println(F("PAYLOAD_ERROR"));
else if(slaveMCU.status == -3)
Serial.println(F("STOP_BYTE_ERROR"));
}
delay(999);
}
解决方法
我进行了一些更改,现在收到的数据具有正确的值。
- 我在 master 中用
delay()
替换了millis()
。 - 我在 master 中用
SerialTransfer::sendData()
替换了SerialTransfer::sendDatum()
。前者用于流式传输多个对象,而后者用于流式传输单个对象。 - 我将结构体中的
int
类型替换为uint8_t
,该结构体通过主从设备的线路发送。
现在 Arduino Uno 可以正确接收这些值。在上述 3. 之前,所有更改都没有任何区别。我保留了其他更改,因为它们对结果也很重要。这是用于从 nodemcu master 到 Arduino Uno slave 正确传输和接收对象的最终代码。
nodemcu 大师:
#include <Wire.h>
#include <SerialTransfer.h>
SerialTransfer masterMCU;
unsigned long tic = millis();
unsigned long toc = tic;
#define DELTA 1000
struct PAYMASTER {
/*
water: instruction to switch pump on or off. Note the float sensor in pump's circuit will prevent overflow.
fan: instruction to control fan speed - LO,MED,HIGH. Note PC fan requires an int between 0 and 255.
led: instruction to control LED brightness. Note that the FastLED library requires an int between 0 and 255.
*/
bool water;
uint8_t fan;
uint8_t led;
} instructions = {
true,201,60
};
void setup() {
// put your setup code here,to run once:
Serial.begin(9600);
delay(999);
masterMCU.begin(Serial);
delay(999);
}
void debug() {
Serial.print("MASTER: ");
Serial.print(millis());
Serial.print(" Water: ");
Serial.print(instructions.water);
Serial.print(",Fan: ");
Serial.print(instructions.fan);
Serial.print(",LED: ");
Serial.println(instructions.led);
}
void loop() {
// put your main code here,to run repeatedly:
toc = millis();
if ((toc - tic) > DELTA) {
masterMCU.txObj(instructions,sizeof(instructions));
masterMCU.sendDatum(instructions),sizeof(instructions);
debug();
tic = toc;
}
}
Arduino Uno slave:
#include <Wire.h>
#include <SerialTransfer.h>
#include <SoftwareSerial.h>
SerialTransfer slaveMCU;
SoftwareSerial Extra(2,3); // Rx: 2,Tx: 3
unsigned long tic = millis();
unsigned long toc = tic;
struct PAYMASTER {
/*
water: instruction to switch pump on or off. Note the float sensor in pump's circuit will prevent overflow.
fan: instruction to control fan speed - LO,HIGH. Note PC fan requires an int between 0 and 255.
led: instruction to control LED brightness. Note that the FastLED library requires an int between 0 and 255.
*/
bool water;
uint8_t fan;
uint8_t led;
} instructions;
void setup() {
// put your setup code here,to run once:
Serial.begin(9600);
delay(201);
Extra.begin(9600);
delay(201);
slaveMCU.begin(Extra);
delay(201);
}
void debug() {
Serial.print("SLAVE: ");
Serial.print(millis());
Serial.print(" Water: ");
Serial.print((bool)instructions.water);
Serial.print(",to run repeatedly:
if (slaveMCU.available()) {
slaveMCU.rxObj(instructions);
debug();
} else if (slaveMCU.status < 0) {
Serial.print("ERROR: ");
if(slaveMCU.status == -1)
Serial.println(F("CRC_ERROR"));
else if(slaveMCU.status == -2)
Serial.println(F("PAYLOAD_ERROR"));
else if(slaveMCU.status == -3)
Serial.println(F("STOP_BYTE_ERROR"));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。