#include "stdafx.h"
#include "windows.h"
#include "tchar.h"
typedef unsigned char u8;
extern HANDLE InitCom(LPCWSTR portname);
extern DWORD ReadData(HANDLE handler, u8 *buffer, DWORD wCount);
extern DWORD WriteData(HANDLE handler, u8 * buffer, DWORD writelen);
HANDLE hcom=NULL;
bool run = true;
DWORD WINAPI MyThreadFunction(LPVOID lpParam)
{
while (run == 1)
{
Sleep(1000);
if (hcom != NULL)
{
WriteData(hcom, (u8*)"helloworld", 10);
}
}
return 0;
}
DWORD WINAPI SportRecvThread(LPVOID lpParam)
{
DWORD readcnt;
u8 buffer[128];
while (run == 1)
{
if (hcom != NULL)
{
readcnt = ReadData(hcom,buffer,128);
if (readcnt > 0)
{
for (int i = 0; i < readcnt; i++)
{
printf("%02x ", buffer[i]);
}
printf("\n");
}
}
Sleep(20);
}
return 0;
}
int main()
{
HANDLE hThread,hThreadRead;
DWORD dwThreadId;
//testsend time
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
MyThreadFunction, // thread function name
NULL, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
hThreadRead = CreateThread(
NULL, // default security attributes
0, // use default stack size
SportRecvThread, // thread function name
NULL, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
int PortNum = 11;
WCHAR lpszPortName[100];
if (PortNum < 10)
wsprintf(lpszPortName, _T("COM%d"), PortNum);
else
wsprintf(lpszPortName, _T("\\\\.\\COM%d"), PortNum);
hcom = InitCom(lpszPortName);
if (hcom != NULL)
{
for (int i = 0; i < 50; i++)
Sleep(1000);
}
run = false;//stop thread
Sleep(1000);
if(hcom !=NULL)
CloseHandle(hcom);
return 0;
}
串口操作程序:
#include "windows.h"
#include "tchar.h"
typedef unsigned char u8;
HANDLE InitCom(LPCWSTR portname )
{
HANDLE hCom;
hCom = CreateFile( portname,//COM7口
GENERIC_READ | GENERIC_WRITE, //允许读和写
0, //独占方式
NULL,
OPEN_EXISTING, //打开而不是创建
0, //同步方式
NULL);
if (hCom == (HANDLE)-1)
{
return NULL;
}
SetupComm(hCom, 1024, 1024);
COMMTIMEOUTS TimeOuts;
//115200 一帧数据<500个
TimeOuts.ReadIntervalTimeout = 1; //两个字符之间的间隔时间
TimeOuts.ReadTotalTimeoutMultiplier = 1; //读取一个字节超时时间
TimeOuts.ReadTotalTimeoutConstant = 50;
TimeOuts.WriteTotalTimeoutMultiplier = 100;
TimeOuts.WriteTotalTimeoutConstant = 500;
SetCommTimeouts(hCom, &TimeOuts); //设置超时
DCB dcb;
GetCommState(hCom, &dcb);
dcb.BaudRate = 115200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = 1;
SetCommState(hCom, &dcb);
PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
return hCom;
}
DWORD ReadData(HANDLE handler, u8 *buffer,DWORD wCount)
{
//char readBuffer[512];
//memset(readBuffer, 0, 512);
BOOL bReadStat;
bReadStat = ReadFile(handler, buffer, wCount, &wCount, NULL);
if (!bReadStat)
{
return 0;
}
PurgeComm(handler, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
return wCount;
}
DWORD WriteData(HANDLE handler, u8 * buffer ,DWORD writelen)
{
COMSTAT ComStat;
DWORD dwErrorFlags;
BOOL bWriteStat;
ClearCommError(handler, &dwErrorFlags, &ComStat);
bWriteStat = WriteFile(handler, buffer, writelen, &writelen, NULL);
if (!bWriteStat)
{
return 0;
}
return writelen;
}