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

Send a signed integer as a byte via serial in c++ and Arduino read it

本文关键字:整数 带符号 一个 字节 读取 Arduino c++      更新时间:2023-10-16

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

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

File > Examples > 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);
}