nim_duilib/examples/proto_debuger/serial_port.h
2022-02-20 11:55:16 +08:00

74 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <iostream>
#include <map>
using namespace std;
typedef struct{
wstring name;
UINT port_num;
uint32_t baurate;
uint8_t data_bits;
uint8_t stop_bits;
uint8_t verify;
uint8_t flow_control;
}UartInfo;
typedef struct {
wstring ip;
uint32_t port;
uint32_t socket_fd;
}TcpClientInfo;
typedef struct {
wstring ip;
uint32_t port;
uint32_t socket_fd;
}TcpServerInfo;
class SerialPort
{
public:
SerialPort();
~SerialPort();
private:
//使用CreateFile打开串口同步方式打开
static HANDLE OpenPort(UINT PortNo = 1);
public:
//通过查询注册表方式获取系统里面的串口,返回串口数量,指针指向串口列表
static UINT GetPortNum(UINT** PortList);
//从指定串口缓冲区读取数据
static UINT GetByte(UINT PortNo);
//关闭指定串口并从map删除
static void ClosePort(UINT PortNo);
//关闭所有串口
static void ClearAllPort();
//初始化指定端口成功后存入map
static int InitPort(UINT PortNo, UINT Baud = 9600, byte Parity = 0, byte Data = 8, byte Stop = 0); //初始化串口
//从指定端口读取数据,返回值表示读取的bytes
static int ReadPort(UINT PortNo, char* P_recved, int length);
//往指定串口发送数据,返回实际写入
static int WritePort(UINT PortNo, const char* pData, int length = 0);
//检查指定串口状态
static int PortState(UINT PortNo);
//清空指定串口缓冲区
static void ClearCom(UINT PortNo);
//指定端口RTS信号检测用于脚踏开关信号触发
static int CheckRTS(UINT PortNo);
public:
//static UINT PortNum; //串口端口号
static map<int, HANDLE> ComMap;
};