我从连接到 esp8266(Nodemcu v0.9)的 Nextion 返回的数据有问题。我正在以 9600 b/s 的速度与 Nextion 通信。
我有代码可以解析通过 IDE 的串行控制台输入的命令,并将适当的命令发送到 Nextion。这里的相关命令是“getn xxxx”,它要求从 Nextion 获取数值。
例如,“getn 123”发送一个“get 123”,期望得到一个数字结果。(有一个 'gets 123' 发送一个 'get "123"' 但这里没有使用)
当我发送一个“getn 123”后跟一个“getn 222”时,这就是我得到的
**ESP8266**
//1st getn command. Note only 7 bytes come back and missing 3rd 0xFF in returned data
Processing command: getn 123
Sending [get 123]
found 7 bytes
71 7b 00 00 00 ff ff
//2nd getn command. Note additional leading 0xFF and missing 3rd 0xFF in returned data
Processing command: getn 222
Sending [get 222]
found 8 bytes
ff 71 de 00 00 00 ff ff
If I connect an Arduino Nano instead of the esp8266, the same code works perfectly!
**nano** (I only execute one 'getn 123' since it works ok
Processing command: getn 123
Sending [get 123]
found 8 bytes
71 7b 00 00 00 ff ff ff
请参阅下面的代码清单。
#include <SoftwareSerial.h>
#define SERIAL_TIMEOUT 3000
#ifdef ESP8266
// ESP8266
#define MY_RX 5 // NEXTION TX, esp8266 D1
#define MY_TX 4 // NEXTION RX, esp8266 D2
#else
// arduino, nano and similar
#define MY_RX 11 // NEXTION TX,
#define MY_TX 10 // NEXTION RX,
#endif
SoftwareSerial mySerial(MY_RX, MY_TX);
char serOutBuf[128];
char cmdBuf[64] = { 0 };
void setup() {
Serial.begin(115200);
while(Serial.available()) {
Serial.read();
}
// Nextion comms are at 9600 b/s
mySerial.begin(9600);
Serial.println("Ready");
}
void loop() {
int charsRead = 0;
cmdBuf[0] = 0;
if (Serial.available()) {
charsRead = Serial.readBytesUntil('\n', cmdBuf, sizeof(cmdBuf) - 1);
cmdBuf[charsRead] = 0;
processCmd(cmdBuf);
}
}
void processCmd(char *cmdBuf) {
char *p = cmdBuf;
char tmpBuf[128] = { 0 };
do { // dummy loop that allows multiple exits to a single point
Serial.print("Processing command: ");Serial.println(cmdBuf);
if (strncmp(cmdBuf, "send ", 5) == 0) {
p = cmdBuf + 5; // skip 'send '
while (*p == ' ') { // skip leading spaces
p++;
}
sendCommand(p);
break;
}
// get numeric value from Nextion. There is a 'gets' too but not shown here
if (strncmp(cmdBuf, "getn ", 5) == 0) {
p = cmdBuf + 5;
while (*p == ' ') {
p++;
}
int n = 0;
sprintf(tmpBuf, "get %s", p); // build command to send
bool ret = getNumber(&n, tmpBuf); // get number value from Nextion
// the next 2 lines cater for the fact the Nano does not support Serial.printf()
sprintf(serOutBuf, "result %d = %d\n", ret, n);
Serial.print(serOutBuf);
break;
}
} while (false); // dummy loop
}
boolean sendCommand(char *cmd) {
sprintf(serOutBuf, "Sending [%s]\n", cmd);
Serial.print(serOutBuf);
mySerial.print(cmd);
mySerial.write(0xFF);
mySerial.write(0xFF);
mySerial.write(0xFF);
delay(20);
return true;
}
bool getNumber(int *n, char *cmd) {
bool res = false;
Serial.print(serOutBuf);
if (n) {
sendCommand(cmd);
byte buf[8];
int l = mySerial.readBytes(buf, sizeof(buf));
sprintf(serOutBuf, "found %d bytes\n", l);
Serial.print(serOutBuf);
for (int i = 0; i < l; i++) {
sprintf(serOutBuf, "%02x ", (byte)buf[i]);
Serial.print(serOutBuf);
}
Serial.println("");
if (buf[0] == 0x71 && buf[5] == 0xff && buf[6] == 0xff && buf[7] == 0xff) {
*n = ((uint32_t)buf[4] << 24) | ((uint32_t)buf[3] << 16) | ((uint32_t)buf[2] << 8) | buf[1];
res = true;
}
}
return res;
}
任何有关上述内容的帮助将不胜感激!