相关疑难解决方法(0)

通过串行连接进行双向C++通信

我正在尝试编写一个非常简单的C++应用程序来与Arduino进行通信.我想向Arduino发送一个它立即发回的角色.我从教程中获取的Arduino代码如下所示:

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    //Have the Arduino wait to receive input
    while (Serial.available()==0);

    //Read the input
    char val = Serial.read();

    //Echo
    Serial.println(val);
}
Run Code Online (Sandbox Code Playgroud)

我可以使用GNU屏幕轻松地与Arduino进行通信,因此我知道基本通信一切正常:

$ screen /dev/tty.usbmodem641 9600

我看到的(破碎的)C++代码如下所示:

#include <fstream>
#include <iostream>
int main()
{
    std::cout << "Opening fstream" << std::endl;
    std::fstream file("/dev/tty.usbmodem641");
    std::cout << "Sending integer" << std::endl;
    file << 5 << std::endl; // endl does flush, which may be important
    std::cout << "Data Sent" << std::endl;
    std::cout << "Awaiting response" << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ linux serial-port arduino

12
推荐指数
2
解决办法
3万
查看次数

标签 统计

arduino ×1

c++ ×1

linux ×1

serial-port ×1