0

我正在尝试使用 BoostAsio 与我的 arduino throw Serial in c++ 进行通信。我发现这段代码可以在 Visual Studio 中编译,但无法在 linux-ubuntu 上编译。

我编译时不使用任何标志。这可能是问题吗?

#include <iostream>
#include <boost/asio.hpp> 
using namespace::boost::asio;  
using namespace std;

#define PORT_LINUX "/dev/ttyUSB0"
#define PORT_WINDOW "COM7"


#define SECURITY_CAMERA 1
#define FIRE_SENSOR 2
#define VOICE_SENSOR 3
#define DISTANCE_SENSOR 4


// Base serial settings
serial_port_base::baud_rate BAUD(9600);
serial_port_base::flow_control FLOW( serial_port_base::flow_control::none );
serial_port_base::parity PARITY( serial_port_base::parity::none );
serial_port_base::stop_bits STOP( serial_port_base::stop_bits::one );
serial_port_base::character_size CSIZE(serial_port_base::character_size::character_size(8U));


//serial_port_base::stop_bits STOP( serial_port_base::stop_bits::one );

int main(){
  io_service io;
  serial_port port( io, PORT_LINUX );

  // Setup port - base settings
  port.set_option( BAUD );
  port.set_option( FLOW );
  port.set_option( PARITY );
  port.set_option( STOP );
  port.set_option( CSIZE );

  char opcode,priority;
  int num;

  while(1){
    num = read(port,buffer(&opcode,1));
    num = read(port,buffer(&priority,1));
    cout << opcode<<" "<<priority<<endl;
  }

  return 0;
}

编译器错误:

error: expected unqualified-id before numeric constant
In file included from /usr/include/boost/asio/serial_port_service.hpp:25:0,
                 from /usr/include/boost/asio/basic_serial_port.hpp:30,
                 from /usr/include/boost/asio.hpp:25,
                 from main.cpp:2:
/usr/include/boost/asio/detail/reactive_serial_port_service.hpp: In instantiation
  of ‘static boost::system::error_code 
  boost::asio::detail::reactive_serial_port_service::store_option(const void*,
  termios&, boost::system::error_code&) [with SettableSerialPortOption = int]’:
/usr/include/boost/asio/detail/reactive_serial_port_service.hpp:126:20:
  required from ‘boost::system::error_code  
  boost::asio::detail::reactive_serial_port_service::set_option(
  boost::asio::detail::reactive_serial_port_service::implementation_type&,
  const SettableSerialPortOption&, boost::system::error_code&) [with 
  SettableSerialPortOption = int; 
  boost::asio::detail::reactive_serial_port_service::implementation_type = 
  boost::asio::detail::reactive_descriptor_service::implementation_type]’
/usr/include/boost/asio/serial_port_service.hpp:167:53:   required from 
  ‘boost::system::error_code 
  boost::asio::serial_port_service::set_option(
  boost::asio::serial_port_service::implementation_type&, const 
  SettableSerialPortOption&, boost::system::error_code&) [with 
  SettableSerialPortOption = int;
  boost::asio::serial_port_service::implementation_type = 
  boost::asio::detail::reactive_descriptor_service::implementation_type]’
/usr/include/boost/asio/basic_serial_port.hpp:390:5:   required from ‘void 
  boost::asio::basic_serial_port<SerialPortService>::set_option(const 
  SettableSerialPortOption&) [with SettableSerialPortOption = int;
  SerialPortService = boost::asio::serial_port_service]’
main.cpp:35:26:   required from here
/usr/include/boost/asio/detail/reactive_serial_port_service.hpp:194:20:  error:
  request for member ‘store’ in ‘*(const int*)option’, which is of non-class type
  ‘const int’
4

1 回答 1

1

由于与CSIZE其他地方定义的宏发生冲突,编译失败。为变量提供不同的标识符serial_port_base::character_size CSIZE,例如CHAR_SIZE在两个位置,它应该编译。

编译器错误“expected unqualified-id before numeric constant”暗示CSIZE正在作为数字常量处理。有了这个提示,我用g++ -E -dD -dI -P标志调用编译器来执行源代码的预处理,保留宏和包含指令。输出表明在我的系统上,bits/termios.h#define CSIZE 0000060.

于 2013-07-17T13:30:19.363 回答