VC 串口操作


#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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值