【问题标题】:Reading arduino serial in C++ using codeblocks on ubuntu在 ubuntu 上使用代码块在 C++ 中读取 arduino 串行
【发布时间】:2015-02-08 03:57:39
【问题描述】:

我尝试使用和编辑来自 Read and Write on serial port in Ubuntu with C/C++ and LibSerial 的代码,并从 Ubuntu 手册页 http://manpages.ubuntu.com/manpages/saucy/man3/LibSerial_SerialStream.3.html 中引用。

当我使用 Arduino IDE 中的串行监视器时,它工作正常。但是当我想用 C++ 的代码块来阅读它时,我得到的只是一些垃圾值。

代码如下:

#include <SerialStream.h>
#include <iostream>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#define PORT "/dev/ttyUSB0"

using namespace std;
using namespace LibSerial;
...
SerialStreamBuf::BaudRateEnum baud_rate = SerialStreamBuf::BAUD_115200;
const SerialStreamBuf::CharSizeEnum csize = SerialStreamBuf::DEFAULT_CHAR_SIZE;
const SerialStreamBuf::ParityEnum parity = SerialStreamBuf::PARITY_NONE;
short stop_bits = 1;
const SerialStreamBuf::FlowControlEnum flow_c = SerialStreamBuf::FLOW_CONTROL_NONE;
...
SerialStream(PORT, baud_rate, csize, parity, stop_bits, flow_c);
SerialStream serial_port ;
serial_port.Open(PORT) ;
...
while( 1  ){
     char next_byte;
     serial_port.get(next_byte);
     std::cerr << next_byte;
     usleep(100);
 }

我该如何解决这个问题?我不擅长面向对象编程,所以我不太确定初始化构造函数。

【问题讨论】:

    标签: c++ ubuntu serial-port arduino codeblocks


    【解决方案1】:

    您为串行流定义了两个变量,但使用了未初始化的变量。尝试替换这个:

    SerialStream(PORT, baud_rate, csize, parity, stop_bits, flow_c);
    SerialStream serial_port ;
    serial_port.Open(PORT) ;
    

    SerialPort serial_port ;
    serial_port.Open(PORT, baud_rate, csize, parity, stop_bits, flow_c);
    

    然后使用ReadByte函数。

    【讨论】:

    • 编译器告诉我“错误:在‘serial_port’中请求成员‘Open’,它是非类类型‘SerialPort()’”,但我只是用SerialPort的内容初始化了serial_port,对吧?为什么它给我这个错误?
    • 我已经解决了这个问题,应该是SerialPort serial_port;而不是SerialPort serial_port("/dev/ttyUSB0")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多