通过串行连接进行双向 C++ 通信 [英] Two-way C++ communication over serial connection

查看:12
本文介绍了通过串行连接进行双向 C++ 通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a really simple C++ application to communicate with an Arduino. I would like to send the Arduino a character that it sends back immediately. The Arduino code that I took from a tutorial looks like this:

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

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

I can communicate with the Arduino easily using GNU screen, so I know that everything is working fine with the basic communication:

$屏幕/dev/tty.usbmodem641 9600

$ screen /dev/tty.usbmodem641 9600

我拥有的(损坏的)C++ 代码如下所示:

The (broken) C++ code that I have looks like this:

#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;
    std::string response;
    file >> response;
    std::cout << "Response: " << response << std::endl;

    return 0;
}

它编译得很好,但是在运行它时,Arduino 上的一些灯会闪烁,并且终端只是挂在:

It compiles fine, but when running it, some lights flash on the Arduino and the terminal just hangs at:

打开fstream

我哪里错了?

推荐答案

分三点:

首先:您不会在 Linux 端初始化串行端口 (TTY).没有人知道它处于什么状态.

First: You don't initialize the serial port (TTY) on the Linux side. Nobody knows in what state it is.

在您的程序中执行此操作必须使用 tcgetattr(3)tcsetattr(3).您可以在此站点、Arduino 站点或 Google 上使用这些关键字找到所需的参数.但是只是为了快速测试我建议在你调用你自己的命令之前发出这个命令:

Doing this in your program you must use tcgetattr(3) and tcsetattr(3). You can find the required parameters by using these keywords at this site, the Arduino site or on Google. But just for quick testing I propose to issue this command before you call your own command:

stty -F /dev/tty.usbmodem641 sane raw pass8 -echo -hupcl clocal 9600

尤其是缺少的 clocal 可能会阻止您打开 TTY.

Especially the the missing clocal might prevent you opening the TTY.

第二:当设备打开时,你应该稍等片刻再发送任何东西.默认情况下,当串行线打开或关闭时,Arduino 会重置.您必须考虑到这一点.

Second: When the device is open, you should wait a little before sending anything. By default the Arduino resets when the serial line is opened or closed. You have to take this into account.

-hupcl 部分将在大多数情况下阻止此重置.但是至少需要一次复位,因为-hupcl只有在TTY已经打开并且Arduino已经接收到复位信号时才能设置.所以 -hupcl 将仅"阻止未来的重置.

The -hupcl part will prevent this reset most of the time. But at least one reset is always necessary, because -hupcl can be set only when the TTY is already open and at that time the Arduino has received the reset signal already. So -hupcl will "only" prevent future resets.

第三:您的代码中有NO错误处理.请在 TTY 上的每个 IO 操作之后添加代码,以检查错误并 - 最重要的部分 - 使用 perror(3) 或类似函数打印有用的错误消息.

Third: There is NO error handling in your code. Please add code after each IO operation on the TTY which checks for errors and - the most important part - prints helpful error messages using perror(3) or similar functions.

这篇关于通过串行连接进行双向 C++ 通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆