【发布时间】:2020-04-20 07:52:18
【问题描述】:
我正在使用
Serial2.begin(9600, SERIAL_8N1, 15, 14,true);
Serial2.write(b,100);
从 esp32 将缓冲区写入串行
但由于某种原因它没有传输完整的缓冲区? 而是一次传输一个字节。
有人对我如何传输完整缓冲区而不是一次传输 1 个字节有建议吗?
代码示例:
#include <HardwareSerial.h>
void setup() {
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8N1, 15, 14,true);
}
uint8_t buf[1000];
uint8_t* genBuffer(const char* txt, int* bufSZ)
{
int index = 0;
int i = 0;
buf[index++] = 0x00;
buf[index++] = 0x00;
buf[index++] = 0x00;
buf[index++] = 0x00;
buf[index++] = 0x00;
buf[index++] = 0x01;
buf[index++] = 0x46;
buf[index++] = 0x46;
buf[index++] = 0x30;
buf[index++] = 0x30;
buf[index++] = 0x02;
buf[index++] = 0x41;
buf[index++] = 0x41;
buf[index++] = 0x43;
buf[index++] = 0x32;
for (i = 0; txt[i]; i++) {
buf[index++] = txt[i];
}
*bufSZ = index;
return buf;
}
void loop()
{
int sz = 0;
uint8_t* b = genBuffer("test", &sz);
Serial.println(sz);
Serial2.write(b,sz);
for (int i = 0; i < sz; i++) {
Serial.print(" ");
Serial.print(b[i], HEX);
}
Serial.println();
delay(5000);
// put your main code here, to run repeatedly:
}
【问题讨论】:
-
你的缓冲区是多少?这是 C++ 顺便说一句,不是 C
-
UART 设置的波特率是多少,您期望的传输速度是多少? “一次一个字节”是你发送缓冲区的方式,当然......
-
00 00 00 00 00 01 46 46 30 30 02 41 41 43 32 30 37 46 32 30 30 36 30 31 30....@Piglet
-
请提供minimal reproducible example,而不是两行代码
-
9600baud 我的意思是在串行监视器/总线狗中,它一次显示 1 个长度为 1 的字节。使用 USB/串行适配器,此缓冲区显示同一传输中的所有字节,长度等。60 @unwind
标签: arduino serial-port esp8266 esp32 arduino-c++