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

打印 上一主题 下一主题

主题 885|帖子 885|积分 2655

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_GUI
  2. CONFIG += SHASHIDI_GUI
  3. 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文件

  1. #ifdef SHASHIDI_GUI
  2. #include "mainwindow.h"
  3. #include <QApplication>
  4. #else
  5. #include "jsserialport.h"
  6. #include <iostream>
  7. using namespace std;
  8. #endif
  9. #ifdef SHASHIDI_GUI
  10. int main(int argc, char *argv[])
  11. {
  12.     QApplication a(argc, argv);
  13.     MainWindow w;
  14.     w.show();
  15.     return a.exec();
  16. #else
  17. int main()
  18. {
  19.     JSSerialPort js;
  20.     js.doWork();
  21.     return 0;
  22. #endif
  23. }
复制代码
4.3、SSerialPort类

4.3.1、头文件

  1. #ifndef SSERIALPORT_H
  2. #define SSERIALPORT_H
  3. #include <iostream>
  4. #include "CSerialPort/SerialPort.h"
  5. #include "CSerialPort/SerialPortInfo.h"
  6. #include "CommonSerialPort.h"
  7. using namespace std;
  8. using namespace itas109;
  9. class SSerialPort : public CSerialPortListener
  10. {
  11. public:
  12.     SSerialPort();
  13.     // 工作
  14.     void doWork();
  15.     // 校验
  16.     void doCheck(char receiveBuf[], int receiveSize);
  17.     // 串口信号
  18.     bool m_flag_serialport = false;
  19.     // 发出消息
  20.     void sendMessage();
  21.     // 读取全部数据
  22.     void readAllData();
  23. private:
  24.     // 获取端口列表
  25.     vector<SerialPortInfo> getSerialPortList();
  26.     // 打开串口
  27.     bool openSerialPort(const char *portName,
  28.                         int baudRate = 9600,
  29.                         itas109::Parity parity = itas109::Parity::ParityNone,
  30.                         itas109::DataBits dataBits = itas109::DataBits::DataBits8,
  31.                         itas109::StopBits stopbits = itas109::StopBits::StopOne,
  32.                         itas109::FlowControl flowControl = itas109::FlowControl::FlowNone,
  33.                         unsigned int readBufferSize = 4096, int timeout = 0);
  34.     // 关闭串口
  35.     void closeSerialPort();
  36.     // 收到消息
  37.     void onReadEvent(const char *portName, unsigned int readBufferLen);
  38.     // 设置串口模式  同步\异步
  39.     void setSync(bool isSync);
  40.     // 设置DTR
  41.     void setDTR(bool isDTR);
  42.     // 设置RTS
  43.     void setRTS(bool isRTS);
  44.     // 初始化串口
  45.     void initSerialPort();
  46.     // 初始化发送消息
  47.     void initSendMessage();
  48. private:
  49.     CSerialPort m_SerialPort;   // 串口
  50.     struct TERMINAL_TO_FIRECONTROL *m_terminal_to_firecontrol;    // 终端到火控
  51.     struct FIRECONTROL_TO_TERMINAL *m_firecontrol_to_terminal;    // 火控到终端
  52.     int m_packet_frame_count = 0;   // 报文帧号累计
  53.     int m_buffer_size = 2048;
  54.     int m_frame_length = 34;
  55.     unsigned char m_firstChar = 161;    // 0xA1
  56.     unsigned char m_secondChar = 177;   // 0xB1
  57.     unsigned char m_thirdChar = 50;     // 0x32
  58.     int m_tx = 0;
  59.     int m_rx = 0;
  60. };
  61. #endif // SSERIALPORT_H
复制代码
4.3.2、源文件

  1. #include "sserialport.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. SSerialPort::SSerialPort()
  5. {
  6. }
  7. void SSerialPort::doWork()
  8. {
  9.     cout << "doWork: initSerialPort" << endl;
  10.     initSerialPort();
  11.     cout << "doWork: initSendMessage" << endl;
  12.     initSendMessage();
  13.     cout << "doWork: sendMessage" << endl;
  14.     sendMessage();
  15.     cout << "doWork: readAllData" << endl;
  16.     readAllData();
  17. }
  18. void SSerialPort::doCheck(char receiveBuf[], int receiveSize)
  19. {
  20.     cout << "doCheck: start" << receiveSize << endl;
  21.     for(int i = 0; i < receiveSize; i++) {
  22.         if( ( receiveSize - i ) < m_frame_length ) { //不足一帧则退出
  23.             break;
  24.         }
  25.         cout << "doCheck1: " << receiveBuf[i] << endl;
  26.         if( (receiveBuf[i] == m_secondChar) && (receiveBuf[i+1] == m_firstChar) ) { //发现帧头,取出一帧校验
  27.             cout << "doCheck: " << receiveBuf[i] << endl;
  28.             string aa(receiveBuf);
  29.             cout << "doCheck: " << aa << endl;
  30.             string tmp_s(aa, i, m_frame_length);
  31.             cout << "doCheck: " << tmp_s << endl;
  32.             int data = 0;
  33.             for(int i = 0; i < (m_frame_length - 1); i++) {
  34.                 data = data + tmp_s[i];
  35.             }
  36.             unsigned char data1 = data & 0x7f;
  37.             if(data1 != tmp_s[m_frame_length - 1]) {
  38.                 i = i + 1;
  39.                 continue;
  40.             }
  41.             //校验通过,取值
  42.             m_firecontrol_to_terminal->send_address = tmp_s[0];
  43.             m_firecontrol_to_terminal->receive_address = tmp_s[1];
  44.             m_firecontrol_to_terminal->packet_length = tmp_s[2];
  45.             m_firecontrol_to_terminal->packet_frame_number_low = tmp_s[3];
  46.             m_firecontrol_to_terminal->packet_frame_number_high = tmp_s[4];
  47.             m_firecontrol_to_terminal->light_aiming_status_word_1.all = tmp_s[5];
  48.             m_firecontrol_to_terminal->light_aiming_status_word_2.all = tmp_s[6];
  49.             m_firecontrol_to_terminal->laser_ranging_distance_low = tmp_s[7];
  50.             m_firecontrol_to_terminal->laser_ranging_distance_high = tmp_s[8];
  51.             m_firecontrol_to_terminal->servo_status_word_1.all = tmp_s[9];
  52.             m_firecontrol_to_terminal->servo_status_word_2.all = tmp_s[10];
  53.             m_firecontrol_to_terminal->speed_of_chassis = tmp_s[11];
  54.             m_firecontrol_to_terminal->chassis_battery_power = tmp_s[12];
  55.             m_firecontrol_to_terminal->front_flip_arm_angle_low = tmp_s[13];
  56.             m_firecontrol_to_terminal->front_flip_arm_angle_high = tmp_s[14];
  57.             m_firecontrol_to_terminal->back_flip_arm_angle_low = tmp_s[15];
  58.             m_firecontrol_to_terminal->back_flip_arm_angle_high = tmp_s[16];
  59.             m_firecontrol_to_terminal->relay_status_word.all = tmp_s[17];
  60.             m_firecontrol_to_terminal->light_azimuth_position_low = tmp_s[18];
  61.             m_firecontrol_to_terminal->light_azimuth_position_high = tmp_s[19];
  62.             m_firecontrol_to_terminal->light_pitch_position_low = tmp_s[20];
  63.             m_firecontrol_to_terminal->light_pitch_position_high = tmp_s[21];
  64.             m_firecontrol_to_terminal->weapon_azimuth_position_low = tmp_s[22];
  65.             m_firecontrol_to_terminal->weapon_azimuth_position_high = tmp_s[23];
  66.             m_firecontrol_to_terminal->weapon_pitch_position_low = tmp_s[24];
  67.             m_firecontrol_to_terminal->weapon_pitch_position_high = tmp_s[25];
  68.             m_firecontrol_to_terminal->platform_pitch = tmp_s[26];
  69.             m_firecontrol_to_terminal->platform_roll_angle = tmp_s[27];
  70.             m_firecontrol_to_terminal->residual_bullet_low = tmp_s[28];
  71.             m_firecontrol_to_terminal->residual_bullet_high = tmp_s[29];
  72.             m_firecontrol_to_terminal->rsve1 = tmp_s[30];
  73.             m_firecontrol_to_terminal->rsve2 = tmp_s[31];
  74.             m_firecontrol_to_terminal->rsve3 = tmp_s[32];
  75.             m_firecontrol_to_terminal->check = tmp_s[33];
  76.             cout << "doCheck: send_address" << tmp_s << endl;
  77. //            emit sig_check(data1);
  78.         }
  79.     }
  80. }
  81. vector<SerialPortInfo> SSerialPort::getSerialPortList()
  82. {
  83.     vector<SerialPortInfo> portNameList = CSerialPortInfo::availablePortInfos();
  84.     for (size_t i = 0; i < portNameList.size(); i++) {
  85.         cout << "getSerialPortList:" << portNameList[i].portName << endl;
  86.     }
  87.     if ( portNameList.size() == 0 ) {
  88.         cout << "getSerialPortList is Null" << endl;
  89.     }
  90.     return portNameList;
  91. }
  92. bool SSerialPort::openSerialPort(const char *portName, int baudRate, Parity parity, DataBits dataBits, StopBits stopbits, FlowControl flowControl, unsigned int readBufferSize, int timeout)
  93. {
  94.     cout << "openSerialPort-----------------------------------start" << endl;
  95.     bool isOpen = false;
  96.     if(getSerialPortList().size() > 0) {
  97.         m_SerialPort.init(portName, baudRate, parity, dataBits, stopbits, flowControl, readBufferSize);
  98.         cout << "portName:" << portName << endl;
  99.         cout << "baudRate:" << baudRate << endl;
  100.         cout << "parity:" << parity << endl;
  101.         cout << "dataBits:" << dataBits << endl;
  102.         cout << "stopbits:" << stopbits << endl;
  103.         cout << "flowControl:" << flowControl << endl;
  104.         cout << "readBufferSize:" << readBufferSize << endl;
  105.         m_SerialPort.setReadIntervalTimeout(timeout);
  106.         isOpen = m_SerialPort.open();
  107.         if(!isOpen) {
  108.             cerr << "open port error" << m_SerialPort.getLastError() << m_SerialPort.getLastErrorMsg() << endl;
  109.         }
  110.     } else {
  111.         cerr << "This Computer no avaiable port!" << endl;
  112.     }
  113.     cout << "openSerialPort-----------------------------------end" << endl;
  114.     return isOpen;
  115. }
  116. void SSerialPort::closeSerialPort()
  117. {
  118.     m_SerialPort.close();
  119. }
  120. void SSerialPort::onReadEvent(const char *portName, unsigned int readBufferLen)
  121. {
  122.     cout << "onReadEvent:" << portName << readBufferLen << endl;
  123.     if(readBufferLen > 0) {
  124.         unsigned long recLen = 0;
  125.         char * str = NULL;
  126.         str = new char[readBufferLen];
  127.         recLen = m_SerialPort.readData(str, readBufferLen);
  128.         if(recLen > 0) {
  129.             // TODO: 中文需要由两个字符拼接,否则显示为空""
  130.             cout << "onReadEvent:" << portName << str << recLen << endl;
  131.             doCheck(str, recLen);
  132.         } else {
  133.         }
  134.         if(str) {
  135.             delete[] str;
  136.             str = NULL;
  137.         }
  138.     }
  139. }
  140. void SSerialPort::sendMessage()
  141. {
  142.     char message[50];
  143.     if(m_SerialPort.isOpen()) {
  144.         // 伺服旋转限制
  145.         // 高位低位赋值
  146.         m_terminal_to_firecontrol->packet_frame_number_low = m_packet_frame_count & 0x00ff;
  147.         m_terminal_to_firecontrol->packet_frame_number_high = (m_packet_frame_count >> 8) & 0x00ff;
  148.         message[0] = m_firstChar;
  149.         message[1] = m_secondChar;
  150.         message[2] = m_thirdChar;
  151.         cout << "m_terminal_to_firecontrol->packet_frame_number_low: " << m_terminal_to_firecontrol->packet_frame_number_low << endl;
  152.         message[3] = m_terminal_to_firecontrol->packet_frame_number_low;
  153.         message[4] = m_terminal_to_firecontrol->packet_frame_number_high;
  154.         message[5] = m_terminal_to_firecontrol->light_aiming_control_word_1.all;//变化
  155.         message[6] = m_terminal_to_firecontrol->light_aiming_control_word_2.all;
  156.         message[7] = m_terminal_to_firecontrol->light_aiming_control_word_3.all;
  157.         message[8] = m_terminal_to_firecontrol->servo_control_word_1.all;
  158.         message[9] = m_terminal_to_firecontrol->servo_control_word_2.all;
  159.         message[10] = m_terminal_to_firecontrol->chassis_control_word_1.all;
  160.         message[11] = m_terminal_to_firecontrol->relay_control_word.all;
  161.         message[12] = m_terminal_to_firecontrol->light_azimuth_rotation_speed_low;
  162.         message[13] = m_terminal_to_firecontrol->light_azimuth_rotation_speed_high;
  163.         message[14] = m_terminal_to_firecontrol->light_pitch_rotation_speed_low;
  164.         message[15] = m_terminal_to_firecontrol->light_pitch_rotation_speed_high;
  165.         message[16] = m_terminal_to_firecontrol->fan_sweep_azimuth_setting_Angle;
  166.         message[17] = m_terminal_to_firecontrol->fan_sweep_azimuth_fu_setting_Angle;
  167.         message[18] = m_terminal_to_firecontrol->fan_sweep_pitch_setting_Angle;
  168.         message[19] = m_terminal_to_firecontrol->fan_sweep_pitch_fu_setting_Angle;
  169.         message[20] = m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_low;
  170.         message[21] = m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_high;
  171.         message[22] = m_terminal_to_firecontrol->quick_boot_pitch_coordinates_low;
  172.         message[23] = m_terminal_to_firecontrol->quick_boot_pitch_coordinates_high;
  173.         message[24] = m_terminal_to_firecontrol->left_track_speed_low;
  174.         message[25] = m_terminal_to_firecontrol->left_track_speed_high;
  175.         message[26] = m_terminal_to_firecontrol->right_track_speed_low;
  176.         message[27] = m_terminal_to_firecontrol->right_track_speed_high;
  177.         message[28] = m_terminal_to_firecontrol->forward_arm_speed;
  178.         message[29] = m_terminal_to_firecontrol->back_arm_speed;
  179.         message[30] = m_terminal_to_firecontrol->residual_low;
  180.         message[31] = m_terminal_to_firecontrol->residual_high;
  181.         message[32] = m_terminal_to_firecontrol->azimuth_correction;
  182.         message[33] = m_terminal_to_firecontrol->pitch_correction;
  183.         message[34] = m_terminal_to_firecontrol->delay_correction;
  184.         message[35] = m_terminal_to_firecontrol->light_aiming_control_word_4.all;
  185.         message[36] = m_terminal_to_firecontrol->light_aiming_control_word_5.all;
  186.         message[37] = m_terminal_to_firecontrol->servo_azimuth_rotation_speed_low;
  187.         message[38] = m_terminal_to_firecontrol->servo_azimuth_rotation_speed_high;
  188.         message[39] = m_terminal_to_firecontrol->servo_pitch_rotation_speed_low;
  189.         message[40] = m_terminal_to_firecontrol->servo_pitch_rotation_speed_high;
  190.         message[41] = m_terminal_to_firecontrol->firing_servo_start_low;
  191.         message[42] = m_terminal_to_firecontrol->firing_servo_start_high;
  192.         message[43] = m_terminal_to_firecontrol->firing_servo_stop_low;
  193.         message[44] = m_terminal_to_firecontrol->firing_servo_stop_high;
  194.         message[45] = m_terminal_to_firecontrol->firing_insurance_close_low;
  195.         message[46] = m_terminal_to_firecontrol->firing_insurance_close_high;
  196.         message[47] = m_terminal_to_firecontrol->firing_insurance_open_low;
  197.         message[48] = m_terminal_to_firecontrol->firing_insurance_open_high;
  198.         int data = 0;
  199.         for( int i = 0; i < 49; i++ ) {
  200.             cout << "sendMessage: " << message[i] << endl;
  201.             data = data + message[i];
  202.         }
  203.         message[49] = data & 0x7f;
  204.         int length = sizeof(message)/sizeof(char);
  205.         if( !m_flag_serialport ) {
  206.             // 支持中文并获取正确的长度
  207.             m_SerialPort.writeData(message, length);
  208.         } else {
  209.         }
  210.         if( m_packet_frame_count >= 0xffff ) {
  211.             m_packet_frame_count = 1;
  212.         } else {
  213.             m_packet_frame_count++;
  214.         }
  215.         cout << "sendMessage:" << message << " length: " << length << endl;
  216.     } else {
  217.         cerr << "please open serial port first!" << endl;
  218.     }
  219. }
  220. void SSerialPort::readAllData()
  221. {
  222.     int recLen = 0;
  223.     char str[4096] = {0};
  224.     recLen = m_SerialPort.readAllData(str);
  225.     if(recLen > 0) {
  226.         // TODO: 中文需要由两个字符拼接,否则显示为空""
  227.         cout << "readAllData:" << str << " length: " << recLen << endl;
  228.         doCheck(str, recLen);
  229.     } else {
  230.     }
  231. }
  232. void SSerialPort::setSync(bool isSync)
  233. {
  234.     if(isSync) {
  235.         m_SerialPort.setOperateMode(itas109::SynchronousOperate);
  236.     } else {
  237.         m_SerialPort.setOperateMode(itas109::AsynchronousOperate);
  238.     }
  239. }
  240. void SSerialPort::setDTR(bool isDTR)
  241. {
  242.     m_SerialPort.setDtr(isDTR);
  243. }
  244. void SSerialPort::setRTS(bool isRTS)
  245. {
  246.     m_SerialPort.setRts(isRTS);
  247. }
  248. void SSerialPort::initSerialPort()
  249. {
  250. #ifdef WIN32
  251.     this->openSerialPort("COM3", 115200, itas109::ParityNone,
  252.                          itas109::DataBits8, itas109::StopOne);
  253. #else
  254.     this->openSerialPort("/dev/ttyUSB0", 115200, itas109::ParityNone,
  255.                          itas109::DataBits8, itas109::StopOne);
  256. #endif
  257. }
  258. void SSerialPort::initSendMessage()
  259. {
  260.     cout << "initSendMessage-----------------------------------start" << endl;
  261.     m_packet_frame_count = 1;
  262.     m_terminal_to_firecontrol = (struct TERMINAL_TO_FIRECONTROL*)malloc(sizeof(struct TERMINAL_TO_FIRECONTROL));
  263.     m_firecontrol_to_terminal = (struct FIRECONTROL_TO_TERMINAL*)malloc(sizeof(struct FIRECONTROL_TO_TERMINAL));
  264.     m_terminal_to_firecontrol->send_address = m_firstChar;
  265.     m_terminal_to_firecontrol->receive_address = m_secondChar;
  266.     m_terminal_to_firecontrol->packet_length = m_thirdChar;
  267.     m_terminal_to_firecontrol->packet_frame_number_low = 1;
  268.     std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(m_terminal_to_firecontrol->packet_frame_number_low)<<endl;
  269.     m_terminal_to_firecontrol->packet_frame_number_high = 0;
  270.     m_terminal_to_firecontrol->light_aiming_control_word_1.all = 0x01;
  271.     m_terminal_to_firecontrol->light_aiming_control_word_2.all = 0x00;
  272.     m_terminal_to_firecontrol->light_aiming_control_word_3.all = 0x00;
  273.     m_terminal_to_firecontrol->servo_control_word_1.all = 0x09;
  274.     m_terminal_to_firecontrol->servo_control_word_2.all = 0x02;
  275.     m_terminal_to_firecontrol->chassis_control_word_1.all = 0x00;
  276.     m_terminal_to_firecontrol->relay_control_word.all = 0x00;
  277.     m_terminal_to_firecontrol->light_azimuth_rotation_speed_low = 0x00;
  278.     m_terminal_to_firecontrol->light_azimuth_rotation_speed_high = 0X00;
  279.     m_terminal_to_firecontrol->light_pitch_rotation_speed_low = 0;
  280.     m_terminal_to_firecontrol->light_pitch_rotation_speed_high = 0;
  281.     m_terminal_to_firecontrol->fan_sweep_azimuth_setting_Angle = 0;
  282.     m_terminal_to_firecontrol->fan_sweep_azimuth_fu_setting_Angle = 0;
  283.     m_terminal_to_firecontrol->fan_sweep_pitch_setting_Angle = 0;
  284.     m_terminal_to_firecontrol->fan_sweep_pitch_fu_setting_Angle = 0;
  285.     m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_low = 0;
  286.     m_terminal_to_firecontrol->quick_boot_azimuth_coordinates_high = 0;
  287.     m_terminal_to_firecontrol->quick_boot_pitch_coordinates_low = 0;
  288.     m_terminal_to_firecontrol->quick_boot_pitch_coordinates_high = 0;
  289.     m_terminal_to_firecontrol->left_track_speed_low = 0;
  290.     m_terminal_to_firecontrol->left_track_speed_high = 0;
  291.     m_terminal_to_firecontrol->right_track_speed_low = 0;
  292.     m_terminal_to_firecontrol->right_track_speed_high = 0;
  293.     m_terminal_to_firecontrol->forward_arm_speed = 0;
  294.     m_terminal_to_firecontrol->back_arm_speed = 0;
  295.     m_terminal_to_firecontrol->residual_low = 0;
  296.     m_terminal_to_firecontrol->residual_high = 0;
  297.     m_terminal_to_firecontrol->azimuth_correction = 0;
  298.     m_terminal_to_firecontrol->pitch_correction = 0;
  299.     m_terminal_to_firecontrol->delay_correction = 0;
  300.     m_terminal_to_firecontrol->light_aiming_control_word_4.all = 0;
  301.     m_terminal_to_firecontrol->light_aiming_control_word_5.all = 0;
  302.     m_terminal_to_firecontrol->servo_azimuth_rotation_speed_low = 0x00;
  303.     m_terminal_to_firecontrol->servo_azimuth_rotation_speed_high = 0x00;
  304.     m_terminal_to_firecontrol->servo_pitch_rotation_speed_low = 0;
  305.     m_terminal_to_firecontrol->servo_pitch_rotation_speed_high = 0;
  306.     m_terminal_to_firecontrol->firing_servo_start_low = 0;
  307.     m_terminal_to_firecontrol->firing_servo_start_high = 0;
  308.     m_terminal_to_firecontrol->firing_servo_stop_low = 0;
  309.     m_terminal_to_firecontrol->firing_servo_stop_high = 0;
  310.     m_terminal_to_firecontrol->firing_insurance_close_low = 0;
  311.     m_terminal_to_firecontrol->firing_insurance_close_high = 0;
  312.     m_terminal_to_firecontrol->firing_insurance_open_low = 0;
  313.     m_terminal_to_firecontrol->firing_insurance_open_high = 0;;
  314.     m_terminal_to_firecontrol->check = 0;
  315.     cout << "initSendMessage-----------------------------------end" << endl;
  316. }
复制代码
4.4、Linux下的CMakeLists.txt

  1. cmake_minimum_required(VERSION 3.0.2)
  2. project(electro_optical_system)
  3. ## Compile as C++11, supported in ROS Kinetic and newer
  4. add_compile_options(-std=c++11)
  5. ## Find catkin macros and libraries
  6. ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
  7. ## is used, also find other catkin packages
  8. find_package(catkin REQUIRED COMPONENTS
  9.   roscpp
  10.   rospy
  11.   std_msgs
  12. )
  13. ## System dependencies are found with CMake's conventions
  14. # find_package(Boost REQUIRED COMPONENTS system)
  15. find_package(OpenCV REQUIRED)
  16. ## Uncomment this if the package has a setup.py. This macro ensures
  17. ## modules and global scripts declared therein get installed
  18. ## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
  19. # catkin_python_setup()
  20. ################################################
  21. ## Declare ROS messages, services and actions ##
  22. ################################################
  23. ## To declare and build messages, services or actions from within this
  24. ## package, follow these steps:
  25. ## * Let MSG_DEP_SET be the set of packages whose message types you use in
  26. ##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
  27. ## * In the file package.xml:
  28. ##   * add a build_depend tag for "message_generation"
  29. ##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
  30. ##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
  31. ##     but can be declared for certainty nonetheless:
  32. ##     * add a exec_depend tag for "message_runtime"
  33. ## * In this file (CMakeLists.txt):
  34. ##   * add "message_generation" and every package in MSG_DEP_SET to
  35. ##     find_package(catkin REQUIRED COMPONENTS ...)
  36. ##   * add "message_runtime" and every package in MSG_DEP_SET to
  37. ##     catkin_package(CATKIN_DEPENDS ...)
  38. ##   * uncomment the add_*_files sections below as needed
  39. ##     and list every .msg/.srv/.action file to be processed
  40. ##   * uncomment the generate_messages entry below
  41. ##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
  42. ## Generate messages in the 'msg' folder
  43. # add_message_files(
  44. #   FILES
  45. #   Message1.msg
  46. #   Message2.msg
  47. # )
  48. ## Generate services in the 'srv' folder
  49. # add_service_files(
  50. #   FILES
  51. #   Service1.srv
  52. #   Service2.srv
  53. # )
  54. ## Generate actions in the 'action' folder
  55. # add_action_files(
  56. #   FILES
  57. #   Action1.action
  58. #   Action2.action
  59. # )
  60. ## Generate added messages and services with any dependencies listed here
  61. # generate_messages(
  62. #   DEPENDENCIES
  63. #   std_msgs
  64. # )
  65. ################################################
  66. ## Declare ROS dynamic reconfigure parameters ##
  67. ################################################
  68. ## To declare and build dynamic reconfigure parameters within this
  69. ## package, follow these steps:
  70. ## * In the file package.xml:
  71. ##   * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
  72. ## * In this file (CMakeLists.txt):
  73. ##   * add "dynamic_reconfigure" to
  74. ##     find_package(catkin REQUIRED COMPONENTS ...)
  75. ##   * uncomment the "generate_dynamic_reconfigure_options" section below
  76. ##     and list every .cfg file to be processed
  77. ## Generate dynamic reconfigure parameters in the 'cfg' folder
  78. # generate_dynamic_reconfigure_options(
  79. #   cfg/DynReconf1.cfg
  80. #   cfg/DynReconf2.cfg
  81. # )
  82. ###################################
  83. ## catkin specific configuration ##
  84. ###################################
  85. ## The catkin_package macro generates cmake config files for your package
  86. ## Declare things to be passed to dependent projects
  87. ## INCLUDE_DIRS: uncomment this if your package contains header files
  88. ## LIBRARIES: libraries you create in this project that dependent projects also need
  89. ## CATKIN_DEPENDS: catkin_packages dependent projects also need
  90. ## DEPENDS: system dependencies of this project that dependent projects also need
  91. catkin_package(
  92. #  INCLUDE_DIRS include
  93. #  LIBRARIES electro_optical_system
  94. #  CATKIN_DEPENDS roscpp rospy std_msgs
  95. #  DEPENDS system_lib
  96. )
  97. ###########
  98. ## Build ##
  99. ###########
  100. ## Specify additional locations of header files
  101. ## Your package locations should be listed before other locations
  102. include_directories(
  103. # include
  104.   ${catkin_INCLUDE_DIRS}
  105. )
  106. ## Declare a C++ library
  107. # add_library(${PROJECT_NAME}
  108. #   src/${PROJECT_NAME}/electro_optical_system.cpp
  109. # )
  110. ## Add cmake target dependencies of the library
  111. ## as an example, code may need to be generated before libraries
  112. ## either from message generation or dynamic reconfigure
  113. # add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
  114. ## Declare a C++ executable
  115. ## With catkin_make all packages are built within a single CMake context
  116. ## The recommended prefix ensures that target names across packages don't collide
  117. # add_executable(${PROJECT_NAME}_node src/electro_optical_system_node.cpp)
  118. ## Rename C++ executable without prefix
  119. ## The above recommended prefix causes long target names, the following renames the
  120. ## target back to the shorter version for ease of user use
  121. ## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
  122. # set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
  123. ## Add cmake target dependencies of the executable
  124. ## same as for the library above
  125. # add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
  126. ## Specify libraries to link a library or executable target against
  127. # target_link_libraries(${PROJECT_NAME}_node
  128. #   ${catkin_LIBRARIES}
  129. # )
  130. #############
  131. ## Install ##
  132. #############
  133. # all install targets should use catkin DESTINATION variables
  134. # See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
  135. ## Mark executable scripts (Python etc.) for installation
  136. ## in contrast to setup.py, you can choose the destination
  137. # catkin_install_python(PROGRAMS
  138. #   scripts/my_python_script
  139. #   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
  140. # )
  141. ## Mark executables for installation
  142. ## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
  143. # install(TARGETS ${PROJECT_NAME}_node
  144. #   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
  145. # )
  146. ## Mark libraries for installation
  147. ## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
  148. # install(TARGETS ${PROJECT_NAME}
  149. #   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  150. #   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  151. #   RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
  152. # )
  153. ## Mark cpp header files for installation
  154. # install(DIRECTORY include/${PROJECT_NAME}/
  155. #   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
  156. #   FILES_MATCHING PATTERN "*.h"
  157. #   PATTERN ".svn" EXCLUDE
  158. # )
  159. ## Mark other files for installation (e.g. launch and bag files, etc.)
  160. # install(FILES
  161. #   # myfile1
  162. #   # myfile2
  163. #   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
  164. # )
  165. #############
  166. ## Testing ##
  167. #############
  168. ## Add gtest based cpp test target and link libraries
  169. # catkin_add_gtest(${PROJECT_NAME}-test test/test_electro_optical_system.cpp)
  170. # if(TARGET ${PROJECT_NAME}-test)
  171. #   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
  172. # endif()
  173. ## Add folders to be run by python nosetests
  174. # catkin_add_nosetests(test)
  175. 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)
  176. target_link_libraries(EOS ${catkin_LIBRARIES}
  177.                                 ${OpenCV_LIBS})
  178. add_dependencies(EOS ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

愛在花開的季節

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表