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

📄 mcu2pcdlg.cpp

📁 基于VC++6.0应用MSCOMM控件的串口通信上位机程序,实现与51单片机的通信
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		{
			BYTE bt=*(char *)(rxdata+k);
			strtemp.Format("%02x",bt);
			if(m_bStopdis)
			{
				//		m_getstr=strtemp;
			m_getstr=m_getstr+' '+strtemp;
			}
		}
		if(m_bStopdis)
			{
				m_getstr+="\r\n";
				m_strreceive=m_getstr;
			}
		
		m_Port.SetInBufferCount(0);
		DealWithData();//外理数据
	
		
	}

	if(m_bStopdis)
		{
			UpdateData(FALSE);
		}
//	m_Port.SetInputLen(0);	m_Port.SetOutBufferCount(0)
; 
}

void CMcu2pcDlg::DealWithData()
{
//	float cur,vol;
//		CString hr,min,sec;;
	//	m_getstr.Empty();
		//UpdateData(FALSE);

		CString strID;
		CString strtemp;
		strtemp="";
		//unsigned int money;
		char temp_h,temp_l;
		if(rxdata[3]==0x12)	//读卡完毕
		{
				temp_h=Num2Char(rxdata[4]>>4);				
				temp_l=Num2Char(rxdata[4]&0x0f);
				strtemp+=temp_h;				
				strtemp+=temp_l;

				temp_h=Num2Char(rxdata[5]>>4);				
				temp_l=Num2Char(rxdata[5]&0x0f);
				strtemp+=temp_h;
				strtemp+=temp_l;

				temp_h=Num2Char(rxdata[6]>>4);				
				temp_l=Num2Char(rxdata[6]&0x0f);
				strtemp+=temp_h;
				strtemp+=temp_l;

				temp_h=Num2Char(rxdata[7]>>4);				
				temp_l=Num2Char(rxdata[7]&0x0f);
				strtemp+=temp_h;
				strtemp+=temp_l;

				temp_h=Num2Char(rxdata[8]>>4);				
				temp_l=Num2Char(rxdata[8]&0x0f);
				strtemp+=temp_h;
				strtemp+=temp_l;

				m_id=strtemp;	//卡标识
				m_money=(float)(rxdata[12]*256+rxdata[13])/10;	//钱				
		}else if(rxdata[3]==0x34)	//写卡完毕
			{
				MessageBox("写卡完毕");
				OnRd();
			}else if(rxdata[3]==0x56)	//读卡错误
			{
				MessageBox("读卡错误,不可识别的卡或已损坏的卡!");	
			}else if(rxdata[3]==0x78)	//写卡错误
			{
				MessageBox("写卡错误,请确认IC卡没有损坏!");
			}else if(rxdata[3]==0x9a)	//钱不够
			{
				MessageBox("卡上没有足够的余额,本次消费失败,请及时充值!");	
				//OnRd(); 
			}else if(rxdata[3]==0xbc)
			{
				MessageBox("本次充值失败\n\r可能的原因是:\n\r1.超出充值范围,请减少充值金额!\n\r2.这张卡已损坏!");
				//OnRd() ;
			}

		UpdateData(FALSE);
}

void CMcu2pcDlg::OnOpencom() 
{
	// TODO: Add your control notification handler code here
	m_bOpenCom=!m_bOpenCom;
	if(!m_bOpenCom)//关闭串口
	{
		m_SetIcon.SetIcon(m_hIconOff);//红灯亮
		m_OpenCom.SetWindowText("打开串口");
		m_Port.SetPortOpen(FALSE);//关闭串口
		m_status.SetWindowText("串口已关闭");
		//UpdateData(FALSE);	
	}else 
	{
		m_SetIcon.SetIcon(m_hIconRed);//红灯亮
		m_OpenCom.SetWindowText("关闭串口");
		m_Port.SetPortOpen(TRUE);//打开串口
	//	UpdateData(FALSE);
		UpdateStatusEdit();
	}

}



void CMcu2pcDlg::OnBtnClrRec() 
{
	// TODO: Add your control notification handler code here
	m_strreceive.Empty();
	m_getstr.Empty();
	UpdateData(FALSE);
}

void CMcu2pcDlg::OnBtnStopdis() 
{
	// TODO: Add your control notification handler code here
	m_bStopdis=!m_bStopdis;
	if(m_bStopdis)
	{
		m_ctrlStopDis.SetWindowText("停止显示");
	}else
	m_ctrlStopDis.SetWindowText("继续显示");

}

void CMcu2pcDlg::OnBtnClrSnd() 
{
	// TODO: Add your control notification handler code here
//	m_strsend.Empty();
	UpdateData(FALSE);
}

int CMcu2pcDlg::String2Hex(CString str, CByteArray &senddata)
{
		int hexdata,lowhexdata;
	int hexdatalen=0;
	int len=str.GetLength();//获取字符串长

	senddata.SetSize(len/2);//设定十六进制数组长度
		
	for(int i=0;i<len;)
	{
		char lstr;
		char hstr=str[i];
		if(hstr==' ')//字符串结束
		{
			i++;
			continue;//结束本次循环
		}
		i++;
		if(i>len)		
			break;	//字符串转换完毕,结束
		lstr=str[i];

		hexdata=ConvertHexChar(hstr);
		lowhexdata=ConvertHexChar(lstr);

		if((hexdata==16)||(lowhexdata==16))
			break;
		else 
			hexdata=hexdata*16+lowhexdata;
		i++;
		senddata[hexdatalen]=hexdata;
		hexdatalen++;//下一个十六进制数据
	}
	senddata.SetSize(hexdatalen);//设定十六进制数据个数
	return hexdatalen;
}

char CMcu2pcDlg::ConvertHexChar(char ch)
{
		if('0'<=ch&&ch<='9')
		return ch-'0';
	else if('A'<ch&&ch<='F')
		return ch-'A'+10;
	else if('a'<ch&&ch<='f')
		return ch-'a'+10;
	else return -1;
}

//DEL void CMcu2pcDlg::Num2Char(BYTE num, char ch)
//DEL {
//DEL 	if(num<=9)
//DEL 		ch=num+'0';
//DEL 	else ch=num-10+'a';
//DEL }


//DEL void CMcu2pcDlg::OnWr() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	CByteArray hexdata;
//DEL 	//int len=String2Hex(m_strsend,hexdata);
//DEL 	hexdata.SetSize(3);
//DEL 	hexdata[0]=0x45;
//DEL 	hexdata[1]=0x45;
//DEL 	hexdata[2]=0x45;
//DEL 	m_Port.SetOutput(COleVariant(hexdata));//以十六进制格式化发送
//DEL }


void CMcu2pcDlg::OnRd() 
{
	// TODO: Add your control notification handler code here
	CByteArray hexdata;
	//int len=String2Hex(m_strsend,hexdata);
	hexdata.SetSize(14);
	hexdata[0]=0x45;
	hexdata[1]=0x45;
	hexdata[2]=0x45;
	hexdata[3]=0x45;

	m_Port.SetOutput(COleVariant(hexdata));//以十六进制格式化发送	
}

void CMcu2pcDlg::OnWr() 
{
	// TODO: Add your control notification handler code here
		// TODO: Add your control notification handler code here
	CByteArray hexdata;
	CByteArray strGet;
	int i;
	unsigned int uimoney;
	CString str;
	int flag;
	UpdateData(TRUE);//更新数据
	str=m_id;
	for(i=0;i<str.GetLength();i++)
	{	
		if(!((str[i]<='9' &&str[i]>='0') || (str[i]<='f' &&str[i]>='a') || (str[i]<='F' &&str[i]>='A') ))
		{
			flag=1;
		}else flag=0;
	}
		if(flag==1)
		{
			MessageBox("请输入十位0~F的\"数字\"");			
		}
		else
		{
			if(str.GetLength()!=10)MessageBox("请输入十位0~F的\"数字\"");
			else
			{
				
			
					hexdata.SetSize(14);				

					hexdata[0]=0x42;
					hexdata[1]=0x45;
					hexdata[2]=0x42;
					hexdata[3]=0x67;

					hexdata[4]=ConvertHexChar(str[0])*16+ConvertHexChar(str[1]);		
					hexdata[5]=ConvertHexChar(str[2])*16+ConvertHexChar(str[3]);
					hexdata[6]=ConvertHexChar(str[4])*16+ConvertHexChar(str[5]);	
					hexdata[7]=ConvertHexChar(str[6])*16+ConvertHexChar(str[7]);	
					hexdata[8]=ConvertHexChar(str[8])*16+ConvertHexChar(str[9]);	
				

					hexdata[9]=1;
					hexdata[10]=2;
					hexdata[11]=3;
					uimoney=(unsigned int)(m_money*10);

					hexdata[12]=uimoney>>8;
					hexdata[13]=uimoney&0xff;
					//hexdata[13]=strGet[10];
				//	m_Port.SetOutBufferCount(0);
					m_Port.SetOutput(COleVariant(hexdata));//以十六进制格式化发送
			}

		}

}

char CMcu2pcDlg::Num2Char(unsigned char ch)
{
	if(ch<=9)
 		return (ch+'0');
 	else return (ch-10+'a');
}

void CMcu2pcDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	CByteArray hexdata;

	unsigned int uimoney;


	UpdateData(TRUE);//更新数据


		if(m_money2<=6553.5 && m_money2>=0)
		{

					hexdata.SetSize(14);				

					hexdata[0]=0x49;	//充钱命令
					hexdata[1]=0x42;
					hexdata[2]=0xab;
					hexdata[3]=0xab;

					uimoney=(unsigned int)(m_money2*10);

					hexdata[4]=uimoney>>8;
					hexdata[5]=uimoney&0xff;
				
					//hexdata[13]=strGet[10];
				//	m_Port.SetOutBufferCount(0);
					m_Port.SetOutput(COleVariant(hexdata));//以十六进制格式化发送	
		}else
		{
			MessageBox("请入一个0~6553.5的数!");
		}
	



}

void CMcu2pcDlg::OnDec() 
{
	// TODO: Add your control notification handler code here
		CByteArray hexdata;

	unsigned int uimoney;

	UpdateData(TRUE);//更新数据

		if(m_money2<=6553.5 && m_money2>=0)
		{
				hexdata.SetSize(14);				

				hexdata[0]=0x45;	//充钱命令
				hexdata[1]=0x42;
				hexdata[2]=0x45;
				hexdata[3]=0x89;

				uimoney=(unsigned int)(m_money2*10);

				hexdata[4]=uimoney>>8;
				hexdata[5]=uimoney&0xff;
			
				//hexdata[13]=strGet[10];
				//m_Port.SetOutBufferCount(0);
				m_Port.SetOutput(COleVariant(hexdata));//以十六进制格式化发送
		}else
		{
			MessageBox("请入一个0~6553.5的数!");
		}
	
}

void CMcu2pcDlg::OnChangeAddDec() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here

}

void CMcu2pcDlg::OnChangeId() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CMcu2pcDlg::OnChangeEdit1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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