首页 文章

通过c中的串行发送有符号整数作为字节,Arduino读取它

提问于
浏览
2

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

1 回答

  • 0

    如果打开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);
    }
    

相关问题