当前位置: 首页>>代码示例>>C++>>正文


C++ Serial::open_serial_port方法代码示例

本文整理汇总了C++中Serial::open_serial_port方法的典型用法代码示例。如果您正苦于以下问题:C++ Serial::open_serial_port方法的具体用法?C++ Serial::open_serial_port怎么用?C++ Serial::open_serial_port使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Serial的用法示例。


在下文中一共展示了Serial::open_serial_port方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

/**
 * The start the Arduino node
 *
 * This read command line arguments if applicable the opens the serial port.
 * Then reads the serial port at 400 Hz.
 *
 */
int main(int argc, char **argv) {

    std::string usb_port_name = ARDUINO_SERIAL_PORT_DEFAULT;
    int baudrate = ARDUINO_BAUDRATE_DEFAULT;
    int opt;

    //to initialize ros with command line arguments and name of the node
    ros::init(argc, argv, "arduino_handler");

    ros::NodeHandle n;

    //get any params/ set any params
    n.param<std::string>("dev", usb_port_name, usb_port_name);
    n.param<int>("baud_rate", baudrate, baudrate);

    //commandline arguments take precedance to params
    while(( opt = getopt(argc, argv, "u:b:")) != -1) {
        switch (opt) {
        case 'u':
            usb_port_name.assign(optarg);
            n.setParam("dev",usb_port_name);
            break;
        case 'b':
            baudrate = atoi(optarg);
            n.setParam("baud_rate", baudrate);
            break;
        default:
            break;
        }
    }

    bIsConnected = false;
    nmeaChecksumFailedCount = 0;

    //advertise(topic_name, queue_size, latch)
    tIsAduinoConnected = n.advertise<std_msgs::Bool>(IsArduinoConnected_T, 1, true);
    std_msgs::Bool msg_isArduinoConnected;
    msg_isArduinoConnected.data = bIsConnected;
    tIsAduinoConnected.publish(msg_isArduinoConnected);

    tIMUData = n.advertise<ghost::imu>(IMU_T, 1, true);

    Serial serial;

    if (serial.open_serial_port(usb_port_name, baudrate) == false) {
        ros::shutdown();
    }

    ros::Timer watchdogTimer = n.createTimer(ros::Duration(ARDUINO_WATCHDOG_DURATION), arduinoWatchdog);
    ros::Timer heartbeatTimer = n.createTimer(ros::Duration(ARDUINO_HEARTBEAT_DURATION), arduinoHeartBeat);

    //specify the speed of the loop
    ros::Rate loop(ARDUINO_LOOP_RATE);

    std::string inBuf;

    //loop until program is closed (Ctrl-C or ros::shutdown())
    while (ros::ok()) {

        //lastConnectedTime.sec

        inBuf += serial.read_serial_data();
        parse_nmea_message(inBuf);

        //to check if subscribed topics have updated
        ros::spinOnce();

        //sleep until next loop time
        loop.sleep();
    }

    watchdogTimer.stop();
    heartbeatTimer.start();

    serial.close_serial_port();

    return 0;
}
开发者ID:illinoistechrobotics,项目名称:ghost,代码行数:85,代码来源:arduino.cpp


注:本文中的Serial::open_serial_port方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。