linux - 使用 boost ASIO 配置串口

标签 linux embedded serial-port boost-asio

我正在使用 boost asio 配置串口。 但是为什么下面的代码会出错? 如果我评论 set_option 它工作正常。

下面的代码给出了错误

serial_config.cpp:13: error: expected unqualified-id before numeric constant In file included from /home/chirag/boost_install/include/boost/asio/serial_port_service.hpp:25, from /home/chirag/boost_install/include/boost/asio/basic_serial_port.hpp:30, from /home/chirag/boost_install/include/boost/asio.hpp:25, from serial_config.cpp:1: /home/chirag/boost_install/include/boost/asio/detail/reactive_serial_port_service.hpp: In static member function âstatic boost::system::error_code boost::asio::detail::reactive_serial_port_service::store_option(const void*, termios&, boost::system::error_code&) [with SettableSerialPortOption = int]â: /home/chirag/boost_install/include/boost/asio/detail/reactive_serial_port_service.hpp:126: instantiated from âboost::system::error_code boost::asio::detail::reactive_serial_port_service::set_option(boost::asio::detail::reactive_descriptor_service::implementation_type&, const SettableSerialPortOption&, boost::system::error_code&) [with SettableSerialPortOption = int]â /home/chirag/boost_install/include/boost/asio/serial_port_service.hpp:167: instantiated from âboost::system::error_code boost::asio::serial_port_service::set_option(boost::asio::detail::reactive_descriptor_service::implementation_type&, const SettableSerialPortOption&, boost::system::error_code&) [with SettableSerialPortOption = int]â /home/chirag/boost_install/include/boost/asio/basic_serial_port.hpp:390: instantiated from âvoid boost::asio::basic_serial_port::set_option(const SettableSerialPortOption&) [with SettableSerialPortOption = int, SerialPortService = boost::asio::serial_port_service]â serial_config.cpp:31: instantiated from here /home/chirag/boost_install/include/boost/asio/detail/reactive_serial_port_service.hpp:194: error: request for member âstoreâ in â*(const int*)optionâ, which is of non-class type âconst intâ

#include <boost/asio.hpp> // include boost
using namespace::boost::asio;  // save tons of typing
#include <iostream>
using std::cin;

// These are the values our port needs to connect
#ifdef _WIN32
// windows uses com ports, this depends on what com port your cable is plugged in to.
    const char *PORT = "COM3";
#else
// *nix com ports
    const char *PORT = "dev/ttyS3";
#endif
// Note: all the following except BAUD are the exact same as the default values

// what baud rate do we communicate at
serial_port_base::baud_rate BAUD(19200);
// how big is each "packet" of data (default is 8 bits)
serial_port_base::character_size CSIZE( 8 );
// what flow control is used (default is none)
serial_port_base::flow_control FLOW( serial_port_base::flow_control::none );
// what parity is used (default is none)
serial_port_base::parity PARITY( serial_port_base::parity::none );
// how many stop bits are used (default is one)
serial_port_base::stop_bits STOP( serial_port_base::stop_bits::one );

int main()
{
    // create the I/O service that talks to the serial device
    io_service io;
    // create the serial device, note it takes the io service and the port name
    serial_port port( io, PORT );

    // go through and set all the options as we need them
    // all of them are listed, but the default values work for most cases
    port.set_option( BAUD );
    port.set_option( CSIZE );
    port.set_option( FLOW );
    port.set_option( PARITY );
    port.set_option( STOP );

    // buffer to store commands
    // this device reads 8 bits, meaning an unsigned char, as instructions
    // varies with the device, check the manual first
    unsigned char command[1] = {0};

    // read in user value to be sent to device
    int input;
    cin >> input;

    // Simple loop, since the only good values are [0,255]
    //  break when a negative number is entered.
    // The cast will convert too big numbers into range.
    while( input >= 0 )
    {
        // convert our read in number into the target data type
        command[0] = static_cast<unsigned char>( input );

        // this is the command that sends the actual bits over the wire
        // note it takes a stream and a asio::buffer
        // the stream is our serial_port
        // the buffer is constructed using our command buffer and
        //  the number of instructions to send
        write( port, buffer( command, 1 ) );

        // read in the next input value
        cin >> input;
    }

    // all done sending commands
    return 0;
}

最佳答案

如果您将 character_size 变量名从 CSIZE 更改为 C_SIZE,那么它将起作用......

关于linux - 使用 boost ASIO 配置串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12641149/

相关文章:

linux - 显示不同行为的 Shell 命令..在终端与运行脚本中

linux - 阻塞和非阻塞 I/O

linux - unix - 如果同时执行,则 shell 脚本不会以正确的顺序执行

io - 通过无竞争条件的缓冲区将数据写入 SD 卡

c - 如何将数字转换为Q格式

linux - 有没有复制更新替代命令?

objective-c - 在 Xcode 中,如何从不断更新的文本文件中读取数据?

python - PySerial 不与 Arduino 交谈

linux - OpenSSL 1.0.0l 配置脚本中没有 fips 和 --with-fipsdir 参数

c - C语言串口: read non-canonical mode