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

📄 inverterdlg.cpp

📁 Control the 24 pulse inverter module dsp based
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}

void CinverterDlg::OnEnChangeEdtRetunit()
{
	// 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
	//UpdateData(1);
	CString a;
	unsigned char h,l,chk;
	GetDlgItem(IDC_EDT_RETUNIT)->GetWindowText(a);
	//m_edtq3.GetWindowText(a);		
	
	if(a.GetLength()==2)
	{
		h=a.GetAt(0);
		l=a.GetAt(1);
		m_serial3=(BYTE)(convert2num(h)*16)+convert2num(l);
	}
	if(a.GetLength()==1)
	{
		h=a.GetAt(0);
		m_serial3=(BYTE)convert2num(h);
	}
	else if(a.GetLength()==0)
	{
		m_serial3=0;
	}
	chk=0;	
	chk=(unsigned char)((UINT)m_serial0+(UINT)m_serial1+(UINT)m_serial2+(UINT)m_serial3+(UINT)m_serial5);
	a.Format("%x",chk);
	GetDlgItem(IDC_STC_CHKSUM)->SetWindowText(a);
	//m_stcchk.Format("%x",chk);
	//UpdateData(0);
}

void CinverterDlg::OnEnChangeEdtPhase()
{
	// 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
	//UpdateData(1);
	CString a;	
	float number=1;
	unsigned char temp[15];	
	unsigned char h,l,chk;
	unsigned char num=0,mantis=0;
	int index;
	bool neg=false,point=false;
	m_edtq4.GetWindowText(a);

	if(!a.IsEmpty())
	{
		a.MakeUpper();

		for(int i=0;i<a.GetLength();i++)
		{
			temp[i]=a.GetAt(i);
		}
		i=0;
		if(temp[0]=='-')
		{
			neg=true;
			i=1;                    
		}
		for(;i<a.GetLength();i++)
		{		
			if(temp[i]=='.')
			{
				point=true;
				index=i;
			}
			else if(point==false)
			{
				num++;
			}
			else
				mantis++;
		}
		number=0;
		if(point==true)
		{
			float temp2=0.1;
			for(i=index+1;i<a.GetLength();i++)
			{
				number+=(temp[i]-'0')*temp2;
				temp2/=10.0;
			}
		}
		else
			index=1;

		if(neg==true)
		{
			float temp2=1;
			for(i=num;i>=1;i--)
			{
				number+=(temp[i]-'0')*temp2;
				temp2*=10.0;
			}
			number*=-1.0;
		}
		else
		{
			float temp2=1;
			for(i=num-1;i>=0;i--)
			{
				number+=(temp[i]-'0')*temp2;
				temp2*=10.0;
			}
		}
		if(number>180.0)
		{
			MessageBox("Phase must be in the [-180 180] range","Out of range");
			return;
		}
		else if(number<-180.0)
		{
			MessageBox("Phase must be in the [-180 180] range","Out of range");
			return;
		}
		else
		{
			if(number>=0)
			{
				 m_serial5=(unsigned char)(number/0.036);
				 m_serial4=(unsigned char)((number/0.036)/256.0);
			}
			else
			{
				number*=-1.0;
				m_serial5=5000+(unsigned char)(number/.036);
				m_serial4=5000+(unsigned char)((number/0.036)/256.0);
			}
		}
		chk=0;
		chk=(unsigned char)((UINT)m_serial0+(UINT)m_serial1+(UINT)m_serial2+(UINT)m_serial3+(UINT)m_serial5);
		m_stcchk.Format("%x",chk);
	}
	else
	{
		m_serial5=0;
		m_serial4=0;
		chk=0;
		chk=(unsigned char)((UINT)m_serial0+(UINT)m_serial1+(UINT)m_serial2+(UINT)m_serial3+(UINT)m_serial5);
		m_stcchk.Format("%x",chk);
	}
	//UpdateData(0);
}
//void CinverterDlg::evaluatechecksum(void)

int CinverterDlg::convert2num(char ch)
{
	if(ch>='0' && ch<='9')
		return ch-'0';
	else if(ch>='a' && ch<='f')
		return (ch-'a'+10);
	else if(ch>='A' && ch<='F')
		return (ch-'A'+10);
	else
		return 0;

}

//int CinverterDlg::OnInitDialog()


int CinverterDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	m_serial0=0xaa;
	m_serial1=0xff;
	m_serial2=0;
	m_serial3=0;
	m_serial4=0;
	m_serial5=0;
	
	rxCounter=0;
	txCounter=0;

	SetTimer(nonPreceiseTimer,500,NULL);

	// TODO:  Add your specialized creation code here
	return 0;
}

void CinverterDlg::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	HANDLE hCom;
	DCB dcb;
	COMMTIMEOUTS comTimeOut;
	CString aaa[4];	
	char l_buffer_transmit[10];
	char l_buffer_receive[7];
	unsigned char m_serial[7];
	int checksum=0;
	int i;
	DWORD writesize;
	DWORD readsize;

	if(startTransmit==TRUE)
	{		
		UpdateData(TRUE);
		hCom=CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
		if (hCom == INVALID_HANDLE_VALUE)
		{
			CDialog::OnTimer(nIDEvent);
			return;
		}
		GetCommState(hCom, &dcb);
		dcb.BaudRate = 19200;
		dcb.ByteSize = 8;
		dcb.Parity = NOPARITY;
		dcb.StopBits = ONESTOPBIT;
		SetCommState(hCom, &dcb);
		comTimeOut.ReadIntervalTimeout=1;
		comTimeOut.ReadTotalTimeoutMultiplier=1;
		comTimeOut.ReadTotalTimeoutConstant = 1;
		comTimeOut.WriteTotalTimeoutMultiplier = 1;
		comTimeOut.WriteTotalTimeoutConstant = 1;
		SetCommTimeouts(hCom,&comTimeOut);

		m_serial[0]=m_serial0;		//0xAA
		m_serial[1]=m_serial1;		//0xff
		m_serial[2]=m_serial2;		//Command
		m_serial[3]=m_serial3;		//Unit id
		m_serial[4]=m_serial4;		//Phase1
		m_serial[5]=m_serial5;		//Phase2
		
		m_serial[6]=(BYTE)(m_serial0+m_serial1+m_serial2+m_serial3+m_serial4+m_serial5);		//Cheksum	


		WriteFile(hCom, m_serial, 7, &writesize, NULL);
		FlushFileBuffers(hCom);
		txCounter++;

		i=0;
		do
		{
			if (i > 0)
				Sleep(1);
			ReadFile(hCom,l_buffer_receive,7,&readsize,NULL);
		}
		while (readsize != 7 && ++i <4);
		int chk=0;
		for(i=0;i<6;i++)
		{
			chk+=l_buffer_receive[i];
		}
		aaa[0]="NAN";aaa[1]="NAN";aaa[2]="NAN";aaa[3]="NAN";
		if(readsize==7 && (BYTE)l_buffer_receive[6]==(BYTE)chk)
		{
			rxCounter++;			
			aaa[0].Format("%2x",(BYTE)l_buffer_receive[0]);
			//m_q10.Format("%2x",(BYTE)l_buffer_receive[0]);
			//m_q11.Format("%2x",(BYTE)l_buffer_receive[1]);
            aaa[1].Format("%2x",(BYTE)l_buffer_receive[1]);

			//m_q13.Format("%2x",(BYTE)l_buffer_receive[2]);
			aaa[2].Format("%2x",(BYTE)l_buffer_receive[2]);

			float temp=0;
			temp=(float)(l_buffer_receive[4])*256 +(float)(l_buffer_receive[3]);
			temp=6.0*temp*.002;
			////m_q12.Format("%f",temp);
			//aaa[3].Format("%f",temp);
			aaa[3].Format("%x %x %f",l_buffer_receive[4],l_buffer_receive[3],temp);
		}
		else
		{
			m_q10="NaN";
			m_q11="NaN";
			m_q12="NaN";
			m_q13="NaN";
		}
		CString temp1;		
		//temp1.Format("%X",m_serial0);m_edtq0.SetWindowText(temp1.GetString());
		//temp1.Format("%X",m_serial1);m_edtq1.SetWindowText(temp1.GetString());
		//temp1.Format("%X",m_serial2);m_edtq2.SetWindowText(temp1.GetString());
		//temp1.Format("%X",m_serial3);m_edtq3.SetWindowText(temp1.GetString());
		//temp1.Format("%X",m_serial4);m_edtq4.SetWindowText(temp1.GetString());
		
		//temp1.Format("%X",m_serial0);GetDlgItem(IDC_EDT_HEADER0)->SetWindowText(temp1);
		//temp1.Format("%X",m_serial1);GetDlgItem(IDC_EDT_HEADER2)->SetWindowText(temp1);
		//temp1.Format("%X",m_serial2);GetDlgItem(IDC_EDT_CMD)->SetWindowText(temp1);
		//temp1.Format("%X",m_serial3);GetDlgItem(IDC_EDT_RETUNIT)->SetWindowText(temp1);
		//temp1.Format("%X",m_serial4);GetDlgItem(IDC_EDT_PHASE)->SetWindowText(temp1);
		GetDlgItem(IDC_STC_HEADER0)->SetWindowText(aaa[0]);
		GetDlgItem(IDC_STC_HEADER1)->SetWindowText(aaa[1]);
		GetDlgItem(IDC_STC_PERIOD)->SetWindowText(aaa[3]);
		GetDlgItem(IDC_STC_UNITID)->SetWindowText(aaa[2]);

		temp1.Format("%d",txCounter);GetDlgItem(IDC_STC_TXCOUNTER)->SetWindowText(temp1);
		temp1.Format("%d",rxCounter);GetDlgItem(IDC_STC_RXCOUNTER)->SetWindowText(temp1);
		
		//m_txcounter.Format("%d",txCounter);
		//m_rxcounter.Format("%d",rxCounter);
		
		//UpdateData(FALSE);
		CloseHandle(hCom);
	}
	CDialog::OnTimer(nIDEvent);
}

void CinverterDlg::OnDestroy()
{
	CDialog::OnDestroy();
	KillTimer(nonPreceiseTimer);
	// TODO: Add your message handler code here
}

⌨️ 快捷键说明

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