1 Star 0 Fork 7

沐风/SerialTool

forked from jiangxiaogang/SerialTool 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
UART.c 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
jiangxiaogang 提交于 2022-03-08 18:29 . 修复GBK问题
#include "stdafx.h"
#include "UART.h"
HANDLE UART_Open(LPTSTR pzName)
{
HANDLE hUART;
COMMTIMEOUTS timeout;
char szPortName[32];
sprintf(szPortName,"\\\\.\\%s",pzName);
hUART = CreateFile(szPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if((hUART==INVALID_HANDLE_VALUE)||(hUART==NULL))
{
return NULL;
}
GetCommTimeouts(hUART, &timeout);
timeout.ReadIntervalTimeout = MAXDWORD;
timeout.ReadTotalTimeoutMultiplier = 0;
timeout.ReadTotalTimeoutConstant = 0;
timeout.WriteTotalTimeoutConstant = 0;
timeout.WriteTotalTimeoutMultiplier = 0;
SetCommTimeouts(hUART,&timeout);
SetupComm(hUART, 64*1024, 0);
PurgeComm(hUART, PURGE_RXCLEAR | PURGE_TXCLEAR);
return hUART;
}
void UART_Close(HANDLE hUART)
{
CloseHandle(hUART);
}
void UART_Setup(HANDLE hUART, DWORD dwBaudRate, BYTE biDataBits, BYTE biStopBits, BYTE biParity)
{
DCB dcb;
GetCommState(hUART, &dcb);
dcb.BaudRate = dwBaudRate;
dcb.Parity = biParity;
dcb.ByteSize = biDataBits;
dcb.StopBits = biStopBits;
SetCommState(hUART, &dcb);
}
DWORD UART_Read(HANDLE hUART, void *pData, DWORD dwLen)
{
BOOL biRet;
DWORD dwRead = 0;
biRet = ReadFile(hUART,pData,dwLen,&dwRead,NULL);
return biRet?dwRead:0;
}
DWORD UART_Write(HANDLE hUART, void *pData, DWORD dwLen)
{
BOOL biRet;
DWORD dwWrite;
biRet = WriteFile(hUART,pData,dwLen,&dwWrite,NULL);
return biRet?dwWrite:0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/cody_liu/SerialTool.git
git@gitee.com:cody_liu/SerialTool.git
cody_liu
SerialTool
SerialTool
master

搜索帮助