gpt4 book ai didi

c++ - 在 C++ 中通过串行发送一个有符号整数作为字节,Arduino 读取它

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:46:37 27 4
gpt4 key购买 nike

我可以通过串口发送一个字符串,但是 Arduino 读取字符串的速度非常慢。因此,我在 Arduino 中使用读取字节,但问题是我不知道如何在 C++ 中通过串行端口将数字 (<256) 作为字节发送。

最佳答案

如果您打开 Arduino IDE,在菜单下,查看:

文件 > 示例 > 08.Strings > CharacterAnalysis

我认为您会在这里找到非常接近您所寻找的东西。总而言之,它打开一个串行连接(USB)并从计算机读取输入。 (您需要确保匹配波特率并使用“发送”按钮。)这取决于您是否根据需要对 Arduino 进行编程。在示例中,它只是将反馈发送回串行监视器。自己运行它,看看会发生什么:)

示例中的 [MCVE] 片段:

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// send an intro:
Serial.println("send any byte and I'll tell you everything I can about it");
Serial.println();
}

void loop() {
// get any incoming bytes:
if (Serial.available() > 0) {
int thisChar = Serial.read();

// say what was sent:
Serial.print("You sent me: \'");
Serial.write(thisChar);
}

关于c++ - 在 C++ 中通过串行发送一个有符号整数作为字节,Arduino 读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37041961/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com