CSerialPort使用

CSerialPort介绍

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

CSerialPort作者:CSDN博客-itas109

github: github.com

gitee: CSerialPort: CSerialPort轻量级跨平台串口类库(支持C/C++/C#/Java/Python/Node.js)

使用环境

主机:工控机外置固定串口(RS232、RS485等)

平台:X86

操作系统:Ubuntu 18.04

使用环境:ROS

使用过程

CSerialPort安装

git clone https://gitee.com/itas109/CSerialPort.git
cd CSerialPort
mkdir bin && cd bin
cmake ..
make
sudo make install # 默认安装目录为/usr/local/

CSerialPort使用

给CSerialPort再套一层壳,这样方便使用

主要参考examples中的Commtui和CommQT两个例程,还有就是作者写的博客,有什么问题也可以加作者的QQ群详细咨询

注意点:My_Serialitas109::CSerialPortListener的继承类,主要是为了重载onReadEvent函数

修改点:My_Serial 初始化接收数据的固定长度,InitSerial提炼串口名和波特率作为初始化函数,将串口接收函数中的接受数据改为单字节接收,根据接收内容判定是否为正确数据,同时校验数据长度,当达到一定长度时进行数据解析,将解析后的数据扔进vector队列中,外层程序通过读取队列数据进行后续操作

my_serial.h

#pragma once

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

class My_Serial : public itas109::CSerialPortListener
{
public:
    My_Serial(int mlen);
    ~My_Serial();

    bool InitSerial(const char *portName,
                    int baudRate,
                    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);

    void SendSerialMsg(uint8_t *txmsg, unsigned int size);
    std::vector<uint8_t> RecSerialMsg(void);

private:
    itas109::CSerialPort *csp_;
    int msgLen_;
    std::vector<uint8_t> rxData_;
    std::queue<std::vector<uint8_t>> rxQue_;
    const int rxMsgLen_ = 1024;

    int mlen_ = 0;

    void onReadEvent(const char *portName, unsigned int readBufferLen);
};

my_serial.cpp

#include "serial/my_serial.h"
My_Serial::My_Serial(int mlen)
    : csp_(NULL)
    , msgLen_(mlen)
{
    csp_ = new itas109::CSerialPort();
}

My_Serial::~My_Serial()
{
    if (csp_)
    {
        csp_->close();
        delete csp_;
        csp_ = NULL;
    }
}

bool My_Serial::InitSerial(const char *portName,
                              int baudRate,
                              itas109::Parity parity,
                              itas109::DataBits dataBits,
                              itas109::StopBits stopbits,
                              itas109::FlowControl flowControl,
                              unsigned int readBufferSize)
{
    csp_->init(portName, baudRate, parity, dataBits, stopbits, flowControl, readBufferSize);

    csp_->open();

    if (csp_->isOpen())
    {
        csp_->connectReadEvent(this);
        LOG(INFO) << "open " << portName << " success";
        return true;
    }
    else
    {
        LOG(WARNING) << "open " << portName << " failed";
        LOG(WARNING) << "please check " << portName << " related settings";
        return false;
    }
}

void My_Serial::SendSerialMsg(uint8_t *txmsg, unsigned int size)
{
    csp_->writeData(txmsg, size);
}

std::vector<uint8_t> My_Serial::RecSerialMsg(void)
{
    std::vector<uint8_t> result;
    if (rxQue_.empty())
    {
        return result;
    }
    result = rxQue_.front();
    rxQue_.pop();
    return result;
}

void My_Serial::onReadEvent(const char *portName, unsigned int readBufferLen)
{
    if (readBufferLen > 0)
    {
        rxData_.resize(msgLen_);
        while (readBufferLen)
        {
            uint8_t *data = new uint8_t[1];
            int recLen = csp_->readData(data, 1);
            readBufferLen--;
            if ((*data == 0xAA))
            {
                mlen_ = 0;
            }
            rxData_[mlen_] = *data;
            mlen_++;
            delete[] data;
            data = NULL;
        }

        if (mlen_ >= msgLen_)
        {
            // for (auto s : rxData_)
            // {
            //     LOGF(INFO, "rxData_ : %x", s);
            // }

            if (rxQue_.size() == rxMsgLen_)
            {
                rxQue_.pop();
            }
            rxQue_.push(rxData_);

            rxData_.clear();
            mlen_ = 0;
        }
    }
}

CMakeLists.txt

link_directories(
  /usr/local/lib
  /usr/lib/
  /usr/lib/x86_64-linux-gnu/
  ${catkin_LIB_DIRS}
)

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
  /usr/include/
  /usr/local/include
)
    
FIND_LIBRARY(CSERIAL_LIBRARIES cserialport)
    
add_library(my_serial
  src/serial/my_serial.cpp  
)
add_dependencies(my_serial ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(my_serial
  ${catkin_LIBRARIES}
  ${Boost_LIBRARIES}
  ${CSERIAL_LIBRARIES}
)
posted @ 2023-07-27 10:12  泡泡吐泡泡啊  阅读(388)  评论(0编辑  收藏  举报