#define _CRT_SECURE_NO_WARNINGS //关闭安全检查
#include <windows.h>
#include "iostream"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04
#define ASCII_BEL 0x07
#define ASCII_BS 0x08
#define ASCII_LF 0x0A
#define ASCII_CR 0x0D
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13
HANDLE OpenCom(DWORD nPort, DWORD nBaud)
{
HANDLE m_hIDComDev;
OVERLAPPED m_OverlappedRead, m_OverlappedWrite;
char szPort[15];
char szComParams[50];
DCB dcb;
m_hIDComDev = NULL;
if (nPort>10)
return NULL;
wsprintf(szPort, "COM%d", nPort);
m_hIDComDev = CreateFile(szPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (m_hIDComDev == NULL)
return NULL;
memset(&m_OverlappedRead, 0, sizeof(OVERLAPPED));
memset(&m_OverlappedWrite, 0, sizeof(OVERLAPPED));
COMMTIMEOUTS CommTimeOuts;
/*
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 5000;
*/
CommTimeOuts.ReadIntervalTimeout = 1000;
CommTimeOuts.ReadTotalTimeoutMultiplier = 500;
CommTimeOuts.ReadTotalTimeoutConstant = 5000;
CommTimeOuts.WriteTotalTimeoutMultiplier = 500;
CommTimeOuts.WriteTotalTimeoutConstant = 20000;
int x =SetCommTimeouts(m_hIDComDev, &CommTimeOuts);
if (!(x>0))
return NULL;
wsprintf(szComParams, "COM\\\\.\\%d:%d,n,8,1", nPort, nBaud);
m_OverlappedRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
m_OverlappedWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
dcb.DCBlength = sizeof(DCB);
GetCommState(m_hIDComDev, &dcb);
dcb.BaudRate = nBaud;
dcb.ByteSize = 8;
//--------------------------------
dcb.Parity = NOPARITY; //无 奇 偶 校 验 位
dcb.StopBits = TWOSTOPBITS; //两 个 停 止 位
//--------------------------------
unsigned char ucSet;
ucSet = (unsigned char)((FC_RTSCTS & FC_DTRDSR) != 0);
ucSet = (unsigned char)((FC_RTSCTS & FC_RTSCTS) != 0);
ucSet = (unsigned char)((FC_RTSCTS & FC_XONXOFF) != 0);
if (!SetCommState(m_hIDComDev, &dcb) ||
!SetupComm(m_hIDComDev, 10000, 10000) ||
m_OverlappedRead.hEvent == NULL ||
m_OverlappedWrite.hEvent == NULL)
{
DWORD dwError = GetLastError();
if (m_OverlappedRead.hEvent != NULL)
CloseHandle(m_OverlappedRead.hEvent);
if (m_OverlappedWrite.hEvent != NULL)
CloseHandle(m_OverlappedWrite.hEvent);
CloseHandle(m_hIDComDev);
return NULL;
}
//PurgeComm(m_hIDComDev, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
PurgeComm(m_hIDComDev, PURGE_TXCLEAR | PURGE_RXCLEAR);
return m_hIDComDev;
}
BOOL CloseCom(HANDLE m_hIDComDev)
{
if (m_hIDComDev == NULL)
return false;
CloseHandle(m_hIDComDev);
m_hIDComDev = NULL;
return true;
}
DWORD SendHexStr(HANDLE m_hIDComDev, const char *buffer)
{
BOOL bWriteStat;
DWORD dwBytesWritten = 0;
OVERLAPPED m_OverlappedWrite;
BYTE Hbyte;
BYTE Lbyte;
int i;
int len;
len = strlen(buffer) / 2;
BYTE * SendByte = new BYTE[len];
for (i = 0; i<len; i++)
{
switch (buffer[i * 2])
{
case 'a':
case 'A':Hbyte = 10;
break;
case 'b':
case 'B':Hbyte = 11;
break;
case 'c':
case 'C':Hbyte = 12;
break;
case 'd':
case 'D':Hbyte = 13;
break;
case 'e':
case 'E':Hbyte = 14;
break;
case 'f':
case 'F':Hbyte = 15;
break;
case '0':Hbyte = 0;
break;
case '1':Hbyte = 1;
break;
case '2':Hbyte = 2;
break;
case '3':Hbyte = 3;
break;
case '4':Hbyte = 4;
break;
case '5':Hbyte = 5;
break;
case '6':Hbyte = 6;
break;
case '7':Hbyte = 7;
break;
case '8':Hbyte = 8;
break;
case '9': Hbyte = 9;
break;
default: Hbyte = 0;
break;
}
switch (buffer[i * 2 + 1])
{
case 'a':
case 'A':Lbyte = 10;
break;
case 'b':
case 'B':Lbyte = 11;
break;
case 'c':
case 'C':Lbyte = 12;
break;
case 'd':
case 'D':Lbyte = 13;
break;
case 'e':
case 'E':Lbyte = 14;
break;
case 'f':
case 'F':Lbyte = 15;
break;
case '0':Lbyte = 0;
break;
case '1':Lbyte = 1;
break;
case '2':Lbyte = 2;
break;
case '3':Lbyte = 3;
break;
case '4':Lbyte = 4;
break;
case '5':Lbyte = 5;
break;
case '6':Lbyte = 6;
break;
case '7':Lbyte = 7;
break;
case '8':Lbyte = 8;
break;
case '9': Lbyte = 9;
break;
default: Lbyte = 0;
break;
}
SendByte[i] = Hbyte * 16 + Lbyte;
}
memset(&m_OverlappedWrite, 0, sizeof(OVERLAPPED));
m_OverlappedWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (m_hIDComDev == NULL)
return 0;
bWriteStat = WriteFile(m_hIDComDev, SendByte, len, &dwBytesWritten, &m_OverlappedWrite);
if (!bWriteStat)
{
if (GetLastError() == ERROR_IO_PENDING)
{
if (WaitForSingleObject(m_OverlappedWrite.hEvent, 10000))
return GetLastError();
else
{
GetOverlappedResult(m_hIDComDev, &m_OverlappedWrite, &dwBytesWritten, FALSE);//sleep
m_OverlappedWrite.Offset += dwBytesWritten;
}
}
}
else
{
return GetLastError();
}
PurgeComm(m_hIDComDev, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
return(dwBytesWritten);
}
DWORD ReadData(HANDLE m_hIDComDev, char *buffer)
{
if (m_hIDComDev == NULL)
return 0;
BOOL bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;
ComStat.fRlsdHold = 1;
OVERLAPPED m_OverlappedRead;
memset(&m_OverlappedRead, 0, sizeof(OVERLAPPED));
m_OverlappedRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
while (ComStat.fRlsdHold != 0)
{
ClearCommError(m_hIDComDev, &dwErrorFlags, &ComStat);
}
if (!ComStat.cbInQue)
return 0;
dwBytesRead = (DWORD)ComStat.cbInQue;
bReadStatus = ReadFile(m_hIDComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead);
if (!bReadStatus)
{
if (GetLastError() == ERROR_IO_PENDING)
{
WaitForSingleObject(m_OverlappedRead.hEvent, 20000);//sleep
//GetOverlappedResult(m_hIDComDev, &m_OverlappedRead, &dwBytesRead, TRUE);
return(dwBytesRead);
}
return GetLastError();
}
return dwBytesRead;
}
//***********************************Test***************************************************
HANDLE handle = NULL;
char sendTo[25] = { 0 };
char input[50] = { 0 };
DWORD sd = -1, rd = -1;
int count = 0;
//***********************************Test***************************************************
void main()
{
// 129 : 1
handle = OpenCom(3, 115200);
if (!(handle > 0))
{
printf("com open error!\n");
return;
}
count = 0;
for (int i = 0; i < 80000; i++)
{
if (i==0)
Sleep(200);
memset(sendTo, '0', sizeof(sendTo));
strcpy(sendTo, "0100010000000000640064");
sd = SendHexStr(handle, sendTo);
if (!(sd > 0))
{
printf("com send error!\n");
}
Sleep(10);
memset(input, '0', sizeof(input));
rd = ReadData(handle, input);
if (!(rd > 0 && sd > 0))
{
count++;
printf("com readerData error!%d\n", count);
}
else
{
count = 0;
}
}
system("pause");
} |
|