⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialcontrol.cpp

📁 at91sam7s64芯片上位机程序
💻 CPP
字号:
#include "StdAfx.h"
#include "SerialControl.h"

HANDLE hcom;			//串口句柄		
HANDLE hcomcontrol;		//监测线程句柄
LPCTSTR comnum="COM1";			//串口号
DWORD combaud=9600;			//串口波特率
int largebuff[4100];	//存放接受的数据
bool rxstaute;			//判断接受的是高字节还是低字节
int  highbyte;
CFile savefile;


bool opencom()					//打开串口
 {
	hcom=CreateFile(comnum,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
	  /*FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED*/0,NULL);
	if(hcom!=INVALID_HANDLE_VALUE)
	{
		DCB wdcb;
		SetupComm(hcom,1,5);		//设置接收和发送缓冲区
		GetCommState (hcom,&wdcb);
		wdcb.BaudRate=combaud;
		wdcb.fBinary=0;
		wdcb.fParity=false;
		wdcb.ByteSize=8;
		wdcb.Parity=NOPARITY;
		wdcb.StopBits=ONESTOPBIT;
		SetCommState (hcom,&wdcb);
		AfxMessageBox("成功打开串口");   //8位,无校验,1位停止位
		PurgeComm(hcom,PURGE_TXCLEAR|PURGE_RXCLEAR);
	}
	else
	{
		AfxMessageBox("串口打开失败");
		return false;
	}
	return true;
 }

DWORD WINAPI SerialPortThread( HWND hWnd)
{
	int databuff=0;				//接收到的数据
	unsigned long datacount;	//接收数据的数量

	OVERLAPPED wol;
	wol.Internal=0;
	wol.InternalHigh=0;
	wol.Offset=0;
	wol.OffsetHigh=0;
	wol.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);

	while(1)
	{
		ReadFile(hcom, &databuff, 1, &datacount, &wol);	
		if (datacount!=0)			//收到有效数据
		{							//把接收的数据作为参数传给消息相应函数
			::PostMessage(hWnd, COM_RECVDATA, (unsigned int)databuff, 1);
		}
	}
	return 1;
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -