2013-07-16 69 views
0

與串行通信與我的arduino拋出與BoostAsio的c + +串行。我發現這個代碼可以在Visual Studio中編譯,但是它無法在linux-ubuntu上編譯。與與Arduino在ubuntu c + +串行通信與boost :: asio

我編譯時不使用任何標誌。這可能是問題嗎?

#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’ 

回答

1

的彙編的碰撞與宏定義CSIZE別處失敗所致。爲serial_port_base::character_size CSIZE變量提供不同的標識符,例如CHAR_SIZE在兩個位置,並且應該編譯。

編譯器錯誤「期望數字常量之前的非限定標識」暗示CSIZE正在作爲數字常量處理。通過該提示,我用g++ -E -dD -dI -P標誌調用編譯器來執行源代碼的預處理,保留宏和包含指令。輸出結果表明在我的系統上,bits/termios.h#define CSIZE 0000060