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

标签 c++ arduino serial-port

我可以通过串口发送一个字符串,但是 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/

相关文章:

c++ - C++ 放置删除在内部如何工作(C++ 运行时)?如何克服它的局限性?

python - 在 Python 中通过 USB 转换器进行串行通信 - 如何解决这个问题?

c++ - 在 Qt Creator 中禁用编译器警告项目范围(使用 MSVC 时)

c++ - 套接字发送错误

c++ - 如何在 Arduino 上格式化长加千位分隔符

c++ - 来自 Arduino 库的包含文件冲突

Select()可以用来检测和识别多个流输入

c# - 适用于 .Net/C# 的 GPS 驱动程序,用于从任何支持 NMEA 的 GPS 装置中提取 GPS 数据

c++ - 查找 Internet Explorer_Server 的链接元素并发送 Click() 事件 (C++)

c++ - 将字符串发送到 Arduino 的最佳方式?