赞
踩
环境:
系统:windows 10 64位
QT: 5.12.9(5.9及以上版本支持cmake构建)
MFC: vs2008
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项目地址:
$ cd CommConsole
$ git clone https://github.com/itas109/CSerialPort
目录结构如下:
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
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; }
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 ()
新建一个QT项目目,解决方案名称为CommQT
【文件】-【新建文件或项目】-【Application(Qt)】-【Qt Widgets Application】-【choose…】-【名称: CommQT】-【Build system: CMake】
CommQT解决方案目录下载CSerialPort源码
$ cd CommQT
$ git clone https://github.com/itas109/CSerialPort
目录结构如下:
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
本节重点说明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 ()
新建一个基于对话框的MFC项目,解决方案名称为CommMFC
$ cd CommMFC
$ git clone https://github.com/itas109/CSerialPort
目录结构如下:
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
本节重点说明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 ()
License
License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎
Reference:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。