Distribution_Service/CC_SDK/Include/Module/Comm/CCSerialPort.h

206 lines
5.9 KiB
C
Raw Normal View History

2025-11-11 17:46:19 +08:00
#ifndef IMGUI_CCSERIALPORT_H
#define IMGUI_CCSERIALPORT_H
// 根据操作系统定义睡眠函数Windows使用Sleep其他平台使用usleep
#ifdef _WIN32
#include <windows.h>
#define imsleep(microsecond) Sleep(microsecond) // ms
#else
#include <unistd.h>
#define imsleep(microsecond) usleep(1000 * microsecond) // ms
#endif
// 包含必要的头文件
#include <functional>
#include <string>
#include "CSerialPort/SerialPort.h"
#include "CSerialPort/SerialPortInfo.h"
// 使用itas109命名空间
using namespace itas109;
// 定义CTL命名空间
namespace CTL {
/**
* @class SerialPort
* @brief CSerialPortListener
*/
class SerialPort : public CSerialPortListener
{
public:
/**
* @brief SerialPort对象
*/
SerialPort();
/**
* @brief 使CSerialPort对象进行初始化
*
* @param sp CSerialPort对象指针
*/
explicit SerialPort(CSerialPort *sp) : COM(sp)
{
// 获取所有可用的串口信息并存储在PortsList中
PortsList = CSerialPortInfo::availablePortInfos();
};
/**
* @brief
*
* @return unsigned int
*/
unsigned int GetPortSize();
/**
* @brief
*
* @return SerialPortInfo*
*/
std::vector<SerialPortInfo> GetProtInfoAll();
/**
* @brief
*
* @param portName "COM1"
* @param baudRate 9600
* @param parity
* @param dataBits 8
* @param stopbits 1
* @param flowControl
* @param readBufferSize 4096
*/
void Initialize(
const char *portName,
int baudRate = itas109::BaudRate::BaudRate9600,
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);
/**
* @brief
*
* @param ms
*/
void SetReadIntervalTime(unsigned int ms);
/**
* @brief
*
* @return bool truefalse
*/
bool Open();
/**
* @brief
*
* @return bool truefalse
*/
bool IsOpen();
/**
* @brief
*
* @return int
*/
int GetLastError();
/**
* @brief
*
* @return const char*
*/
const char *GetLastErrorMsg();
/**
* @brief
*
* @param portName
* @param readBufferLen
*/
void onReadEvent(const char *portName, unsigned int readBufferLen) override;
/**
* @brief
*
* @param data
* @param size
*/
void Write(const void *data, int size);
/**
* @brief
*
* @param data
*/
void Write(std::string data);
/**
* @brief
*
* @return char*
*/
char *GetReadData();
/**
* @brief
*/
void Close();
/**
* @brief
*
* @tparam Func
* @tparam Args
* @param fun
* @param args
* @return bool truefalse
*/
template <typename Func, typename... Args>
bool SetReadClick(Func &&fun, Args &&... args);
private:
/**
* @brief
*/
CSerialPort *COM;
/**
* @brief
*/
std::vector<SerialPortInfo> PortsList;
/**
* @brief
*/
std::function<void()> Function;
/**
* @brief
*/
char *Date;
};
}
/**
* @brief
*
* @tparam Func
* @tparam Args
* @param fun
* @param args
* @return bool truefalse
*/
template<typename Func, typename... Args>
inline bool CTL::SerialPort::SetReadClick(Func &&fun, Args &&... args)
{
Function = std::bind(std::forward<Func>(fun), std::forward<Args>(args)...);
return true;
}
#endif