简体   繁体   English

C++ Boost::asio 与 Arduino 的串行通信无法写入

[英]C++ Boost::asio serial communcation with Arduino can't write

I tried to communicate with my Arduino Uno using the Boost Asio library.我尝试使用 Boost Asio 库与我的 Arduino Uno 进行通信。 Somehow I can't send data to my Arduino, and I have no idea what i'm doing wrong.不知何故,我无法将数据发送到我的 Arduino,而且我不知道我做错了什么。 Reading works fine, but writing only works when i open a terminal and say:阅读工作正常,但只有当我打开终端并说:

cat /dev/ttyACM0

When this terminal window is open, and I run my C++ application it works otherwise it doesn't work.当这个终端窗口打开时,我运行我的 C++ 应用程序,否则它不起作用。

Code of the test application (C++):测试应用程序代码(C++):

#include <iostream>
#include <boost/asio.hpp>

char* message;

int main()
{

    boost::asio::io_service ioservice;
    boost::asio::serial_port serial(ioservice, "/dev/ttyACM0");

    serial.set_option(boost::asio::serial_port_base::baud_rate(115200));
    serial.set_option(boost::asio::serial_port::flow_control(boost::asio::serial_port::flow_control::none));
    serial.set_option(boost::asio::serial_port::parity(boost::asio::serial_port::parity::none));
    serial.set_option(boost::asio::serial_port::stop_bits(boost::asio::serial_port::stop_bits::one));
    serial.set_option(boost::asio::serial_port::character_size(boost::asio::serial_port::character_size(8)));

    std::string s = "u";
    boost::asio::streambuf b;
    std::ostream os(&b);
    os << s;
    boost::asio::write(serial, b.data());

    if (serial.is_open()) {
        serial.close();
    }

    return 0;
}

Code of my Arduino application:我的 Arduino 应用程序代码:

#include "Servo.h"

Servo servo;
void setup() {
  Serial.begin(115200);
  servo.attach(9);
  servo.write(0);
}

void loop() {
  if(Serial.available()) {
    char c = Serial.read();
    if(c == 'u') {
      servo.write(180);
    } else if (c == 'v') {
      servo.write(0);
    }
  }
}

I tried this both on my Ubuntu 18.04 and Debian 10 installation to rule out a permission issue, so I think there is something wrong with my code.我在我的 Ubuntu 18.04 和 Debian 10 安装上都尝试了这个以排除权限问题,所以我认为我的代码有问题。

Update:更新:

I found the issue, the Arduino is restarting when making a serial connection.我发现了这个问题,Arduino 在进行串行连接时正在重新启动。 When I add a thread sleep for for example 5 seconds and after that resent the data it works (because then it keeps the serial connection alive).当我添加一个线程睡眠例如 5 秒,然后重新发送数据时,它可以工作(因为它使串行连接保持活动状态)。 I'm still looking for a permanent solution, so that I don't have to do a write before I really want to write something.我仍在寻找一个永久的解决方案,这样我就不必在真正想写东西之前写东西了。

Update 2:更新 2:

Apparently I don't even have to do a write, but where must be a small delay before I can start writing, because when after opening the port the Arduino is still restarting.显然我什至不必写,但是在我开始写之前必须有一个小的延迟,因为在打开端口后 Arduino 仍在重新启动。

I fixed it with adding a small delay before writing to the serial port.我在写入串行端口之前通过添加一个小的延迟来修复它。 As I also wrote in my comment above, the Arduino is restarting when you start a serial communication.正如我在上面的评论中所写的那样,当您开始串行通信时,Arduino 正在重新启动。

This can be disabled on several ways: https://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection/这可以通过多种方式禁用: https : //playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection/

Another option is to send a "ready" signal from the Arduino to know in your application that the Arduino is rebooted.另一种选择是从 Arduino 发送“就绪”信号,以在您的应用程序中了解 Arduino 已重新启动。 So then start in your application with reading, and when you received that message, you can start writing.因此,然后在您的应用程序中开始阅读,当您收到该消息时,您就可以开始写作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM