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

📄 avr_usbdlg.cpp

📁 usb 程序 usb通讯 仿真程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	mtemp[2] = m_rrow;
	mtemp[3] = m_rL;

	mtemp[4] = m_wsec;
	mtemp[5] = m_wcol;
	mtemp[6] = m_wrow;

	mtemp[7] = m_Prd;
	mtemp[8] = m_Pwr;
	mtemp[9] = m_Erase;

	UpdateData(TRUE);
	if(m_rsec<0 || m_rsec>255)c=1;
	if(m_rcol<0 || m_rcol>255)c=1;
	if(m_rrow <0 || m_rrow >65535)c=1;
	if(m_rL   <0 || m_rL   >34603008)c=1;

	if(m_wsec<0 || m_wsec>255)c=1;
	if(m_wcol<0 || m_wcol>255)c=1;
	if(m_wrow <0 || m_wrow >65535)c=1;

	if(m_Prd <0 || m_Prd >65535)c=1;
	if(m_Pwr <0 || m_Pwr >65535)c=1;
	if(m_Erase <0 || m_Erase >65535)c=1;

	if(c)
	{
		m_rsec = mtemp[0];
		m_rcol = mtemp[1];
		m_rrow = mtemp[2];
		m_rL   = mtemp[3];

		m_wsec = mtemp[4];
		m_wcol = mtemp[5];
		m_wrow = mtemp[6];

		m_Prd = mtemp[7];
		m_Pwr = mtemp[8];
		m_Erase = mtemp[9];

		UpdateData(FALSE);
	}
	hFile = openUSBfile_1();
		
	if(hFile == INVALID_HANDLE_VALUE) {
		m_wendu = "未连接USB";
	}else
	{
		short rdBuffer[8]={0,0,0,0,0,0,0,0};
		int rdLength=16;
		float temperature;
		bResult = ReadFile(hFile,
			&rdBuffer,
			rdLength,
			&nBytes,
			NULL);
		if (bResult == TRUE)
		{
			if(nBytes>=0)
			{
				temperature =(float) rdBuffer[0]/16;
				sprintf(temp,"温度: %.4f ℃",temperature);
				m_wendu = temp;
			}
		}
	}
	UpdateData(FALSE);
	
	CloseHandle(hFile);
}

void CAVR_USBDlg::Onwendu() 
{
	
    if(cOnwendu==0)
	{
		cOnwendu = 1;
		usb_command(0x02,0);
		//SetTimer(100,1000,NULL); // 1HZ
	}else
	{
		SetTimer(100,-1,NULL);
		cOnwendu = 0;
	}
	
}

char portb = 0xff;
void CAVR_USBDlg::OnPb0() 
{
	// TODO: Add your control notification handler code here
	if(portb & 0x01)
	{
		portb &= 0xfe;
	}else
	{
		portb |= 0x01;
	}
	usb_command(3,portb);
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnPb1() 
{
	// TODO: Add your control notification handler code here
	
	if(portb & 0x02)
	{
		portb &= 0xfd;
	}else
	{
		portb |= 0x02;
	}
	usb_command(3,portb);
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnPb2() 
{
	// TODO: Add your control notification handler code here
	
	if(portb & 0x04)
	{
		portb &= 0xfb;
	}else
	{
		portb |= 0x04;
	}
	usb_command(3,portb);
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnPb3() 
{
	// TODO: Add your control notification handler code here
	
	if(portb & 0x08)
	{
		portb &= 0xf7;
	}else
	{
		portb |= 0x08;
	}
	usb_command(3,portb);
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnPb4() 
{
	// TODO: Add your control notification handler code here
	
	if(portb & 0x10)
	{
		portb &= 0xef;
	}else
	{
		portb |= 0x10;
	}
	usb_command(3,portb);
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnPb5() 
{
	// TODO: Add your control notification handler code here
	
	if(portb & 0x20)
	{
		portb &= 0xdf;
	}else
	{
		portb |= 0x20;
	}
	usb_command(3,portb);
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnPb6() 
{
	// TODO: Add your control notification handler code here
	
	if(portb & 0x40)
	{
		portb &= 0xbf;
	}else
	{
		portb |= 0x40;
	}
	usb_command(3,portb);
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnPb7() 
{
	// TODO: Add your control notification handler code here
	if(portb & 0x80)
	{
		portb &= 0x7f;
	}else
	{
		portb |= 0x80;
	}
	usb_command(3,portb);
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::usb_command(char usbcmd0,char usbcmd1)
{
    char temp [80],cc[80];
    char wrBuffer[16];
	CAVR_USBDlg* testDlg;
	BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	HANDLE hFile=NULL;
	testDlg=this;
	SetTimer(100,-1,NULL);

	hFile = openUSBfile_0();
	if(hFile == INVALID_HANDLE_VALUE)
	{
		strcpy(temp, "不能写数据到pipe_0!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
		return;
	}
	wrBuffer[0] = usbcmd0;
	wrBuffer[1] = usbcmd1;
	int wrLength=2;
	bResult = WriteFile(hFile,
		&wrBuffer,
		wrLength,
		&nBytes,
		NULL);
				
	if (bResult != TRUE)
	{
		strcpy (cc, "写pipe_0失败! ");
		testDlg->MessageBox((LPCSTR)cc, "写入错误!");
	}
	CloseHandle(hFile);
    if(cOnwendu==1)
	{
		SetTimer(100,1000,NULL); // 1HZ
	}
}

void CAVR_USBDlg::OnWriteCOM() 
{
    char temp [80],cc[80];
	CAVR_USBDlg* testDlg;
	BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	HANDLE hFile=NULL;
	unsigned char rdBuffer[17];
	int rdLength=16;

	HANDLE hCom=NULL;
	int wrLength;
    
	DWORD dwError;
	DCB dcb;

	COMMTIMEOUTS TimeOuts;

	SetTimer(100,-1,NULL);
	usb_command(3,portb);
	
	testDlg=this;

	hFile = openUSBfile_1();
	if(hFile == INVALID_HANDLE_VALUE) {
		strcpy(temp, "不能从 pipe_1 读数据!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
		return;
	}
	hCom=CreateFile("COM1", // 文件名
			GENERIC_READ | GENERIC_WRITE, // 允许读和写 
			0, // 独占方式
			NULL, 
			OPEN_EXISTING, //打开而不是创建
			FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // 重叠方式
			NULL
			);

	if(hCom == INVALID_HANDLE_VALUE)
	{
		dwError=GetLastError( );
		strcpy(temp, "COM1 打开失败!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
		return;
	}
	SetupComm( hCom, 1024, 1024 ); //缓冲区的大小为1024
	PurgeComm( hCom, PURGE_TXABORT | PURGE_RXABORT
		  | PURGE_TXCLEAR | PURGE_RXCLEAR ); //清干净输入、输出缓冲区 


	TimeOuts.ReadIntervalTimeout=0; 
	TimeOuts.ReadTotalTimeoutMultiplier=0; 
	TimeOuts.ReadTotalTimeoutConstant=1000; 
	TimeOuts.WriteTotalTimeoutMultiplier=0; 
	TimeOuts.WriteTotalTimeoutConstant=1000;
	SetCommTimeouts(hCom, &TimeOuts); // 设置超时

	GetCommState(hCom, &dcb);//取得通讯资源当前设置

	dcb.BaudRate = 19200; // 波特率为19200
	dcb.ByteSize = 8; // 每个字符有8位

	dcb.Parity = 0;//NOPARITY; //无校验 1奇校验 2-偶校验

	dcb.fParity = FALSE; 
	dcb.fBinary = TRUE;
	dcb.StopBits=2;//ONESTOPBIT; //0 一个停止位

	SetCommState(hCom, &dcb);//设置端口

	UpdateData(TRUE);
	wrLength = m_COMtoUSB.GetLength();
	if(wrLength>16)wrLength = 16;

 if(wrLength>0)
 {

		
		bResult = WriteFile(hCom,
		m_COMtoUSB, //&rdBuffer,//
		wrLength,//
		&nBytes,
		NULL);
	
		DWORD time0,time1;
		
		time0 = timeGetTime();

		time1 = timeGetTime();
		while((time1-time0)<100)
			time1 = timeGetTime();
		
	if (bResult == TRUE)
	{
			bResult = ReadFile(hFile,
				&rdBuffer,
				rdLength,
				&nBytes,
				NULL);

			if (bResult == TRUE)
			{
				bResult = ReadFile(hFile,
					&rdBuffer,
					rdLength,
					&nBytes,
					NULL);
				if (bResult == TRUE)
				{
					rdBuffer[nBytes] = 0;
					m_USBtoCOM = rdBuffer;
					UpdateData(FALSE);
				}
			}	
		if(bResult != TRUE)
		{
			strcpy (cc, "读 pipe_1 失败! ");
			testDlg->MessageBox((LPCSTR)cc, "读出错误!");
		}
	}else
	{
		strcpy (cc, "写 COM1 失败! ");
		testDlg->MessageBox((LPCSTR)cc, "错误!");
	}
 }
	CloseHandle(hCom);
	CloseHandle(hFile);
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnUSBtoCOM() 
{
    //发送数据到串口命令
	char temp [80],cc[80];
	CString Tstr;
    char wrBuffer[16];
	CAVR_USBDlg* testDlg;
	BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	HANDLE hFile=NULL;
	testDlg=this;
	
	UpdateData(TRUE);
	
	hFile = openUSBfile_0();
	if(hFile == INVALID_HANDLE_VALUE)
	{
		strcpy(temp, "不能写数据到pipe_0!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
		return;
	}
	wrBuffer[0] = 4;
	wrBuffer[1] = 0;
	Tstr = wrBuffer[0]+m_USBtoCOM;
	int wrLength=Tstr.GetLength();
	if(wrLength>16)wrLength=16;

	HANDLE hCom=NULL;
	
	DWORD dwError;
	DCB dcb;

	COMMTIMEOUTS TimeOuts;

	hCom=CreateFile("COM1", // 文件名
			GENERIC_READ | GENERIC_WRITE, // 允许读和写 
			0, // 独占方式
			NULL, 
			OPEN_EXISTING, //打开而不是创建
			FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // 重叠方式
			NULL
			);

	if(hCom == INVALID_HANDLE_VALUE)
	{
		dwError=GetLastError( );
		strcpy(temp, "COM1 打开失败!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
	
		CloseHandle(hFile);
		return;
	}
	SetupComm( hCom, 1024, 1024 ); //缓冲区的大小为1024
	PurgeComm( hCom, PURGE_TXABORT | PURGE_RXABORT
		  | PURGE_TXCLEAR | PURGE_RXCLEAR ); //清干净输入、输出缓冲区 


	TimeOuts.ReadIntervalTimeout=0; 
	TimeOuts.ReadTotalTimeoutMultiplier=0; 
	TimeOuts.ReadTotalTimeoutConstant=500; 
	TimeOuts.WriteTotalTimeoutMultiplier=0; 
	TimeOuts.WriteTotalTimeoutConstant=500;
	SetCommTimeouts(hCom, &TimeOuts); // 设置超时

	GetCommState(hCom, &dcb);

	dcb.BaudRate = 19200; // 波特率为115200
	dcb.ByteSize = 8; // 每个字符有8位

	dcb.Parity = NOPARITY; //无校验
	dcb.fParity = FALSE; 
	dcb.fBinary = TRUE;
	dcb.StopBits=ONESTOPBIT; //一个停止位 0,1,2 = 1, 1.5, 2 

	SetCommState(hCom, &dcb);

	
	bResult = WriteFile(hFile,
		Tstr,
		wrLength,
		&nBytes,
		NULL);
				
	if (bResult != TRUE)
	{
		strcpy (cc, "写pipe_0失败! ");
		testDlg->MessageBox((LPCSTR)cc, "写入错误!");
	}
	CloseHandle(hFile);

	unsigned char rdBuffer[64];
	int rdLength = wrLength-1;
	bResult = ReadFile(hCom,
		&rdBuffer,
		rdLength,
		&nBytes,
		NULL);
				
	if (bResult == TRUE)
	{
		rdBuffer[nBytes] = 0;
		m_COMtoUSB = rdBuffer;
		UpdateData(FALSE);
	}else
	{
		strcpy (cc, "读 COM1 失败! ");
		testDlg->MessageBox((LPCSTR)cc, "读出错误!");
	}
	CloseHandle(hCom);
}

⌨️ 快捷键说明

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