You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.2 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*********************************************************************
*文件说明: 串口控制类
*作者: logos
*日期: 2018/11/09
*修改日志: 2018/11/09 create
***********************************************************************/
#pragma once
#ifndef _DLL_INCLUDE_COMPORT_H
#define _DLL_INCLUDE_COMPORT_H
#ifndef EXPORT_C
#define EXPORT_C extern "C"
#endif
#ifndef WINAPI
#define WINAPI __stdcall
#endif
enum EMParity
{//波特率
EMP_NO = 0, //无校验位
EMP_ODD, //奇校验
EMP_EVEN, //偶校验
EMP_MARK, //mark
EMP_SPACE, //space
};
enum EMByteSize
{//数据位
EMD_BITS5 = 5, //5位
EMD_BITS6, //6位
EMD_BITS7, //7位
EMD_BITS8, //8位
};
enum EMStopBits
{//停止位
EMS_ONE = 0, //1位
EMS_ONE5, //1.5位
EMS_TWO, //2位
};
struct ComParam
{//串口参数
uint comPort; //串口index 1、2、3...
uint baudRate; //波特率600、1200、...9600...115200
EMParity parity; //校验位
EMByteSize btSize; //数据位
EMStopBits stopBit; //停止位
uint readSize; //读缓冲区
uint writeSize; //写缓冲区
uint bOverlapped;//是否异步 1 是 0不是
};
enum IComState
{//串口状态
ICom_Closed = 0, //关闭
ICom_Error, //未设置参数或通讯失败
ICom_Normal, //正常
};
class icomport
{//串口类接口
public:
/****************************************************
*Function: 打开串口
*Intput: CommNum 串口ID
*Output: NULL
*Return: 成功返回0 ,失败返回错误码
******************************************************/
virtual int Open(int iport) = 0;
/****************************************************
*Function: 关闭串口
*Intput: NULL
*Output: NULL
*Return: 成功返回0 ,失败返回错误码
******************************************************/
virtual int Close() = 0;
/****************************************************
*Function: 重置错误或复位串口
*Intput: NULL
*Output: NULL
*Return: 成功返回0 ,失败返回错误码
******************************************************/
virtual int Reset() = 0;
/****************************************************
*Function: 设置串口参数 -- Open以后调用
*Intput: pszparam 设置参数 -- "19200,n,8,1"
19200 波特率
无校验n //偶校验e //奇校验o
8 数据位
1 停止位
*Output: NULL
*Return: 成功返回0 ,失败返回错误码
*PS调用此接口后才能通讯
******************************************************/
virtual int SetParam(const char* pszparam) = 0;
/****************************************************
*Function: 设置串口缓冲区参数 -- Open以后调用
*Intput: iread 输入缓冲区大小
iwrite 输出缓冲区大小
*Output: NULL
*Return: 成功返回0 ,失败返回错误码
******************************************************/
virtual int SetBuffer(int iread, int iwrite) = 0;
/****************************************************
*Function: 获取串口状态
*Intput: NULL
*Output: NULL
*Return: 返回串口状态
******************************************************/
virtual IComState GetStatus() = 0;
/****************************************************
*Function: 写入串口数据
*Intput: pszbuff 数据指针
len 数据长度
*Output: NULL
*Return: 成功返回写入的数据长度 ,失败返回错误码
******************************************************/
virtual int Write(const char* pszbuff, int len) = 0;
/****************************************************
*Function: 读取串口数据
*Intput: pszbuff 数据指针
nsize 期望读取的数据长度
timeout 超时时间 单位ms
*Output: NULL
*Return: 成功返回写入的数据长度 ,失败返回错误码
******************************************************/
virtual int Read(char* pszbuff, int nsize, int timeout) = 0;
};
//导出接口
EXPORT_C int WINAPI icomport_open(icomport* p, int iport);
EXPORT_C int WINAPI icomport_close(icomport* p);
EXPORT_C int WINAPI icomport_reset(icomport* p);
EXPORT_C int WINAPI icomport_set_param(icomport* p, const char* pszparam);
EXPORT_C int WINAPI icomport_set_buffer(icomport* p, int iread, int iwrite);
EXPORT_C int WINAPI icomport_status(icomport* p);
EXPORT_C int WINAPI icomport_write(icomport* p, const char* pszbuff, int len);
EXPORT_C int WINAPI icomport_read(icomport* p, char* pszbuff, int nsize, int timeout);
/****************************************************
*Function: 创建com对象
*Intput: NULL
*Output: h com对象指针
*Return: 成功返回0 ,失败返回错误码
******************************************************/
EXPORT_C int WINAPI CreateComPort(icomport** h);
/****************************************************
*Function: 释放com对象
*Intput: NULL
*Output: h com对象指针
*Return: 成功返回0 ,失败返回错误码
******************************************************/
EXPORT_C int WINAPI ReleaseComPort(icomport** h);
#endif //防止重复包含