// api3333.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
HANDLE hcomm;
HANDLE WriteEvent;
HANDLE WriteExitEvent;
OVERLAPPED m_ov;
COMSTAT comstat;
DWORD dwBytesRead;
BOOL bRead = true;
int n;
int h;
char d[100];
ProcessErrorMessage(char* ErrorText)//?
{
char* Temp = new char[200];
LPVOID LpMegBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
0,
(LPTSTR)&LpMegBuf,
0,
NULL
);
sprintf(Temp,"WARNING: %s Failed with the following error:\n%s\nport:%d\n",ErrorText,LpMegBuf,"COM3");
MessageBox(NULL,Temp,"Application Error",MB_ICONSTOP);
LocalFree(LpMegBuf);
delete[] Temp;
return true;
}
bool openport(char* portname)//????
{
hcomm = CreateFile(portname,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);
if (hcomm == INVALID_HANDLE_VALUE)
{
DWORD dwopenerror = GetLastError();
printf("open comport error:%u\n",dwopenerror);
CloseHandle(hcomm);
return false;
}
else
return true;
}
bool setupdcb(int rate_arg)
{
DCB dcb;
int rate = rate_arg;
memset(&dcb,0,sizeof(dcb));
if(!GetCommState(hcomm,&dcb))
return FALSE;
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = rate;
dcb.Parity = NOPARITY;
dcb.fParity = 0;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
if(!SetCommState(hcomm,&dcb))
return false;
else
return true;
}
bool setuptimeout(DWORD ReadInterval,DWORD ReadTotalMultiplier,DWORD ReadTotalconstant,
DWORD WriteTotalMultiplier,DWORD WriteTotalconstant)
{
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = ReadInterval;
timeouts.ReadTotalTimeoutConstant = ReadTotalconstant;
timeouts.ReadTotalTimeoutMultiplier = ReadTotalMultiplier;
timeouts.WriteTotalTimeoutConstant = WriteTotalconstant;
timeouts.WriteTotalTimeoutMultiplier = WriteTotalMultiplier;
if(!SetCommTimeouts(hcomm,&timeouts))
return false;
else
return true;
}
writechar(LPVOID lpoutbuff)
{
BOOL bWrite = true;
BOOL bResult = true;
DWORD BytesSent = 0;
if(bWrite)
{
m_ov.Offset = 0;
m_ov.OffsetHigh = 0;
bResult = WriteFile(hcomm,lpoutbuff,n,&BytesSent,&m_ov);
if(!bResult)
{
DWORD dwError = GetLastError();
switch(dwError)
{
case ERROR_IO_PENDING:
{
BytesSent = 0;
bWrite = false;
break;
}
default:
{
ProcessErrorMessage("WriteFile()");//?
}
}
}
}
if(!bWrite)
{
bWrite = true;
bResult = GetOverlappedResult(hcomm,&m_ov,&BytesSent,true);
if(!bResult)
{
ProcessErrorMessage("GetOverlappedResult() in WriteFile()");//?
}
}
return true;
}
DWORD ReadComm(LPVOID lpBuf)
{
DWORD dwError;
BOOL bReadState;
DWORD dwErrorFlags;
ClearCommError(hcomm, &dwErrorFlags, &comstat);
dwBytesRead=comstat.cbInQue;
DWORD dwBytesToRead=comstat.cbInQue;
if(dwBytesRead >= 1)
bReadState = ReadFile(hcomm,lpBuf,dwBytesToRead,&dwBytesRead, &m_ov);
if(!bReadState) // ??????
{
switch(dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
printf("GetLastError == ERROR_IO_PENDING\n");
bRead = false;
break;
}
default:
{
break;
}
}
}
if(!bRead)
{
bRead = true;
bReadState = GetOverlappedResult(hcomm,&m_ov,&dwBytesRead,true);
}
if(!bReadState)
{
ProcessErrorMessage("GetOverlappedResult() in ReadFile()");//?
}
return dwBytesRead;
}
DWORD WINAPI readthread(LPVOID pParam)
{
int i;
DWORD read;
DWORD dwEventMask = 0;
char rbuff[1024];
for(;;)
{
SetCommMask(hcomm,EV_RXCHAR|EV_ERR);
WaitCommEvent(hcomm, &dwEventMask, &m_ov);
if(dwEventMask == EV_RXCHAR)
{
read = ReadComm(rbuff);
if(read > 0)
{
printf("the received string:\n");
for(i=0;i < dwBytesRead;i++)
printf("%c",rbuff[i]);
printf("\n");
}
}
if(dwEventMask == EV_ERR)
{
printf("Error");
}
}
return true;
}
DWORD WINAPI writethread(LPVOID pParam)
{
WriteEvent=CreateEvent(NULL,false,false,NULL);
WriteExitEvent=CreateEvent(NULL,false,false,NULL);
HANDLE EventArray[2];
EventArray[0]=WriteEvent;
EventArray[1]=WriteExitEvent;
int Done = 0;
while(!Done)
{
DWORD dwRes=WaitForMultipleObjects(2,EventArray,false,INFINITE);
switch(dwRes)
{
case WAIT_FAILED:
{
printf("waitfailed\n");
break;
}
case WAIT_OBJECT_0:
{
writechar(d);//??????
break;
}
case WAIT_OBJECT_0 + 1:
{
Done=true;
break;
}
}
}
return true;
}
void main()
{
int DONE = 1;
bool open;
open = openport("COM1");
if(open)
printf("open comport success!\n");
else
printf("open comport failure!\n");
if(setupdcb(9600))
printf("setup DCB success!\n");
else
printf("setup DCB failure!\n");
if(setuptimeout(1000,1000,1000,1000,1000))
printf("setup timeout success!\n");
else
printf("setup timeout failure!\n");
PurgeComm(hcomm,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_TXABORT);
if(PurgeComm == 0)
{
printf("PurgeComm fall!\n");
}
else
{
printf("PurgeComm success!\n");
}
HANDLE hreadthread = CreateThread(NULL,0,readthread,0,0,NULL);//??????
HANDLE hwritethread = CreateThread(NULL,0,writethread,0,0,NULL);
do
{
gets(d);
n=strlen(d);
char END[]="end";
if(strcmp(d,END)==0)
{
SetEvent(WriteExitEvent);
DONE=0;
}
else
{
if(n>0)
SetEvent(WriteEvent);
}
}while(DONE);
CloseHandle(hwritethread);
CloseHandle(hreadthread);
}
重要的参考winapi没有mfc的操作
最新推荐文章于 2023-11-10 13:10:03 发布