0


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

文章目录

1、简述

本文主要讲述 C++开源库 CSerialPort 的使用,主要在Windows和Linux两个平台分别应用。
有需要了解 C++开源库 CSerialPort 的小伙伴,可以先去这边文章下了解下为什么要用 CSerialPort 和 CSerialPort 的介绍。

文章链接:【C/C++】轻量级跨平台 开源串口库 CSerialPort

2、效果图

2.1、命令行(不带GUI)

Windows效果如下,Linux一样可以运行,只是串口名称不一样。因为还要切换系统,我就不截Linux图了
在这里插入图片描述

2.2、GUI(这里用的Qt)

放开pro里面的这两行即可,注释掉就是命令行,Windows效果图如下。Linux下这台没装Qt,没尝试,串口名称改下问题不大。

  1. DEFINES += SHASHIDI_GUI
  2. CONFIG += SHASHIDI_GUI

在这里插入图片描述

3、串口硬件知识普及

在这里插入图片描述
因为只测试串口的收发消息,把发送和接收的两个端口短接。找一个杜邦线直接串联起来就可以,这样发什么消息,就收到什么消息。如图是1口和3口短接。

4、核心实现

4.1、Qt的pro文件

Qt 的 pro 文件,如下。这里我用了一个宏来区分,一个是Pro的区分,一个是代码里面的区分

  1. DEFINES+=SHASHIDI_GUICONFIG+=SHASHIDI_GUISHASHIDI_GUI{QT+= core gui
  2. greaterThan(QT_MAJOR_VERSION,4):QT+= widgets
  3. TARGET= Demo_SerialPort
  4. SOURCES+= main.cpp \
  5. mainwindow.cpp
  6. FORMS+= \
  7. mainwindow.ui
  8. HEADERS+= \
  9. mainwindow.h
  10. }else{TARGET= Demo_SerialPort_Console
  11. CONFIG+= console c++11CONFIG-= app_bundle
  12. CONFIG-= qt
  13. HEADERS+= \
  14. jsserialport.h
  15. SOURCES+= main.cpp \
  16. jsserialport.cpp
  17. }TEMPLATE= app
  18. DEFINES+= _UNICODE
  19. include($$PWD/CSerialPort/CSerialPort.pri)
  20. win32*:{
  21. #about windows reg
  22. LIBS+= $$PWD/CSerialPort/windows/x86/advapi32.lib
  23. #about windows setupapi
  24. LIBS+= $$PWD/CSerialPort/windows/x86/setupapi.lib
  25. }CONFIG(debug, debug|release){DESTDIR= $$PWD/bin_debug
  26. }else{DESTDIR= $$PWD/bin_release
  27. }HEADERS+= \
  28. CommonSerialPort.h

4.2、main文件

  1. #ifdefSHASHIDI_GUI#include"mainwindow.h"#include<QApplication>#else#include"jsserialport.h"#include<iostream>
  2. using namespace std;#endif#ifdefSHASHIDI_GUIintmain(int argc,char*argv[]){
  3. QApplication a(argc, argv);
  4. MainWindow w;
  5. w.show();return a.exec();#elseintmain(){
  6. JSSerialPort js;
  7. js.doWork();return0;#endif}

4.3、SSerialPort类

4.3.1、头文件

  1. #ifndefSSERIALPORT_H#defineSSERIALPORT_H#include<iostream>#include"CSerialPort/SerialPort.h"#include"CSerialPort/SerialPortInfo.h"#include"CommonSerialPort.h"
  2. using namespace std;
  3. using namespace itas109;
  4. class SSerialPort : public CSerialPortListener
  5. {
  6. public:SSerialPort();// 工作voiddoWork();// 校验voiddoCheck(char receiveBuf[],int receiveSize);// 串口信号
  7. bool m_flag_serialport = false;// 发出消息voidsendMessage();// 读取全部数据voidreadAllData();
  8. private:// 获取端口列表
  9. vector<SerialPortInfo>getSerialPortList();// 打开串口
  10. bool openSerialPort(constchar*portName,int baudRate =9600,
  11. itas109::Parity parity = itas109::Parity::ParityNone,
  12. itas109::DataBits dataBits = itas109::DataBits::DataBits8,
  13. itas109::StopBits stopbits = itas109::StopBits::StopOne,
  14. itas109::FlowControl flowControl = itas109::FlowControl::FlowNone,unsignedint readBufferSize =4096,int timeout =0);// 关闭串口voidcloseSerialPort();// 收到消息voidonReadEvent(constchar*portName,unsignedint readBufferLen);// 设置串口模式 同步\异步voidsetSync(bool isSync);// 设置DTRvoidsetDTR(bool isDTR);// 设置RTSvoidsetRTS(bool isRTS);// 初始化串口voidinitSerialPort();// 初始化发送消息voidinitSendMessage();
  15. private:
  16. CSerialPort m_SerialPort;// 串口structTERMINAL_TO_FIRECONTROL*m_terminal_to_firecontrol;// 终端到火控structFIRECONTROL_TO_TERMINAL*m_firecontrol_to_terminal;// 火控到终端int m_packet_frame_count =0;// 报文帧号累计int m_buffer_size =2048;int m_frame_length =34;unsignedchar m_firstChar =161;// 0xA1unsignedchar m_secondChar =177;// 0xB1unsignedchar m_thirdChar =50;// 0x32int m_tx =0;int m_rx =0;};#endif// SSERIALPORT_H

4.3.2、源文件

  1. #include"sserialport.h"#include<iostream>#include<iomanip>
  2. SSerialPort::SSerialPort(){}void SSerialPort::doWork(){
  3. cout <<"doWork: initSerialPort"<< endl;initSerialPort();
  4. cout <<"doWork: initSendMessage"<< endl;initSendMessage();
  5. cout <<"doWork: sendMessage"<< endl;sendMessage();
  6. cout <<"doWork: readAllData"<< endl;readAllData();}void SSerialPort::doCheck(char receiveBuf[],int receiveSize){
  7. cout <<"doCheck: start"<< receiveSize << endl;for(int i =0; i < receiveSize; i++){if(( receiveSize - i )< m_frame_length ){//不足一帧则退出break;}
  8. cout <<"doCheck1: "<< receiveBuf[i]<< endl;if((receiveBuf[i]== m_secondChar)&&(receiveBuf[i+1]== m_firstChar)){//发现帧头,取出一帧校验
  9. cout <<"doCheck: "<< receiveBuf[i]<< endl;
  10. string aa(receiveBuf);
  11. cout <<"doCheck: "<< aa << endl;
  12. string tmp_s(aa, i, m_frame_length);
  13. cout <<"doCheck: "<< tmp_s << endl;int data =0;for(int i =0; i <(m_frame_length -1); i++){
  14. data = data + tmp_s[i];}unsignedchar data1 = data &0x7f;if(data1 != tmp_s[m_frame_length -1]){
  15. i = i +1;continue;}//校验通过,取值
  16. m_firecontrol_to_terminal->send_address = tmp_s[0];
  17. m_firecontrol_to_terminal->receive_address = tmp_s[1];
  18. m_firecontrol_to_terminal->packet_length = tmp_s[2];
  19. m_firecontrol_to_terminal->packet_frame_number_low = tmp_s[3];
  20. m_firecontrol_to_terminal->packet_frame_number_high = tmp_s[4];
  21. m_firecontrol_to_terminal->light_aiming_status_word_1.all = tmp_s[5];
  22. m_firecontrol_to_terminal->light_aiming_status_word_2.all = tmp_s[6];
  23. m_firecontrol_to_terminal->laser_ranging_distance_low = tmp_s[7];
  24. m_firecontrol_to_terminal->laser_ranging_distance_high = tmp_s[8];
  25. m_firecontrol_to_terminal->servo_status_word_1.all = tmp_s[9];
  26. m_firecontrol_to_terminal->servo_status_word_2.all = tmp_s[10];
  27. m_firecontrol_to_terminal->speed_of_chassis = tmp_s[11];
  28. m_firecontrol_to_terminal->chassis_battery_power = tmp_s[12];
  29. m_firecontrol_to_terminal->front_flip_arm_angle_low = tmp_s[13];
  30. m_firecontrol_to_terminal->front_flip_arm_angle_high = tmp_s[14];
  31. m_firecontrol_to_terminal->back_flip_arm_angle_low = tmp_s[15];
  32. m_firecontrol_to_terminal->back_flip_arm_angle_high = tmp_s[16];
  33. m_firecontrol_to_terminal->relay_status_word.all = tmp_s[17];
  34. m_firecontrol_to_terminal->light_azimuth_position_low = tmp_s[18];
  35. m_firecontrol_to_terminal->light_azimuth_position_high = tmp_s[19];
  36. m_firecontrol_to_terminal->light_pitch_position_low = tmp_s[20];
  37. m_firecontrol_to_terminal->light_pitch_position_high = tmp_s[21];
  38. m_firecontrol_to_terminal->weapon_azimuth_position_low = tmp_s[22];
  39. m_firecontrol_to_terminal->weapon_azimuth_position_high = tmp_s[23];
  40. m_firecontrol_to_terminal->weapon_pitch_position_low = tmp_s[24];
  41. m_firecontrol_to_terminal->weapon_pitch_position_high = tmp_s[25];
  42. m_firecontrol_to_terminal->platform_pitch = tmp_s[26];
  43. m_firecontrol_to_terminal->platform_roll_angle = tmp_s[27];
  44. m_firecontrol_to_terminal->residual_bullet_low = tmp_s[28];
  45. m_firecontrol_to_terminal->residual_bullet_high = tmp_s[29];
  46. m_firecontrol_to_terminal->rsve1 = tmp_s[30];
  47. m_firecontrol_to_terminal->rsve2 = tmp_s[31];
  48. m_firecontrol_to_terminal->rsve3 = tmp_s[32];
  49. m_firecontrol_to_terminal->check = tmp_s[33];
  50. cout <<"doCheck: send_address"<< tmp_s << endl;// emit sig_check(data1);}}}
  51. vector<SerialPortInfo> SSerialPort::getSerialPortList(){
  52. vector<SerialPortInfo> portNameList = CSerialPortInfo::availablePortInfos();for(size_t i =0; i < portNameList.size(); i++){
  53. cout <<"getSerialPortList:"<< portNameList[i].portName << endl;}if( portNameList.size()==0){
  54. cout <<"getSerialPortList is Null"<< endl;}return portNameList;}
  55. bool SSerialPort::openSerialPort(constchar*portName,int baudRate, Parity parity, DataBits dataBits, StopBits stopbits, FlowControl flowControl,unsignedint readBufferSize,int timeout){
  56. cout <<"openSerialPort-----------------------------------start"<< endl;
  57. bool isOpen = false;if(getSerialPortList().size()>0){
  58. m_SerialPort.init(portName, baudRate, parity, dataBits, stopbits, flowControl, readBufferSize);
  59. cout <<"portName:"<< portName << endl;
  60. cout <<"baudRate:"<< baudRate << endl;
  61. cout <<"parity:"<< parity << endl;
  62. cout <<"dataBits:"<< dataBits << endl;
  63. cout <<"stopbits:"<< stopbits << endl;
  64. cout <<"flowControl:"<< flowControl << endl;
  65. cout <<"readBufferSize:"<< readBufferSize << endl;
  66. m_SerialPort.setReadIntervalTimeout(timeout);
  67. isOpen = m_SerialPort.open();if(!isOpen){
  68. cerr <<"open port error"<< m_SerialPort.getLastError()<< m_SerialPort.getLastErrorMsg()<< endl;}}else{
  69. cerr <<"This Computer no avaiable port!"<< endl;}
  70. cout <<"openSerialPort-----------------------------------end"<< endl;return isOpen;}void SSerialPort::closeSerialPort(){
  71. m_SerialPort.close();}void SSerialPort::onReadEvent(constchar*portName,unsignedint readBufferLen){
  72. cout <<"onReadEvent:"<< portName << readBufferLen << endl;if(readBufferLen >0){unsignedlong recLen =0;char* str =NULL;
  73. str = new char[readBufferLen];
  74. recLen = m_SerialPort.readData(str, readBufferLen);if(recLen >0){// TODO: 中文需要由两个字符拼接,否则显示为空""
  75. cout <<"onReadEvent:"<< portName << str << recLen << endl;doCheck(str, recLen);}else{}if(str){
  76. delete[] str;
  77. str =NULL;}}}void SSerialPort::sendMessage(){char message[50];if(m_SerialPort.isOpen()){// 伺服旋转限制// 高位低位赋值
  78. m_terminal_to_firecontrol->packet_frame_number_low = m_packet_frame_count &0x00ff;
  79. m_terminal_to_firecontrol->packet_frame_number_high =(m_packet_frame_count >>8)&0x00ff;
  80. message[0]= m_firstChar;
  81. message[1]= m_secondChar;
  82. message[2]= m_thirdChar;
  83. cout <<"m_terminal_to_firecontrol->packet_frame_number_low: "<< m_terminal_to_firecontrol->packet_frame_number_low << endl;
  84. message[3]= m_terminal_to_firecontrol->packet_frame_number_low;
  85. message[4]= m_terminal_to_firecontrol->packet_frame_number_high;
  86. message[5]= m_terminal_to_firecontrol->light_aiming_control_word_1.all;//变化
  87. message[6]= m_terminal_to_firecontrol->light_aiming_control_word_2.all;
  88. message[7]= m_terminal_to_firecontrol->light_aiming_control_word_3.all;
  89. message[8]= m_terminal_to_firecontrol->servo_control_word_1.all;
  90. message[9]= m_terminal_to_firecontrol->servo_control_word_2.all;
  91. message[10]= m_terminal_to_firecontrol->chassis_control_word_1.all;
  92. message[11]= m_terminal_to_firecontrol->relay_control_word.all;
  93. message[12]= m_terminal_to_firecontrol->light_azimuth_rotation_speed_low;
  94. message[13]= m_terminal_to_firecontrol->light_azimuth_rotation_speed_high;
  95. message[14]= m_terminal_to_firecontrol->light_pitch_rotation_speed_low;
  96. message[15]= m_terminal_to_firecontrol->light_pitch_rotation_speed_high;
  97. message[16]= m_terminal_to_firecontrol->fan_sweep_azimuth_setting_Angle;
  98. message[17]= m_terminal_to_firecontrol->fan_sweep_azimuth_fu_setting_Angle;
  99. message[18]= m_terminal_to_firecontrol->fan_sweep_pitch_setting_Angle;
  100. message[19]= m_terminal_to_firecontrol->fan_sweep_pitch_fu_setting_Angle;
  101. message[20]= m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_low;
  102. message[21]= m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_high;
  103. message[22]= m_terminal_to_firecontrol->quick_boot_pitch_coordinates_low;
  104. message[23]= m_terminal_to_firecontrol->quick_boot_pitch_coordinates_high;
  105. message[24]= m_terminal_to_firecontrol->left_track_speed_low;
  106. message[25]= m_terminal_to_firecontrol->left_track_speed_high;
  107. message[26]= m_terminal_to_firecontrol->right_track_speed_low;
  108. message[27]= m_terminal_to_firecontrol->right_track_speed_high;
  109. message[28]= m_terminal_to_firecontrol->forward_arm_speed;
  110. message[29]= m_terminal_to_firecontrol->back_arm_speed;
  111. message[30]= m_terminal_to_firecontrol->residual_low;
  112. message[31]= m_terminal_to_firecontrol->residual_high;
  113. message[32]= m_terminal_to_firecontrol->azimuth_correction;
  114. message[33]= m_terminal_to_firecontrol->pitch_correction;
  115. message[34]= m_terminal_to_firecontrol->delay_correction;
  116. message[35]= m_terminal_to_firecontrol->light_aiming_control_word_4.all;
  117. message[36]= m_terminal_to_firecontrol->light_aiming_control_word_5.all;
  118. message[37]= m_terminal_to_firecontrol->servo_azimuth_rotation_speed_low;
  119. message[38]= m_terminal_to_firecontrol->servo_azimuth_rotation_speed_high;
  120. message[39]= m_terminal_to_firecontrol->servo_pitch_rotation_speed_low;
  121. message[40]= m_terminal_to_firecontrol->servo_pitch_rotation_speed_high;
  122. message[41]= m_terminal_to_firecontrol->firing_servo_start_low;
  123. message[42]= m_terminal_to_firecontrol->firing_servo_start_high;
  124. message[43]= m_terminal_to_firecontrol->firing_servo_stop_low;
  125. message[44]= m_terminal_to_firecontrol->firing_servo_stop_high;
  126. message[45]= m_terminal_to_firecontrol->firing_insurance_close_low;
  127. message[46]= m_terminal_to_firecontrol->firing_insurance_close_high;
  128. message[47]= m_terminal_to_firecontrol->firing_insurance_open_low;
  129. message[48]= m_terminal_to_firecontrol->firing_insurance_open_high;int data =0;for(int i =0; i <49; i++){
  130. cout <<"sendMessage: "<< message[i]<< endl;
  131. data = data + message[i];}
  132. message[49]= data &0x7f;int length =sizeof(message)/sizeof(char);if(!m_flag_serialport ){// 支持中文并获取正确的长度
  133. m_SerialPort.writeData(message, length);}else{}if( m_packet_frame_count >=0xffff){
  134. m_packet_frame_count =1;}else{
  135. m_packet_frame_count++;}
  136. cout <<"sendMessage:"<< message <<" length: "<< length << endl;}else{
  137. cerr <<"please open serial port first!"<< endl;}}void SSerialPort::readAllData(){int recLen =0;char str[4096]={0};
  138. recLen = m_SerialPort.readAllData(str);if(recLen >0){// TODO: 中文需要由两个字符拼接,否则显示为空""
  139. cout <<"readAllData:"<< str <<" length: "<< recLen << endl;doCheck(str, recLen);}else{}}void SSerialPort::setSync(bool isSync){if(isSync){
  140. m_SerialPort.setOperateMode(itas109::SynchronousOperate);}else{
  141. m_SerialPort.setOperateMode(itas109::AsynchronousOperate);}}void SSerialPort::setDTR(bool isDTR){
  142. m_SerialPort.setDtr(isDTR);}void SSerialPort::setRTS(bool isRTS){
  143. m_SerialPort.setRts(isRTS);}void SSerialPort::initSerialPort(){#ifdefWIN32
  144. this->openSerialPort("COM3",115200, itas109::ParityNone,
  145. itas109::DataBits8, itas109::StopOne);#else
  146. this->openSerialPort("/dev/ttyUSB0",115200, itas109::ParityNone,
  147. itas109::DataBits8, itas109::StopOne);#endif}void SSerialPort::initSendMessage(){
  148. cout <<"initSendMessage-----------------------------------start"<< endl;
  149. m_packet_frame_count =1;
  150. m_terminal_to_firecontrol =(structTERMINAL_TO_FIRECONTROL*)malloc(sizeof(structTERMINAL_TO_FIRECONTROL));
  151. m_firecontrol_to_terminal =(structFIRECONTROL_TO_TERMINAL*)malloc(sizeof(structFIRECONTROL_TO_TERMINAL));
  152. m_terminal_to_firecontrol->send_address = m_firstChar;
  153. m_terminal_to_firecontrol->receive_address = m_secondChar;
  154. m_terminal_to_firecontrol->packet_length = m_thirdChar;
  155. m_terminal_to_firecontrol->packet_frame_number_low =1;
  156. std::cout << std::hex << std::setw(2)<< std::setfill('0')<< static_cast<int>(m_terminal_to_firecontrol->packet_frame_number_low)<<endl;
  157. m_terminal_to_firecontrol->packet_frame_number_high =0;
  158. m_terminal_to_firecontrol->light_aiming_control_word_1.all =0x01;
  159. m_terminal_to_firecontrol->light_aiming_control_word_2.all =0x00;
  160. m_terminal_to_firecontrol->light_aiming_control_word_3.all =0x00;
  161. m_terminal_to_firecontrol->servo_control_word_1.all =0x09;
  162. m_terminal_to_firecontrol->servo_control_word_2.all =0x02;
  163. m_terminal_to_firecontrol->chassis_control_word_1.all =0x00;
  164. m_terminal_to_firecontrol->relay_control_word.all =0x00;
  165. m_terminal_to_firecontrol->light_azimuth_rotation_speed_low =0x00;
  166. m_terminal_to_firecontrol->light_azimuth_rotation_speed_high =0X00;
  167. m_terminal_to_firecontrol->light_pitch_rotation_speed_low =0;
  168. m_terminal_to_firecontrol->light_pitch_rotation_speed_high =0;
  169. m_terminal_to_firecontrol->fan_sweep_azimuth_setting_Angle =0;
  170. m_terminal_to_firecontrol->fan_sweep_azimuth_fu_setting_Angle =0;
  171. m_terminal_to_firecontrol->fan_sweep_pitch_setting_Angle =0;
  172. m_terminal_to_firecontrol->fan_sweep_pitch_fu_setting_Angle =0;
  173. m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_low =0;
  174. m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_high =0;
  175. m_terminal_to_firecontrol->quick_boot_pitch_coordinates_low =0;
  176. m_terminal_to_firecontrol->quick_boot_pitch_coordinates_high =0;
  177. m_terminal_to_firecontrol->left_track_speed_low =0;
  178. m_terminal_to_firecontrol->left_track_speed_high =0;
  179. m_terminal_to_firecontrol->right_track_speed_low =0;
  180. m_terminal_to_firecontrol->right_track_speed_high =0;
  181. m_terminal_to_firecontrol->forward_arm_speed =0;
  182. m_terminal_to_firecontrol->back_arm_speed =0;
  183. m_terminal_to_firecontrol->residual_low =0;
  184. m_terminal_to_firecontrol->residual_high =0;
  185. m_terminal_to_firecontrol->azimuth_correction =0;
  186. m_terminal_to_firecontrol->pitch_correction =0;
  187. m_terminal_to_firecontrol->delay_correction =0;
  188. m_terminal_to_firecontrol->light_aiming_control_word_4.all =0;
  189. m_terminal_to_firecontrol->light_aiming_control_word_5.all =0;
  190. m_terminal_to_firecontrol->servo_azimuth_rotation_speed_low =0x00;
  191. m_terminal_to_firecontrol->servo_azimuth_rotation_speed_high =0x00;
  192. m_terminal_to_firecontrol->servo_pitch_rotation_speed_low =0;
  193. m_terminal_to_firecontrol->servo_pitch_rotation_speed_high =0;
  194. m_terminal_to_firecontrol->firing_servo_start_low =0;
  195. m_terminal_to_firecontrol->firing_servo_start_high =0;
  196. m_terminal_to_firecontrol->firing_servo_stop_low =0;
  197. m_terminal_to_firecontrol->firing_servo_stop_high =0;
  198. m_terminal_to_firecontrol->firing_insurance_close_low =0;
  199. m_terminal_to_firecontrol->firing_insurance_close_high =0;
  200. m_terminal_to_firecontrol->firing_insurance_open_low =0;
  201. m_terminal_to_firecontrol->firing_insurance_open_high =0;;
  202. m_terminal_to_firecontrol->check =0;
  203. cout <<"initSendMessage-----------------------------------end"<< endl;}

4.4、Linux下的CMakeLists.txt

  1. cmake_minimum_required(VERSION 3.0.2)project(electro_optical_system)
  2. ## Compile as C++11, supported in ROS Kinetic and newer
  3. add_compile_options(-std=c++11)
  4. ## Find catkin macros and libraries
  5. ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
  6. ## is used, also find other catkin packages
  7. find_package(catkin REQUIRED COMPONENTS
  8. roscpp
  9. rospy
  10. std_msgs
  11. )
  12. ## System dependencies are found with CMake's conventions
  13. #find_package(Boost REQUIRED COMPONENTS system)find_package(OpenCV REQUIRED)
  14. ## Uncomment this if the package has a setup.py. This macro ensures
  15. ## modules and global scripts declared therein get installed
  16. ## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html#catkin_python_setup()
  17. ################################################
  18. ## Declare ROS messages, services and actions ##
  19. ################################################
  20. ## To declare and build messages, services or actions from within this
  21. ## package, follow these steps:
  22. ## * Let MSG_DEP_SET be the set of packages whose message types you use in
  23. ## your messages/services/actions(e.g. std_msgs, actionlib_msgs,...).
  24. ## * In the file package.xml:
  25. ## * add a build_depend tag for"message_generation"
  26. ## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
  27. ## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
  28. ## but can be declared for certainty nonetheless:
  29. ## * add a exec_depend tag for"message_runtime"
  30. ## * In this file(CMakeLists.txt):
  31. ## * add "message_generation" and every package in MSG_DEP_SET to
  32. ## find_package(catkin REQUIRED COMPONENTS ...)
  33. ## * add "message_runtime" and every package in MSG_DEP_SET to
  34. ## catkin_package(CATKIN_DEPENDS ...)
  35. ## * uncomment the add_*_files sections below as needed
  36. ## and list every .msg/.srv/.action file to be processed
  37. ## * uncomment the generate_messages entry below
  38. ## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
  39. ## Generate messages in the 'msg' folder
  40. #add_message_files(#FILES#Message1.msg#Message2.msg
  41. # )
  42. ## Generate services in the 'srv' folder
  43. #add_service_files(#FILES#Service1.srv#Service2.srv
  44. # )
  45. ## Generate actions in the 'action' folder
  46. #add_action_files(#FILES#Action1.action#Action2.action
  47. # )
  48. ## Generate added messages and services with any dependencies listed here
  49. #generate_messages(#DEPENDENCIES#std_msgs
  50. # )
  51. ################################################
  52. ## Declare ROS dynamic reconfigure parameters ##
  53. ################################################
  54. ## To declare and build dynamic reconfigure parameters within this
  55. ## package, follow these steps:
  56. ## * In the file package.xml:
  57. ## * add a build_depend and a exec_depend tag for"dynamic_reconfigure"
  58. ## * In this file(CMakeLists.txt):
  59. ## * add "dynamic_reconfigure" to
  60. ## find_package(catkin REQUIRED COMPONENTS ...)
  61. ## * uncomment the "generate_dynamic_reconfigure_options" section below
  62. ## and list every .cfg file to be processed
  63. ## Generate dynamic reconfigure parameters in the 'cfg' folder
  64. #generate_dynamic_reconfigure_options(#cfg/DynReconf1.cfg#cfg/DynReconf2.cfg
  65. # )
  66. ###################################
  67. ## catkin specific configuration ##
  68. ###################################
  69. ## The catkin_package macro generates cmake config files for your package
  70. ## Declare things to be passed to dependent projects
  71. ## INCLUDE_DIRS: uncomment this if your package contains header files
  72. ## LIBRARIES: libraries you create in this project that dependent projects also need
  73. ## CATKIN_DEPENDS: catkin_packages dependent projects also need
  74. ## DEPENDS: system dependencies of this project that dependent projects also need
  75. catkin_package(#INCLUDE_DIRS include#LIBRARIES electro_optical_system#CATKIN_DEPENDS roscpp rospy std_msgs#DEPENDS system_lib)
  76. ###########
  77. ## Build ##
  78. ###########
  79. ## Specify additional locations of header files
  80. ## Your package locations should be listed before other locations
  81. include_directories(#include
  82. ${catkin_INCLUDE_DIRS})
  83. ## Declare a C++ library
  84. #add_library(${PROJECT_NAME}#src/${PROJECT_NAME}/electro_optical_system.cpp
  85. # )
  86. ## Add cmake target dependencies of the library
  87. ## as an example, code may need to be generated before libraries
  88. ## either from message generation or dynamic reconfigure
  89. #add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
  90. ## Declare a C++ executable
  91. ## With catkin_make all packages are built within a single CMake context
  92. ## The recommended prefix ensures that target names across packages don't collide
  93. #add_executable(${PROJECT_NAME}_node src/electro_optical_system_node.cpp)
  94. ## Rename C++ executable without prefix
  95. ## The above recommended prefix causes long target names, the following renames the
  96. ## target back to the shorter version for ease of user use
  97. ## 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 "")
  98. ## Add cmake target dependencies of the executable
  99. ## same as for the library above
  100. #add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
  101. ## Specify libraries to link a library or executable target against
  102. #target_link_libraries(${PROJECT_NAME}_node
  103. # ${catkin_LIBRARIES}
  104. # )
  105. #############
  106. ## Install ##
  107. #############
  108. #allinstall targets should use catkin DESTINATION variables#See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
  109. ## Mark executable scripts(Python etc.)for installation
  110. ## in contrast to setup.py, you can choose the destination
  111. #catkin_install_python(PROGRAMS#scripts/my_python_script#DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
  112. # )
  113. ## Mark executables for installation
  114. ## 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}
  115. # )
  116. ## Mark libraries for installation
  117. ## 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}
  118. # )
  119. ## Mark cpp header files for installation
  120. #install(DIRECTORY include/${PROJECT_NAME}/#DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}#FILES_MATCHING PATTERN "*.h"#PATTERN ".svn"EXCLUDE
  121. # )
  122. ## Mark other files forinstallation(e.g. launch and bag files, etc.)#install(FILES
  123. # # myfile1
  124. # # myfile2
  125. #DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
  126. # )
  127. #############
  128. ## Testing ##
  129. #############
  130. ## Add gtest based cpp test target and link libraries
  131. #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()
  132. ## Add folders to be run by python nosetests
  133. #catkin_add_nosetests(test)add_executable(EOS src/main.cpp src/jsserialport.cpp src/CSerialPort/SerialPort.cpp src/CSerialPort/SerialPortInfo.cpp src/CSerialPort/SerialPortUnixBase.cpp src/CSerialPort/SerialPortBase.cpp src/CSerialPort/SerialPortInfoUnixBase.cpp src/CSerialPort/SerialPortInfoBase.cpp)target_link_libraries(EOS ${catkin_LIBRARIES}
  134. ${OpenCV_LIBS})add_dependencies(EOS ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
标签: c语言 c++ 开源

本文转载自: https://blog.csdn.net/u014597198/article/details/135415039
版权归原作者 沙振宇 所有, 如有侵权,请联系我们删除。

“【C/C++】开源串口库 CSerialPort 应用”的评论:

还没有评论