当前位置:   article > 正文

CSerialPort教程4.3.x (5) - CSerialPort在cmake中的使用_vs安装cserialport库

vs安装cserialport库

CSerialPort教程4.3.x (5) - CSerialPort在cmake中的使用

环境:

系统:windows 10 64位
QT: 5.12.9(5.9及以上版本支持cmake构建)
MFC: vs2008
  • 1
  • 2
  • 3

前言

CSerialPort项目是一个基于C/C++的轻量级开源跨平台串口类库,可以轻松实现跨平台多操作系统的串口读写,同时还支持C#, Java, Python, Node.js等。

CSerialPort项目的开源协议自 V3.0.0.171216 版本后采用GNU Lesser General Public License v3.0

为了让开发者更好的使用CSerialPort进行开发,特编写基于4.3.x版本的CSerialPort教程系列。

CSerialPort项目地址:

1. cmake构建console控制台的CSerialPort项目

$ cd CommConsole
$ git clone https://github.com/itas109/CSerialPort
  • 1
  • 2

目录结构如下:

CommConsole $ tree
.
+--- CMakeLists.txt
+--- CSerialPort
|   +--- include
|   |   +--- CSerialPort
|   |   |   +--- SerialPort.h
|   |   |   +--- SerialPortInfo.h
|   +--- src
|   |   +--- SerialPort.cpp
|   |   +--- SerialPortBase.cpp
|   |   +--- SerialPortInfo.cpp
|   |   +--- SerialPortInfoBase.cpp
|   |   +--- SerialPortInfoWinBase.cpp
|   |   +--- SerialPortWinBase.cpp
+--- main.cpp
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

main.cpp

#include <iostream>

#include "CSerialPort/SerialPort.h"
#include "CSerialPort/SerialPortInfo.h"

#include <vector>
using namespace itas109;
using namespace std;

class MyListener : public CSerialPortListener
{
public:
    MyListener(CSerialPort *sp)
        : p_sp(sp){};

    void onReadEvent(const char *portName, unsigned int readBufferLen)
    {
        if (readBufferLen > 0)
        {
            char *data = new char[readBufferLen + 1]; // '\0'

            if (data)
            {
                // read
                int recLen = p_sp->readData(data, readBufferLen);

                if (recLen > 0)
                {
                    data[recLen] = '\0';
                    std::cout << portName << ", Length: " << recLen << ", Str: " << data << std::endl;
                }

                delete[] data;
                data = NULL;
            }
        }
    };

private:
    CSerialPort *p_sp;
};

int main()
{
    CSerialPort sp;
    MyListener listener(&sp);

    std::cout << "Version : " << sp.getVersion() << std::endl << std::endl;

    vector<SerialPortInfo> m_availablePortsList = CSerialPortInfo::availablePortInfos();

    if (0 == m_availablePortsList.size())
    {
        std::cout << "No valid port" << std::endl;
        return 0;
    }

    sp.init(m_availablePortsList[0].portName, // windows:COM1 Linux:/dev/ttyS0
            itas109::BaudRate9600,            // baudrate
            itas109::ParityNone,              // parity
            itas109::DataBits8,               // data bit
            itas109::StopOne,                 // stop bit
            itas109::FlowNone,                // flow
            4096                              // read buffer size
    );
    sp.setReadIntervalTimeout(0); // read interval timeout 0ms
    // sp.setOperateMode(itas109::SynchronousOperate);

    sp.open();

    std::cout << "Open " << m_availablePortsList[0].portName << (sp.isOpen() ? " Success. " : " Failed. ");
    std::cout << "Code: " << sp.getLastError() << ", Message: " << sp.getLastErrorMsg() << std::endl;

    // 绑定接收函数
    sp.connectReadEvent(&listener);

    // 写入数据
    sp.writeData("itas109", 7);

    for (;;)
        ;

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)

project(CommConsole)

if(APPLE)
    find_library(IOKIT_LIBRARY IOKit)
    find_library(FOUNDATION_LIBRARY Foundation)
endif()

include_directories(CSerialPort/include)
file(GLOB_RECURSE COMMON_SOURCES CSerialPort/src/SerialPort.cpp CSerialPort/src/SerialPortBase.cpp CSerialPort/src/SerialPortInfo.cpp CSerialPort/src/SerialPortInfoBase.cpp)
if (CMAKE_HOST_WIN32)
    file(GLOB_RECURSE OS_ABOUT_SOURCES CSerialPort/src/SerialPortInfoWinBase.cpp CSerialPort/src/SerialPortWinBase.cpp)
elseif (CMAKE_HOST_UNIX)
    file(GLOB_RECURSE OS_ABOUT_SOURCES CSerialPort/src/SerialPortInfoUnixBase.cpp CSerialPort/src/SerialPortUnixBase.cpp)
endif ()	

add_executable( ${PROJECT_NAME} main.cpp ${COMMON_SOURCES} ${OS_ABOUT_SOURCES})

if (WIN32)
	target_link_libraries( ${PROJECT_NAME} setupapi )
elseif (APPLE)
    target_link_libraries( ${PROJECT_NAME} ${FOUNDATION_LIBRARY} ${IOKIT_LIBRARY})
elseif (UNIX)
	target_link_libraries( ${PROJECT_NAME} pthread )
endif ()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

2. cmake构建QT的CSerialPort项目

新建一个QT项目目,解决方案名称为CommQT

【文件】-【新建文件或项目】-【Application(Qt)】-【Qt Widgets Application】-【choose…】-【名称: CommQT】-【Build system: CMake】

CommQT解决方案目录下载CSerialPort源码

$ cd CommQT
$ git clone https://github.com/itas109/CSerialPort
  • 1
  • 2

目录结构如下:

D:/CommQT $ tree
.
+--- CMakeLists.txt
+--- CSerialPort
|   +--- include
|   |   +--- CSerialPort
|   |   |   +--- SerialPort.h
|   |   |   +--- SerialPortInfo.h
|   +--- src
|   |   +--- SerialPort.cpp
|   |   +--- SerialPortBase.cpp
|   |   +--- SerialPortInfo.cpp
|   |   +--- SerialPortInfoBase.cpp
|   |   +--- SerialPortInfoUnixBase.cpp
|   |   +--- SerialPortInfoWinBase.cpp
|   |   +--- SerialPortUnixBase.cpp
|   |   +--- SerialPortWinBase.cpp
+--- main.cpp
+--- mainwindow.cpp
+--- mainwindow.h
+--- mainwindow.ui
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

本节重点说明CMakeLists.txt文件,其他代码详见qt使用章节。

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)

project(CommQT LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Widgets REQUIRED)

set(CSerialPortRootPath "${PROJECT_SOURCE_DIR}/CSerialPort")
include_directories(${CSerialPortRootPath}/include)
list(APPEND CSerialPortSourceFiles ${CSerialPortRootPath}/src/SerialPort.cpp ${CSerialPortRootPath}/src/SerialPortBase.cpp ${CSerialPortRootPath}/src/SerialPortInfo.cpp ${CSerialPortRootPath}/src/SerialPortInfoBase.cpp)
if (WIN32)
    list(APPEND CSerialPortSourceFiles ${CSerialPortRootPath}/src/SerialPortInfoWinBase.cpp ${CSerialPortRootPath}/src/SerialPortWinBase.cpp)
else (UNIX)
    list(APPEND CSerialPortSourceFiles ${CSerialPortRootPath}/src/SerialPortInfoUnixBase.cpp ${CSerialPortRootPath}/src/SerialPortUnixBase.cpp)
endif ()

add_executable(${PROJECT_NAME}
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
    ${CSerialPortSourceFiles} # add by itas109
)

target_link_libraries(CommQT Qt5::Widgets)

if (WIN32)
    # for function availableFriendlyPorts
    target_link_libraries( ${PROJECT_NAME} setupapi)
elseif(APPLE)
    find_library(IOKIT_LIBRARY IOKit)
    find_library(FOUNDATION_LIBRARY Foundation)
    target_link_libraries( ${PROJECT_NAME} ${FOUNDATION_LIBRARY} ${IOKIT_LIBRARY})
elseif(UNIX)
    target_link_libraries( ${PROJECT_NAME} pthread)
endif ()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

3. cmake构建MFC的CSerialPort项目

新建一个基于对话框的MFC项目,解决方案名称为CommMFC

$ cd CommMFC
$ git clone https://github.com/itas109/CSerialPort
  • 1
  • 2

目录结构如下:

D:/CommMFC $ tree
.
+--- CommMFC
|   +--- CMakeLists.txt
|   +--- CommMFC.cpp
|   +--- CommMFC.h
|   +--- CommMFC.rc
|   +--- CommMFCDlg.cpp
|   +--- CommMFCDlg.h
|   +--- ReadMe.txt
|   +--- res
|   |   +--- CommMFC.ico
|   |   +--- CommMFC.rc2
|   +--- Resource.h
|   +--- stdafx.cpp
|   +--- stdafx.h
|   +--- targetver.h
+--- CommMFC.sln
+--- CSerialPort
|   +--- include
|   |   +--- CSerialPort
|   |   |   +--- SerialPort.h
|   |   |   +--- SerialPortInfo.h
|   +--- src
|   |   +--- SerialPort.cpp
|   |   +--- SerialPortBase.cpp
|   |   +--- SerialPortInfo.cpp
|   |   +--- SerialPortInfoBase.cpp
|   |   +--- SerialPortInfoWinBase.cpp
|   |   +--- SerialPortWinBase.cpp
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

本节重点说明CMakeLists.txt文件,其他代码详见mfc使用章节。

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)

project(CommMFC)

find_package(MFC)
if (NOT MFC_FOUND)
  MESSAGE(FATAL_ERROR "MFC not found")
endif()

add_definitions(-D_AFXDLL)
set(CMAKE_MFC_FLAG 2) # 1 the static MFC library 2 shared MFC library

set(CSerialPortRootPath "${CMAKE_CURRENT_SOURCE_DIR}/CSerialPort")
include_directories(${CSerialPortRootPath}/include)
list(APPEND CSerialPortSourceFiles ${CSerialPortRootPath}/src/SerialPort.cpp ${CSerialPortRootPath}/src/SerialPortBase.cpp ${CSerialPortRootPath}/src/SerialPortInfo.cpp ${CSerialPortRootPath}/src/SerialPortInfoBase.cpp)
if(WIN32)
    list(APPEND CSerialPortSourceFiles ${CSerialPortRootPath}/src/SerialPortInfoWinBase.cpp ${CSerialPortRootPath}/src/SerialPortWinBase.cpp)
elseif(UNIX)
    list(APPEND CSerialPortSourceFiles ${CSerialPortRootPath}/src/SerialPortInfoUnixBase.cpp ${CSerialPortRootPath}/src/SerialPortUnixBase.cpp)
endif()

set(MFCFiles CommMFC/CommMFC.cpp CommMFC/CommMFC.h CommMFC/CommMFC.rc CommMFC/CommMFCDlg.cpp CommMFC/CommMFCDlg.h CommMFC/Resource.h CommMFC/stdafx.cpp CommMFC/stdafx.h CommMFC/targetver.h)

add_executable(${PROJECT_NAME} WIN32 ${MFCFiles} ${CSerialPortSourceFiles})

if (WIN32)
    # for function availableFriendlyPorts
    target_link_libraries( ${PROJECT_NAME} setupapi)
elseif(APPLE)
    find_library(IOKIT_LIBRARY IOKit)
    find_library(FOUNDATION_LIBRARY Foundation)
    target_link_libraries( ${PROJECT_NAME} ${FOUNDATION_LIBRARY} ${IOKIT_LIBRARY})
elseif(UNIX)
    target_link_libraries( ${PROJECT_NAME} pthread)
endif ()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

License

License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎


Reference:

  1. https://github.com/itas109/CSerialPort
  2. https://gitee.com/itas109/CSerialPort
  3. https://blog.csdn.net/itas109
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/435965
推荐阅读
  

闽ICP备14008679号