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

📄 sdsddlg.cpp

📁 RS232
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		m_Com.SetOutput(COleVariant(array));
	}
	else
	{
		::MessageBox(NULL,"请先打开串口,再进行此操作!","提示!",MB_OK | MB_ICONWARNING);
	}	
}

void CMyDlg::SetComState()
{
	CString StateBuf="";
	ComState = "";
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->GetLBText(((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->GetCurSel(),StateBuf);
	ComState += StateBuf+',';
	((CComboBox*)GetDlgItem(IDC_COMBO_Parity))->GetLBText(((CComboBox*)GetDlgItem(IDC_COMBO_Parity))->GetCurSel(),StateBuf);
	ComState += StateBuf.Mid(1,1)+',';
	((CComboBox*)GetDlgItem(IDC_COMBO_ByteSize))->GetLBText(((CComboBox*)GetDlgItem(IDC_COMBO_ByteSize))->GetCurSel(),StateBuf);
	ComState += StateBuf+',';
	((CComboBox*)GetDlgItem(IDC_COMBO_StopBits))->GetLBText(((CComboBox*)GetDlgItem(IDC_COMBO_StopBits))->GetCurSel(),StateBuf);
	ComState += StateBuf;
//	AfxMessageBox(ComState);
	m_Com.SetSettings(ComState);

}

void CMyDlg::OnSelchangeCOMBOBaudRate() 
{
	// TODO: Add your control notification handler code here
	if(m_Com.GetPortOpen())
		SetComState();		
}

void CMyDlg::OnSelchangeCOMBOByteSize() 
{
	// TODO: Add your control notification handler code here
	if(m_Com.GetPortOpen())
		SetComState();		
}

void CMyDlg::OnSelchangeCOMBOParity() 
{
	// TODO: Add your control notification handler code here
	if(m_Com.GetPortOpen())
		SetComState();		
}

void CMyDlg::OnSelchangeCOMBOStopBits() 
{
	// TODO: Add your control notification handler code here
	if(m_Com.GetPortOpen())
		SetComState();		
}

BEGIN_EVENTSINK_MAP(CMyDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CMyDlg)
	ON_EVENT(CMyDlg, IDC_MSCOMM, 1 /* OnComm */, OnOnCommMscomm, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CMyDlg::OnOnCommMscomm() 
{
	// TODO: Add your control notification handler code here
	CString strtemp;
	if(m_Com.GetCommEvent()==2)
	{
		int len=m_Com.GetInBufferCount();
		if(len>0)
		{
			VARIANT vResponse=m_Com.GetInput();
			COleSafeArray safearray_inp=vResponse;
			BYTE rxdata[4096];
			for(long k=0;k<len;k++)
				safearray_inp.GetElement(&k,rxdata+k);
			for(k=0;k<len;k++)
			{
				BYTE bt=*(unsigned char*)(rxdata+k);
//				if(!m_HexShow)
					strtemp.Format("%c",bt);
//				else
//					strtemp.Format("%02X ",bt);
				m_ReceiveBuffer = m_ReceiveBuffer + strtemp;
			}
		}
		NewReceive=1;
	}
}


void CMyDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}



void CMyDlg::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	ComState = m_Com.GetSettings();
	CDialog::OnClose();
}

void CMyDlg::OnClearRxwnd()
{
	// TODO: Add your control notification handler code here
	m_ReceiveData="";
	m_ReceiveBuffer="";
//	((CBitmapButton *)GetDlgItem(IDC_CLEAR_RXWND))->LoadBitmaps(IDB_BITMAP,IDB_BITMAP,IDB_BITMAP,IDB_BITMAP);
	//((CBitmapButton *)GetDlgItem(IDC_CLEAR_RXWND))->Invalidate(true);
	//((CBitmapButton *)GetDlgItem(IDC_CLEAR_RXWND))->SizeToContent();

	UpdateData(FALSE);
}

void CMyDlg::OnRadioHexshow()
{
	// TODO: Add your control notification handler code here
	m_HexShow = TRUE;
	NewReceive =1;
}


void CMyDlg::OnRadioHexsend()
{
	// TODO: Add your control notification handler code here
	m_HexSend = TRUE;
	((CEdit*)GetDlgItem(IDC_EDIT_SENDDATA))->SetFocus();
	UpdateData(FALSE);
	((CEdit*)GetDlgItem(IDC_EDIT_SENDDATA))->SetSel(-1);
}

void CMyDlg::OnRadioStrsend()
{
	// TODO: Add your control notification handler code here
	m_HexSend = FALSE;
	((CEdit*)GetDlgItem(IDC_EDIT_SENDDATA))->SetFocus();
	UpdateData(FALSE);
	((CEdit*)GetDlgItem(IDC_EDIT_SENDDATA))->SetSel(-1);
}

void CMyDlg::OnRadioStrshow()
{
	// TODO: Add your control notification handler code here
	m_HexShow  = FALSE;
	NewReceive =1;
}

void CMyDlg::OnUpdateEditSenddata() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	char str[3]="";
	CString String="错误的十六进制代码:";
	DWORD j = ((CEdit*)GetDlgItem(IDC_EDIT_SENDDATA))->GetSel();
//	if(	((i & 0xffff0000)>>16) != (i & 0x0000ffff))
//		m_SendData.Delete((i & 0x0000ffff),((i & 0xffff0000)>>16)-(i & 0x0000ffff));
	j &= 0x0000ffff;

	UpdateData();
	if(m_HexSend)
	for(unsigned int i=0;i<strlen(m_SendData);i++)
	{
		if(
			!(	m_SendData.GetAt(i) >= '0' && m_SendData.GetAt(i) <= '9'	||
				m_SendData.GetAt(i) >= 'a' && m_SendData.GetAt(i) <= 'f'	||
				m_SendData.GetAt(i) >= 'A' && m_SendData.GetAt(i) <= 'F'	||
				m_SendData.GetAt(i) == VK_SPACE)
			)
		{
			itoa(m_SendData.GetAt(i),str,10);
			String+=str;
			String+=',';
			j -= 1;
			m_SendData.Delete(i);
			UpdateData(FALSE);
			((CEdit*)GetDlgItem(IDC_EDIT_SENDDATA))->SetSel(j,j,TRUE);
			::MessageBox(NULL,String,"警告",MB_OK | MB_ICONWARNING);
		}
	}	
}
/**************************************************/

void CMyDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	Invalidate();
}

void CMyDlg::OnTimer(UINT nIDEvent) 
{
	CString strtemp;
	int flag=1;
	// TODO: Add your message handler code here and/or call default
	if(NewReceive/*strcmp(m_ReceiveData,m_ReceiveBuffer)*/)
	{
		m_ReceiveData="";
		for(unsigned int i=0;i<strlen(m_ReceiveBuffer);i++)
		{
			if(!m_HexShow)
			{
				if(m_ReceiveBuffer.GetAt(i)=='\n') 
				{
					m_ReceiveData += "\r\n";
					flag=0;
				}
				else 
					strtemp.Format("%c",m_ReceiveBuffer.GetAt(i));
			}
			else
				strtemp.Format("%02X ",(unsigned char)m_ReceiveBuffer.GetAt(i));
			if(flag)m_ReceiveData += strtemp;
			flag=1;
		}
		((CEdit*)GetDlgItem(IDC_EDIT_RECEIVEDATA))->SetFocus();
		UpdateData(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_RECEIVEDATA))->SetSel(-1);	
//			ShowWave();
		NewReceive=0;
	}
//	CDialog::OnTimer(nIDEvent);
}


void CMyDlg::GetPortNum()
{
	CString StateBuf="";
//	ComState = "";
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->GetLBText(((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->GetCurSel(),StateBuf);
	m_Port = StateBuf.GetAt(3)-'0';
}

void CMyDlg::OnSelchangeComboPort() 
{
	// TODO: Add your control notification handler code here
	if(!m_Com.GetPortOpen())
	{
		GetPortNum();
		m_Com.SetCommPort(m_Port);
		m_Com.SetInBufferSize(4096);
		m_Com.SetOutBufferSize(4096);
		m_Com.SetPortOpen(TRUE);
		m_Com.SetInputMode(1);
		SetComState();
		//m_Com.SetSettings(ComState);
		m_Com.SetRThreshold(1);
		m_Com.SetInputLen(0);
		if(m_Com.GetPortOpen())
			((CEdit*)GetDlgItem(IDC_OPEN_COM))->SetWindowText("关闭串口");
		else
			m_Com.SetPortOpen(FALSE);
	}
}

void CMyDlg::OnButtonPlay() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow ( TRUE );
		if ( !m_HwMCI.DeviceIDValid () )
	{
		if ( !m_HwMCI.Open ( "test.wav" ) )
			return;
	}

	ASSERT ( m_HwMCI.DeviceIDValid () );
	m_HwMCI.Play ();
	//GetDlgItem(IDC_BUTTON_STOP)->EnableWindow ( FALSE );
	
}

void CMyDlg::OnButtonRec() 
{
	// TODO: Add your control notification handler code here
	UpdateData ( TRUE );
	GetDlgItem(IDC_BUTTON_REC)->EnableWindow ( FALSE );
	GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow ( FALSE );
	if(((CComboBox*)GetDlgItem(IDC_COMBO_Channels))->GetCurSel()==0)
	{
		m_nChannels=1;
	}
	else
	{
		m_nChannels=2;
	}
	if(((CComboBox*)GetDlgItem(IDC_COMBO_SamplesPerSec))->GetCurSel()==0)
	{
		m_nSamplesPerSec=8000;
	}
	else if(((CComboBox*)GetDlgItem(IDC_COMBO_SamplesPerSec))->GetCurSel()==1)
	{
		m_nSamplesPerSec=11025;
	}
	else
	{
		m_nSamplesPerSec=44100;
	}
	if(((CComboBox*)GetDlgItem(IDC_COMBO_BitsPerSample))->GetCurSel()==0)
	{
		m_wBitsPerSample=8;
	}
	else
	{
		m_wBitsPerSample=16;
	}
	//GetDlgItem(IDC_BUTTON_SEND)->EnableWindow ( TRUE );
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow ( TRUE );
	m_HwMCI.Rec ( m_nChannels,m_nSamplesPerSec,m_wBitsPerSample);
	Sleep(2000);
		m_HwMCI.Stop ();
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow ( FALSE );
	if ( m_HwMCI.Save ( "test.wav" ) )
	{
		//MessageBox ( "Save wave file to [test.wav] success" );
	}
	GetDlgItem(IDC_BUTTON_REC)->EnableWindow ( TRUE );
	GetDlgItem(IDC_BUTTON_SEND)->EnableWindow ( TRUE );
	GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow ( TRUE );
	
}

void CMyDlg::OnButtonSend() 
{
	unsigned long j=0;
	char *strbuf;
	CByteArray array;
	CString str;
	CFile f;

	m_HwMCI.Stop ();
	f.Open("test.wav",CFile::modeReadWrite);
	f.Read(str.GetBuffer(f.GetLength()),f.GetLength());
	if(m_Com.GetPortOpen())
	{

		UpdateData();
		strbuf=str.GetBuffer(f.GetLength());

		array.RemoveAll();
	
		array.SetSize(f.GetLength());
		for(j=0;j<f.GetLength();j++)
			array.SetAt(j,strbuf[j]);
		m_Com.SetOutput(COleVariant(array));
		f.Close();
	}
	else
	{
		::MessageBox(NULL,"请先打开串口,再进行此操作!","提示!",MB_OK | MB_ICONWARNING);
	}	
	

}

void CMyDlg::OnButtonStop() 
{
	// TODO: Add your control notification handler code here
	m_HwMCI.Stop ();
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow ( FALSE );
	if ( m_HwMCI.Save ( "test.wav" ) )
	{
		//MessageBox ( "Save wave file to [test.wav] success" );
	}
	GetDlgItem(IDC_BUTTON_REC)->EnableWindow ( TRUE );
	GetDlgItem(IDC_BUTTON_SEND)->EnableWindow ( TRUE );
	GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow ( TRUE );
	
}
void CMyDlg::OnButtonTest() 
{
	ShellExecute(NULL,"open","测试.exe",NULL,NULL,SW_SHOWNORMAL);

	
}

⌨️ 快捷键说明

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