串行端口配置使用boost ASIO [英] serial port configuration using boost ASIO

查看:325
本文介绍了串行端口配置使用boost ASIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下代码给出错误


serial_config.cpp:13:error:expected
之前的未定义标识$ b $在
/home/chirag/boost_install/include/boost/asio/serial_port_service.hpp:25中包含$来自/home/chirag/boost_install/include/boost/asio/basic_serial_port.hpp30的b $ b,来自/home/chirag/boost_install/include/boost/asio.hpp:25的

serial_config.cpp:1:/home/chirag/boost_install/include/boost/asio/detail/reactive_serial_port_service.hpp:
在静态成员函数âstaticboost :: 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:
实例化d从â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:
从âboost实例化:: system :: error_code
boost :: asio :: serial_port_service :: set_option(boost :: asio :: detail :: reactive_descriptor_service :: implementation_type&
const SettableSerialPortOption& amp; boost :: system :: error_code&)[with
SettableSerialPortOption = int]â
/home/chirag/boost_install/include/boost/asio/basic_serial_port.hpp:390:
从“
实例化boost: :asio :: basic_serial_port :: set_option(const
SettableSerialPortOption&)[with SettableSerialPortOption = int,
SerialPortService = boost :: asio :: serial_port_service]â
serial_config.cpp:31:从这里实例化
/home/chirag/boost_install/include/boost/asio/detail/reactive_serial_port_service.hpp:194:
错误:请求成员â*(const int *)optionâ,这是
的非类类型âconstintâ




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

//这些是我们端口需要连接的值
#ifdef _WIN32
// windows使用com端口,这取决于您的电缆插入的COM端口。
const char * PORT =COM3;
#else
// * nix com ports
const char * PORT =dev / ttyS3;
#endif
//注意:除了BAUD之外的所有以下内容与默认值完全相同

//我们在
serial_port_base上传达什么波特率: :baud_rate BAUD(19200);
//每个数据包数据有多大(默认为8位)
serial_port_base :: character_size CSIZE(8);
//使用什么流控制(默认为none)
serial_port_base :: flow_control FLOW(serial_port_base :: flow_control :: none);
//使用什么奇偶校验(默认为none)
serial_port_base :: parity PARITY(serial_port_base :: parity :: none);
//使用了多少个停止位(默认为一个)
serial_port_base :: stop_bits STOP(serial_port_base :: stop_bits :: one);

int main()
{
//创建与串行设备对话的I / O服务
io_service io;
//创建串行设备,注意它需要io服务和端口名称
serial_port port(io,PORT);

//通过并设置我们需要的所有选项
//所有这些都被列出,但默认值适用于大多数情况
port.set_option(BAUD );
port.set_option(CSIZE);
port.set_option(FLOW);
port.set_option(PARITY);
port.set_option(STOP);

//存储命令的缓冲区
//此设备读取8位,表示无符号字符,如指令
//因设备而异,请先检查手册
unsigned char command [1] = {0};

//读取用户值发送到设备
int input;
cin>>输入;

//简单循环,因为唯一的好的值是[0,255]
//输入负数时中断。
//演员将把太大的数字转换成范围。
while(input> = 0)
{
//将我们的读入数转换为目标数据类型
command [0] = static_cast< unsigned char>(input) ;

//这是通过电线发送实际位的命令
//注意它需要流和asio ::缓冲区
//流是我们的serial_port
//使用我们的命令缓冲区构造缓冲区,
//要发送的指令数量
write(port,buffer(command,1));

//读入下一个输入值
cin>>输入;
}

//所有完成发送命令
返回0;
}


解决方案

如果更改character_size变量名从CSIZE到C_SIZE,那么它将工作.....


I am configuring serial port using boost asio. but why below code is giving error? If I comment set_option it works fine.

below code gives error

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

解决方案

If you change character_size variable name to C_SIZE from CSIZE then it shall work.....

这篇关于串行端口配置使用boost ASIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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