资源简介
简单的串口类,包含.CPP和.H,两个文件,采用异步IO读写,实现串口的读写
代码片段和文件信息
#include “StdAfx.h“
#include “CVisionSerial.h“
CVisionSerial::CVisionSerial(void)
{
hCom = NULL;
memset(&Overlapped0sizeof(Overlapped));
Overlapped.hEvent = CreateEvent(NULLFALSEFALSENULL);
}
CVisionSerial::~CVisionSerial(void)
{
}
int CVisionSerial::Init(LPCTSTR lpstrint BaudRateint ByteSizeint Parityint StopBitsint timer)
{
InitializeCriticalSection(&m_csCommunicationSync);
hCom = CreateFile(lpstr GENERIC_READ | GENERIC_WRITE 0 NULL
OPEN_EXISTING FILE_FLAG_OVERLAPPED
NULL);
if (INVALID_HANDLE_VALUE == hCom)
{
CloseHandle(hCom);
return ERROR_OPENFAIL;
}
if (SetupComm(hCom32*102432*1024) == false)
return ERROR_SETBUFFER;
PurgeComm(hComPURGE_RXCLEAR | PURGE_TXCLEAR);
COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout = 100;
TimeOuts.ReadTotalTimeoutConstant = timer;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.WriteTotalTimeoutConstant = timer;
TimeOuts.WriteTotalTimeoutMultiplier = 0;
if (SetCommTimeouts(hCom&TimeOuts) == false)
return ERROR_CRPTIMEOUT;
DCB dcb;
if(GetCommState(hCom&dcb) == false)
return ERROR_PORTFAULT;
dcb.BaudRate = BaudRate;
dcb.ByteSize = ByteSize;
dcb.Parity = Parity;
dcb.StopBits = StopBits;
if (SetCommState(hCom&dcb) == false)
return ERROR_PORTFAULT;
PurgeComm(hCom PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
return 0;
}
int CVisionSerial::SendData(char *buff int lenunsigned long *WrittenLen)
{
PurgeComm(hCom PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
EnterCriticalSection(&m_csCommunicationSync);
BOOL bWriteStat = WriteFile(hCombufflenWrittenLen&Overlapped); //向用户传送数据
if (!bWriteStat)
{
if (GetLastError() == ERROR_IO_PENDING)
WaitForSingleobject(Overlapped.hEvent1000);
}
LeaveCriticalSection(&m_csCommunicationSync);
return bWriteStat;
}
void CVisionSerial::ReadData(char *buffunsigned long byteRead unsigned long *len)
{
EnterCriticalSection(&m_csCommunicationSync);
if (ReadFile(hCombuffbyteReadlen&Overlapped) == FALSE) //读取串口数据
{
int i=GetLastError();
if (GetLastError() == ERROR_IO_PENDING)
{
GetOverlappedResult(hCom&OverlappedlenTRUE);
}else
{
memset(buff0sizeof(buff));
}
}
LeaveCriticalSection(&m_csCommunicationSync);
}
HANDLE CVisionSerial::getHCOM()
{
return hCom;
}
void CVisionSerial::closeHCOM()
{
if (hCom != NULL)
{
CloseHandle(hCom);
}
}
void CVisionSerial::ClearOutBuffer()
{
::PurgeComm(hCom PURGE_TXABORT | PURGE_TXCLEAR );
}
void CVisionSerial::ClearInBuffer()
{
::PurgeComm(hCom PURGE_RXABORT | PURGE_RXCLEAR);
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2730 2015-08-27 16:55 Serialport.cpp
文件 774 2015-08-27 16:56 Serialport.h
----------- --------- ---------- ----- ----
3504 2
- 上一篇:微信分享到朋友,朋友圈,QQ,QQ空间
- 下一篇:STAP90有限元程序
相关资源
- STM32的AD采集信号,并发送到串口
- labview串口程序
- STM32串口连续接收一帧数据
- Sigcomm2015-2019.rar
- QT5串口上位机实现 源码
- comsol 3.5a License file
- comsol 3.5 LICENSE 文件
- 单片机与电脑串口通信
- myseria串口
- Modbus Rtu Slave (codesyssyscom.lib实现
- xgcom-0.04.2源码
- linux下的一个异步通信的串口类
- linux gsm0710 CMUX串口多路复用源码
- DL/T645规约电能表的抄表和电表模拟软
- Unity3D中使用串口收发数据,仅供参考
- vs+qt+qgis环境下通过串口通信接收数据
- Digital Computation of the Fractional Fourier
- labview串口采集数据 曲线显示 实时最
- MSP430 USB转串口驱动
- 聂小猛——串口调试助手
- AT89S52控制DDS9850产生可调频的正弦波
- 徕卡Geocom使用手册
- epson驱动串口黑标打印控制代码 delp
- 串口网口透传映射源码基于arm-mx335
- 东北大学设计模式实验3
- DICOM格式图像
- FPGA实现UART串口通信最全资料
- Agilent34401A串口程控程序Labview
- Windows串口编程
- Qt编写串口通信程序图文详解
评论
共有 条评论