愛在花開的季節 发表于 2024-8-29 14:12:55

【C/C++】开源串口库 CSerialPort 应用

1、简述

本文主要讲述 C++开源库 CSerialPort 的使用,主要在Windows和Linux两个平台分别应用。
有需要了解 C++开源库 CSerialPort 的小同伴,可以先去这边文章下了解下为什么要用 CSerialPort 和 CSerialPort 的介绍。
文章链接:【C/C++】轻量级跨平台 开源串口库 CSerialPort
2、效果图

2.1、命令行(不带GUI)

Windows效果如下,Linux一样可以运行,只是串口名称不一样。因为还要切换系统,我就不截Linux图了
https://i-blog.csdnimg.cn/blog_migrate/7eca944e433d1dc908c62b487ecbe5c8.png
2.2、GUI(这里用的Qt)

放开pro内里的这两行即可,解释掉就是命令行,Windows效果图如下。Linux下这台没装Qt,没尝试,串口名称改下问题不大。
DEFINES += SHASHIDI_GUI
CONFIG += SHASHIDI_GUI
https://i-blog.csdnimg.cn/blog_migrate/8430e2fb370a640b3e830d3855791764.png
3、串口硬件知识普及

https://i-blog.csdnimg.cn/blog_migrate/8cb23b33b4e0a734f997cbb567d295c8.jpeg
因为只测试串口的收发消息,把发送和吸收的两个端口短接。找一个杜邦线直接串联起来就可以,这样发什么消息,就收到什么消息。如图是1口和3口短接。
4、焦点实现

4.1、Qt的pro文件

Qt 的 pro 文件,如下。这里我用了一个宏来区分,一个是Pro的区分,一个是代码内里的区分
DEFINES += SHASHIDI_GUI
CONFIG += SHASHIDI_GUI
SHASHIDI_GUI{QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = Demo_SerialPortSOURCES += main.cpp \    mainwindow.cppFORMS += \    mainwindow.uiHEADERS += \    mainwindow.h} else {TARGET = Demo_SerialPort_ConsoleCONFIG += console c++11CONFIG -= app_bundleCONFIG -= qtHEADERS += \    jsserialport.hSOURCES += main.cpp \    jsserialport.cpp}TEMPLATE = appDEFINES += _UNICODEinclude($$PWD/CSerialPort/CSerialPort.pri)win32*: {    #about windows reg    LIBS += $$PWD/CSerialPort/windows/x86/advapi32.lib    #about windows setupapi    LIBS += $$PWD/CSerialPort/windows/x86/setupapi.lib}CONFIG(debug, debug|release) {    DESTDIR = $$PWD/bin_debug} else {    DESTDIR = $$PWD/bin_release}HEADERS += \    CommonSerialPort.h 4.2、main文件

#ifdef SHASHIDI_GUI
#include "mainwindow.h"
#include <QApplication>
#else
#include "jsserialport.h"
#include <iostream>
using namespace std;
#endif


#ifdef SHASHIDI_GUI
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
#else
int main()
{
    JSSerialPort js;
    js.doWork();

    return 0;
#endif
}
4.3、SSerialPort类

4.3.1、头文件

#ifndef SSERIALPORT_H
#define SSERIALPORT_H

#include <iostream>
#include "CSerialPort/SerialPort.h"
#include "CSerialPort/SerialPortInfo.h"
#include "CommonSerialPort.h"

using namespace std;
using namespace itas109;

class SSerialPort : public CSerialPortListener
{
public:
    SSerialPort();

    // 工作
    void doWork();

    // 校验
    void doCheck(char receiveBuf[], int receiveSize);

    // 串口信号
    bool m_flag_serialport = false;

    // 发出消息
    void sendMessage();

    // 读取全部数据
    void readAllData();

private:
    // 获取端口列表
    vector<SerialPortInfo> getSerialPortList();

    // 打开串口
    bool openSerialPort(const char *portName,
                        int baudRate = 9600,
                        itas109::Parity parity = itas109::Parity::ParityNone,
                        itas109::DataBits dataBits = itas109::DataBits::DataBits8,
                        itas109::StopBits stopbits = itas109::StopBits::StopOne,
                        itas109::FlowControl flowControl = itas109::FlowControl::FlowNone,
                        unsigned int readBufferSize = 4096, int timeout = 0);

    // 关闭串口
    void closeSerialPort();

    // 收到消息
    void onReadEvent(const char *portName, unsigned int readBufferLen);

    // 设置串口模式同步\异步
    void setSync(bool isSync);

    // 设置DTR
    void setDTR(bool isDTR);

    // 设置RTS
    void setRTS(bool isRTS);

    // 初始化串口
    void initSerialPort();

    // 初始化发送消息
    void initSendMessage();
private:
    CSerialPort m_SerialPort;   // 串口

    struct TERMINAL_TO_FIRECONTROL *m_terminal_to_firecontrol;    // 终端到火控
    struct FIRECONTROL_TO_TERMINAL *m_firecontrol_to_terminal;    // 火控到终端

    int m_packet_frame_count = 0;   // 报文帧号累计
    int m_buffer_size = 2048;
    int m_frame_length = 34;
    unsigned char m_firstChar = 161;    // 0xA1
    unsigned char m_secondChar = 177;   // 0xB1
    unsigned char m_thirdChar = 50;   // 0x32
    int m_tx = 0;
    int m_rx = 0;
};

#endif // SSERIALPORT_H
4.3.2、源文件

#include "sserialport.h"

#include <iostream>
#include <iomanip>

SSerialPort::SSerialPort()
{
}

void SSerialPort::doWork()
{
    cout << "doWork: initSerialPort" << endl;
    initSerialPort();
    cout << "doWork: initSendMessage" << endl;
    initSendMessage();
    cout << "doWork: sendMessage" << endl;
    sendMessage();
    cout << "doWork: readAllData" << endl;
    readAllData();
}

void SSerialPort::doCheck(char receiveBuf[], int receiveSize)
{
    cout << "doCheck: start" << receiveSize << endl;
    for(int i = 0; i < receiveSize; i++) {
      if( ( receiveSize - i ) < m_frame_length ) { //不足一帧则退出
            break;
      }
      cout << "doCheck1: " << receiveBuf << endl;
      if( (receiveBuf == m_secondChar) && (receiveBuf == m_firstChar) ) { //发现帧头,取出一帧校验
            cout << "doCheck: " << receiveBuf << endl;
            string aa(receiveBuf);
            cout << "doCheck: " << aa << endl;
            string tmp_s(aa, i, m_frame_length);
            cout << "doCheck: " << tmp_s << endl;
            int data = 0;
            for(int i = 0; i < (m_frame_length - 1); i++) {
                data = data + tmp_s;
            }
            unsigned char data1 = data & 0x7f;
            if(data1 != tmp_s) {
                i = i + 1;
                continue;
            }
            //校验通过,取值
            m_firecontrol_to_terminal->send_address = tmp_s;
            m_firecontrol_to_terminal->receive_address = tmp_s;
            m_firecontrol_to_terminal->packet_length = tmp_s;
            m_firecontrol_to_terminal->packet_frame_number_low = tmp_s;
            m_firecontrol_to_terminal->packet_frame_number_high = tmp_s;
            m_firecontrol_to_terminal->light_aiming_status_word_1.all = tmp_s;
            m_firecontrol_to_terminal->light_aiming_status_word_2.all = tmp_s;
            m_firecontrol_to_terminal->laser_ranging_distance_low = tmp_s;
            m_firecontrol_to_terminal->laser_ranging_distance_high = tmp_s;
            m_firecontrol_to_terminal->servo_status_word_1.all = tmp_s;
            m_firecontrol_to_terminal->servo_status_word_2.all = tmp_s;
            m_firecontrol_to_terminal->speed_of_chassis = tmp_s;
            m_firecontrol_to_terminal->chassis_battery_power = tmp_s;
            m_firecontrol_to_terminal->front_flip_arm_angle_low = tmp_s;
            m_firecontrol_to_terminal->front_flip_arm_angle_high = tmp_s;
            m_firecontrol_to_terminal->back_flip_arm_angle_low = tmp_s;
            m_firecontrol_to_terminal->back_flip_arm_angle_high = tmp_s;
            m_firecontrol_to_terminal->relay_status_word.all = tmp_s;
            m_firecontrol_to_terminal->light_azimuth_position_low = tmp_s;
            m_firecontrol_to_terminal->light_azimuth_position_high = tmp_s;
            m_firecontrol_to_terminal->light_pitch_position_low = tmp_s;
            m_firecontrol_to_terminal->light_pitch_position_high = tmp_s;
            m_firecontrol_to_terminal->weapon_azimuth_position_low = tmp_s;
            m_firecontrol_to_terminal->weapon_azimuth_position_high = tmp_s;
            m_firecontrol_to_terminal->weapon_pitch_position_low = tmp_s;
            m_firecontrol_to_terminal->weapon_pitch_position_high = tmp_s;
            m_firecontrol_to_terminal->platform_pitch = tmp_s;
            m_firecontrol_to_terminal->platform_roll_angle = tmp_s;
            m_firecontrol_to_terminal->residual_bullet_low = tmp_s;
            m_firecontrol_to_terminal->residual_bullet_high = tmp_s;
            m_firecontrol_to_terminal->rsve1 = tmp_s;
            m_firecontrol_to_terminal->rsve2 = tmp_s;
            m_firecontrol_to_terminal->rsve3 = tmp_s;
            m_firecontrol_to_terminal->check = tmp_s;
            cout << "doCheck: send_address" << tmp_s << endl;
//            emit sig_check(data1);
      }
    }
}

vector<SerialPortInfo> SSerialPort::getSerialPortList()
{
    vector<SerialPortInfo> portNameList = CSerialPortInfo::availablePortInfos();

    for (size_t i = 0; i < portNameList.size(); i++) {
      cout << "getSerialPortList:" << portNameList.portName << endl;
    }
    if ( portNameList.size() == 0 ) {
      cout << "getSerialPortList is Null" << endl;
    }
    return portNameList;
}

bool SSerialPort::openSerialPort(const char *portName, int baudRate, Parity parity, DataBits dataBits, StopBits stopbits, FlowControl flowControl, unsigned int readBufferSize, int timeout)
{
    cout << "openSerialPort-----------------------------------start" << endl;
    bool isOpen = false;
    if(getSerialPortList().size() > 0) {
      m_SerialPort.init(portName, baudRate, parity, dataBits, stopbits, flowControl, readBufferSize);

      cout << "portName:" << portName << endl;
      cout << "baudRate:" << baudRate << endl;
      cout << "parity:" << parity << endl;
      cout << "dataBits:" << dataBits << endl;
      cout << "stopbits:" << stopbits << endl;
      cout << "flowControl:" << flowControl << endl;
      cout << "readBufferSize:" << readBufferSize << endl;
      m_SerialPort.setReadIntervalTimeout(timeout);
      isOpen = m_SerialPort.open();
      if(!isOpen) {
            cerr << "open port error" << m_SerialPort.getLastError() << m_SerialPort.getLastErrorMsg() << endl;
      }
    } else {
      cerr << "This Computer no avaiable port!" << endl;
    }
    cout << "openSerialPort-----------------------------------end" << endl;
    return isOpen;
}

void SSerialPort::closeSerialPort()
{
    m_SerialPort.close();
}

void SSerialPort::onReadEvent(const char *portName, unsigned int readBufferLen)
{
    cout << "onReadEvent:" << portName << readBufferLen << endl;
    if(readBufferLen > 0) {
      unsigned long recLen = 0;
      char * str = NULL;
      str = new char;
      recLen = m_SerialPort.readData(str, readBufferLen);

      if(recLen > 0) {
            // TODO: 中文需要由两个字符拼接,否则显示为空""
            cout << "onReadEvent:" << portName << str << recLen << endl;
            doCheck(str, recLen);
      } else {

      }

      if(str) {
            delete[] str;
            str = NULL;
      }
    }
}

void SSerialPort::sendMessage()
{
    char message;
    if(m_SerialPort.isOpen()) {
      // 伺服旋转限制

      // 高位低位赋值
      m_terminal_to_firecontrol->packet_frame_number_low = m_packet_frame_count & 0x00ff;
      m_terminal_to_firecontrol->packet_frame_number_high = (m_packet_frame_count >> 8) & 0x00ff;

      message = m_firstChar;
      message = m_secondChar;
      message = m_thirdChar;
      cout << "m_terminal_to_firecontrol->packet_frame_number_low: " << m_terminal_to_firecontrol->packet_frame_number_low << endl;
      message = m_terminal_to_firecontrol->packet_frame_number_low;
      message = m_terminal_to_firecontrol->packet_frame_number_high;
      message = m_terminal_to_firecontrol->light_aiming_control_word_1.all;//变化
      message = m_terminal_to_firecontrol->light_aiming_control_word_2.all;
      message = m_terminal_to_firecontrol->light_aiming_control_word_3.all;
      message = m_terminal_to_firecontrol->servo_control_word_1.all;
      message = m_terminal_to_firecontrol->servo_control_word_2.all;
      message = m_terminal_to_firecontrol->chassis_control_word_1.all;
      message = m_terminal_to_firecontrol->relay_control_word.all;
      message = m_terminal_to_firecontrol->light_azimuth_rotation_speed_low;
      message = m_terminal_to_firecontrol->light_azimuth_rotation_speed_high;
      message = m_terminal_to_firecontrol->light_pitch_rotation_speed_low;
      message = m_terminal_to_firecontrol->light_pitch_rotation_speed_high;
      message = m_terminal_to_firecontrol->fan_sweep_azimuth_setting_Angle;
      message = m_terminal_to_firecontrol->fan_sweep_azimuth_fu_setting_Angle;
      message = m_terminal_to_firecontrol->fan_sweep_pitch_setting_Angle;
      message = m_terminal_to_firecontrol->fan_sweep_pitch_fu_setting_Angle;
      message = m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_low;
      message = m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_high;
      message = m_terminal_to_firecontrol->quick_boot_pitch_coordinates_low;
      message = m_terminal_to_firecontrol->quick_boot_pitch_coordinates_high;
      message = m_terminal_to_firecontrol->left_track_speed_low;
      message = m_terminal_to_firecontrol->left_track_speed_high;
      message = m_terminal_to_firecontrol->right_track_speed_low;
      message = m_terminal_to_firecontrol->right_track_speed_high;
      message = m_terminal_to_firecontrol->forward_arm_speed;
      message = m_terminal_to_firecontrol->back_arm_speed;
      message = m_terminal_to_firecontrol->residual_low;
      message = m_terminal_to_firecontrol->residual_high;
      message = m_terminal_to_firecontrol->azimuth_correction;
      message = m_terminal_to_firecontrol->pitch_correction;
      message = m_terminal_to_firecontrol->delay_correction;
      message = m_terminal_to_firecontrol->light_aiming_control_word_4.all;
      message = m_terminal_to_firecontrol->light_aiming_control_word_5.all;
      message = m_terminal_to_firecontrol->servo_azimuth_rotation_speed_low;
      message = m_terminal_to_firecontrol->servo_azimuth_rotation_speed_high;
      message = m_terminal_to_firecontrol->servo_pitch_rotation_speed_low;
      message = m_terminal_to_firecontrol->servo_pitch_rotation_speed_high;
      message = m_terminal_to_firecontrol->firing_servo_start_low;
      message = m_terminal_to_firecontrol->firing_servo_start_high;
      message = m_terminal_to_firecontrol->firing_servo_stop_low;
      message = m_terminal_to_firecontrol->firing_servo_stop_high;
      message = m_terminal_to_firecontrol->firing_insurance_close_low;
      message = m_terminal_to_firecontrol->firing_insurance_close_high;
      message = m_terminal_to_firecontrol->firing_insurance_open_low;
      message = m_terminal_to_firecontrol->firing_insurance_open_high;

      int data = 0;
      for( int i = 0; i < 49; i++ ) {
            cout << "sendMessage: " << message << endl;
            data = data + message;
      }
      message = data & 0x7f;

      int length = sizeof(message)/sizeof(char);
      if( !m_flag_serialport ) {
            // 支持中文并获取正确的长度
            m_SerialPort.writeData(message, length);
      } else {

      }
      if( m_packet_frame_count >= 0xffff ) {
            m_packet_frame_count = 1;
      } else {
            m_packet_frame_count++;
      }
      cout << "sendMessage:" << message << " length: " << length << endl;
    } else {
      cerr << "please open serial port first!" << endl;
    }
}

void SSerialPort::readAllData()
{
    int recLen = 0;
    char str = {0};
    recLen = m_SerialPort.readAllData(str);

    if(recLen > 0) {
      // TODO: 中文需要由两个字符拼接,否则显示为空""
      cout << "readAllData:" << str << " length: " << recLen << endl;
      doCheck(str, recLen);
    } else {

    }
}

void SSerialPort::setSync(bool isSync)
{
    if(isSync) {
      m_SerialPort.setOperateMode(itas109::SynchronousOperate);
    } else {
      m_SerialPort.setOperateMode(itas109::AsynchronousOperate);
    }
}

void SSerialPort::setDTR(bool isDTR)
{
    m_SerialPort.setDtr(isDTR);
}

void SSerialPort::setRTS(bool isRTS)
{
    m_SerialPort.setRts(isRTS);
}

void SSerialPort::initSerialPort()
{
#ifdef WIN32
    this->openSerialPort("COM3", 115200, itas109::ParityNone,
                         itas109::DataBits8, itas109::StopOne);
#else
    this->openSerialPort("/dev/ttyUSB0", 115200, itas109::ParityNone,
                         itas109::DataBits8, itas109::StopOne);
#endif
}

void SSerialPort::initSendMessage()
{
    cout << "initSendMessage-----------------------------------start" << endl;
    m_packet_frame_count = 1;

    m_terminal_to_firecontrol = (struct TERMINAL_TO_FIRECONTROL*)malloc(sizeof(struct TERMINAL_TO_FIRECONTROL));
    m_firecontrol_to_terminal = (struct FIRECONTROL_TO_TERMINAL*)malloc(sizeof(struct FIRECONTROL_TO_TERMINAL));
    m_terminal_to_firecontrol->send_address = m_firstChar;
    m_terminal_to_firecontrol->receive_address = m_secondChar;
    m_terminal_to_firecontrol->packet_length = m_thirdChar;
    m_terminal_to_firecontrol->packet_frame_number_low = 1;
    std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(m_terminal_to_firecontrol->packet_frame_number_low)<<endl;
    m_terminal_to_firecontrol->packet_frame_number_high = 0;
    m_terminal_to_firecontrol->light_aiming_control_word_1.all = 0x01;
    m_terminal_to_firecontrol->light_aiming_control_word_2.all = 0x00;
    m_terminal_to_firecontrol->light_aiming_control_word_3.all = 0x00;
    m_terminal_to_firecontrol->servo_control_word_1.all = 0x09;
    m_terminal_to_firecontrol->servo_control_word_2.all = 0x02;
    m_terminal_to_firecontrol->chassis_control_word_1.all = 0x00;
    m_terminal_to_firecontrol->relay_control_word.all = 0x00;
    m_terminal_to_firecontrol->light_azimuth_rotation_speed_low = 0x00;
    m_terminal_to_firecontrol->light_azimuth_rotation_speed_high = 0X00;
    m_terminal_to_firecontrol->light_pitch_rotation_speed_low = 0;
    m_terminal_to_firecontrol->light_pitch_rotation_speed_high = 0;
    m_terminal_to_firecontrol->fan_sweep_azimuth_setting_Angle = 0;
    m_terminal_to_firecontrol->fan_sweep_azimuth_fu_setting_Angle = 0;
    m_terminal_to_firecontrol->fan_sweep_pitch_setting_Angle = 0;
    m_terminal_to_firecontrol->fan_sweep_pitch_fu_setting_Angle = 0;
    m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_low = 0;
    m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_high = 0;
    m_terminal_to_firecontrol->quick_boot_pitch_coordinates_low = 0;
    m_terminal_to_firecontrol->quick_boot_pitch_coordinates_high = 0;
    m_terminal_to_firecontrol->left_track_speed_low = 0;
    m_terminal_to_firecontrol->left_track_speed_high = 0;
    m_terminal_to_firecontrol->right_track_speed_low = 0;
    m_terminal_to_firecontrol->right_track_speed_high = 0;
    m_terminal_to_firecontrol->forward_arm_speed = 0;
    m_terminal_to_firecontrol->back_arm_speed = 0;
    m_terminal_to_firecontrol->residual_low = 0;
    m_terminal_to_firecontrol->residual_high = 0;
    m_terminal_to_firecontrol->azimuth_correction = 0;
    m_terminal_to_firecontrol->pitch_correction = 0;
    m_terminal_to_firecontrol->delay_correction = 0;
    m_terminal_to_firecontrol->light_aiming_control_word_4.all = 0;
    m_terminal_to_firecontrol->light_aiming_control_word_5.all = 0;
    m_terminal_to_firecontrol->servo_azimuth_rotation_speed_low = 0x00;
    m_terminal_to_firecontrol->servo_azimuth_rotation_speed_high = 0x00;
    m_terminal_to_firecontrol->servo_pitch_rotation_speed_low = 0;
    m_terminal_to_firecontrol->servo_pitch_rotation_speed_high = 0;
    m_terminal_to_firecontrol->firing_servo_start_low = 0;
    m_terminal_to_firecontrol->firing_servo_start_high = 0;
    m_terminal_to_firecontrol->firing_servo_stop_low = 0;
    m_terminal_to_firecontrol->firing_servo_stop_high = 0;
    m_terminal_to_firecontrol->firing_insurance_close_low = 0;
    m_terminal_to_firecontrol->firing_insurance_close_high = 0;
    m_terminal_to_firecontrol->firing_insurance_open_low = 0;
    m_terminal_to_firecontrol->firing_insurance_open_high = 0;;
    m_terminal_to_firecontrol->check = 0;
    cout << "initSendMessage-----------------------------------end" << endl;
}

4.4、Linux下的CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)
project(electro_optical_system)

## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)

## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
find_package(OpenCV REQUIRED)

## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()

################################################
## Declare ROS messages, services and actions ##
################################################

## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##   but can be declared for certainty nonetheless:
##   * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
##   * add "message_generation" and every package in MSG_DEP_SET to
##   find_package(catkin REQUIRED COMPONENTS ...)
##   * add "message_runtime" and every package in MSG_DEP_SET to
##   catkin_package(CATKIN_DEPENDS ...)
##   * uncomment the add_*_files sections below as needed
##   and list every .msg/.srv/.action file to be processed
##   * uncomment the generate_messages entry below
##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

## Generate messages in the 'msg' folder
# add_message_files(
#   FILES
#   Message1.msg
#   Message2.msg
# )

## Generate services in the 'srv' folder
# add_service_files(
#   FILES
#   Service1.srv
#   Service2.srv
# )

## Generate actions in the 'action' folder
# add_action_files(
#   FILES
#   Action1.action
#   Action2.action
# )

## Generate added messages and services with any dependencies listed here
# generate_messages(
#   DEPENDENCIES
#   std_msgs
# )

################################################
## Declare ROS dynamic reconfigure parameters ##
################################################

## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
##   * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
##   * add "dynamic_reconfigure" to
##   find_package(catkin REQUIRED COMPONENTS ...)
##   * uncomment the "generate_dynamic_reconfigure_options" section below
##   and list every .cfg file to be processed

## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
#   cfg/DynReconf1.cfg
#   cfg/DynReconf2.cfg
# )

###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
#INCLUDE_DIRS include
#LIBRARIES electro_optical_system
#CATKIN_DEPENDS roscpp rospy std_msgs
#DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)

## Declare a C++ library
# add_library(${PROJECT_NAME}
#   src/${PROJECT_NAME}/electro_optical_system.cpp
# )

## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/electro_optical_system_node.cpp)

## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
#   ${catkin_LIBRARIES}
# )

#############
## Install ##
#############

# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# catkin_install_python(PROGRAMS
#   scripts/my_python_script
#   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
#   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
#   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# )

## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
#   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
#   FILES_MATCHING PATTERN "*.h"
#   PATTERN ".svn" EXCLUDE
# )

## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
#   # myfile1
#   # myfile2
#   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )

#############
## Testing ##
#############

## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_electro_optical_system.cpp)
# if(TARGET ${PROJECT_NAME}-test)
#   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()

## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
add_executable(EOS src/main.cpp src/jsserialport.cpp src/CSerialPort/SerialPort.cpp src/CSerialPort/SerialPortInfo.cppsrc/CSerialPort/SerialPortUnixBase.cpp src/CSerialPort/SerialPortBase.cppsrc/CSerialPort/SerialPortInfoUnixBase.cppsrc/CSerialPort/SerialPortInfoBase.cpp)
target_link_libraries(EOS ${catkin_LIBRARIES}
                              ${OpenCV_LIBS})
add_dependencies(EOS ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 【C/C++】开源串口库 CSerialPort 应用