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

📄 crc_uidlg.cpp

📁 该工具用于数字电路仿真中的usb数据文件转换: 1.可以把11比特的令牌包数据(crc5)或者任意字节的数据包数据(crc16)转换为可以作为仿真输入的dn dp文件。 2.可以根据仿真接收到的d
💻 CPP
字号:
// crc_uiDlg.cpp : implementation file
//

#include "stdafx.h"
#include "crc_ui.h"

#include <string.h>
#include "crc_func.h"
#include "crc_uiDlg.h"
#include "global.h"
#include ".\crc_uidlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern struct trans_info transfer_info;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCrc_uiDlg dialog

CCrc_uiDlg::CCrc_uiDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCrc_uiDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCrc_uiDlg)
	m_data_to_trans = _T("");
	m_pathname = _T("");
	m_dlg_input = FALSE;
	m_file_input = FALSE;
	m_file_rx = _T("");
	m_dns = _T("");
	m_dps = _T("");
	m_rxd = _T("");
	m_delay = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCrc_uiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCrc_uiDlg)
	DDX_Control(pDX, IDC_MODE, m_mode);
	DDX_Control(pDX, IDC_DIREC, m_direction);
	DDX_Control(pDX, IDC_SPEED, m_speed_mode);
	DDX_Text(pDX, IDC_DATA, m_data_to_trans);
	DDV_MaxChars(pDX, m_data_to_trans, 32768);
	DDX_Text(pDX, IDC_FILE_FOR_TRANSFER, m_pathname);
	DDX_Check(pDX, IDC_DLG_INPUT, m_dlg_input);
	DDX_Check(pDX, IDC_FILE_INPUT, m_file_input);
	DDX_Text(pDX, IDC_FILE_FOR_RX, m_file_rx);
	DDX_Text(pDX, IDC_DN, m_dns);
	DDV_MaxChars(pDX, m_dns, 512);
	DDX_Text(pDX, IDC_DP, m_dps);
	DDV_MaxChars(pDX, m_dps, 512);
	DDX_Text(pDX, ID_RXD, m_rxd);
	DDV_MaxChars(pDX, m_rxd, 512);
	DDX_Text(pDX, IDC_DELAY, m_delay);
	DDV_MaxChars(pDX, m_delay, 512);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCrc_uiDlg, CDialog)
	//{{AFX_MSG_MAP(CCrc_uiDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_FILE, OnGetFile)
	ON_BN_CLICKED(IDC_TRANSFER, OnTransfer)
	ON_BN_CLICKED(IDC_FILE_INPUT, OnFileInputSelected)
	ON_BN_CLICKED(IDC_DLG_INPUT, OnDlgInputSelected)
	ON_BN_CLICKED(IDC_FILE2, OnFileForRX)
	//}}AFX_MSG_MAP
	ON_CBN_SELCHANGE(IDC_COMBO1, OnCbnSelchangeCombo1)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCrc_uiDlg message handlers

BOOL CCrc_uiDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
		// TODO: Add extra initialization here

	
	
	//init trans mode select
	CString transmode[2]={"CRC 5","CRC 16"};
	
	m_mode.AddString(transmode[1]);
	m_mode.AddString(transmode[0]);
	

	m_mode.SetCurSel(0);


	CString direct[2]={"TX","RX"};
	m_direction.AddString(direct[1]);
	m_direction.AddString(direct[0]);
	m_direction.SetCurSel(1);

	CString speed[2]={"HIGH","FULL"};
	m_speed_mode.AddString(speed[1]);
	m_speed_mode.AddString(speed[0]);
	m_speed_mode.SetCurSel(1);
	
	((CButton*)GetDlgItem(IDC_DLG_INPUT))->SetCheck(FALSE);
	dlg_input_select = UNSELECT_ALREADY;
	((CButton*)GetDlgItem(IDC_FILE_INPUT))->SetCheck(TRUE);
	file_input_select = SELECT_ALREADY;
	
	GetDlgItem(IDC_DN)->SetWindowText("rxdn = ");	
	GetDlgItem(IDC_DP)->SetWindowText("rxdp = ");	
	GetDlgItem(ID_RXD)->SetWindowText("rxd = ");	
	GetDlgItem(IDC_DELAY)->SetWindowText("#(8*`HALF_PERIOD)");	

	


	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCrc_uiDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CCrc_uiDlg::OnDestroy()
{
	WinHelp(0L, HELP_QUIT);
	CDialog::OnDestroy();
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCrc_uiDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCrc_uiDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCrc_uiDlg::OnGetFile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
	if(dlg.DoModal()==IDOK)
		m_pathname = dlg.GetPathName();
	//MessageBox(m_pathname);
	SetDlgItemText(IDC_FILE_FOR_TRANSFER,m_pathname);

}

void CCrc_uiDlg::debug( char* info,int dat)
{
	CString out;
	out.Format("%s 0x%x",info,dat);
	AfxMessageBox(out);
}

void CCrc_uiDlg::debug_string( char* info )
{
	CString out;
	out.Format("%s ",info);
	AfxMessageBox(out);
}

void CCrc_uiDlg::move_to_tmp_file_for_editbox(void)
{
	char   aa[256];  
	int len;
	CFile file_tmp;
	
	file_tmp.Open(FILE_FOR_TMP_DATA, CFile::modeCreate | CFile::modeWrite);
	
	len = GetDlgItem(IDC_DATA)->GetWindowTextLength();
	//debug("len is",len);
	if(len >= 256){
		GetDlgItemText(IDC_DATA,aa,256);
		file_tmp.Write(aa, 256);
		len -= 256;
	}
	GetDlgItemText(IDC_DATA,aa,len+1);
	//debug("write len is",strlen(aa));
	//debug(aa);
	file_tmp.Write(aa, strlen(aa));
	len = 0;
	file_tmp.Close();
	//debug_string("ok");
			
}

void CCrc_uiDlg::move_to_tmp_file_for_rx(void)
{
	CFile file_tmp;
	CFile file_result;
	file_result.Open(transfer_info.source_for_rx, CFile::modeRead); 
	
	if(transfer_info.speed == HIGH_SP){
		if(transfer_info.mode == CRC16_FLAG)
			file_tmp.Open(RX_16_HS, CFile::modeCreate | CFile::modeWrite);
		else if(transfer_info.mode == CRC5_FLAG)
			file_tmp.Open(RX_5_HS, CFile::modeCreate | CFile::modeWrite);
	}
	else if(transfer_info.speed == FULL_SP){
		if(transfer_info.mode == CRC16_FLAG)
			file_tmp.Open(RX_16_FS, CFile::modeCreate | CFile::modeWrite);
		else if(transfer_info.mode == CRC5_FLAG)
			file_tmp.Open(RX_5_FS, CFile::modeCreate | CFile::modeWrite);
	}
	file_tmp.Write("\n", 1);
	int n_dn,n_dp, n, range;
	char dat[2] = "";
	n = 0;
	range = 0;
	//debug_string(transfer_info.source_for_rx);
	do{
	//	debug_string("ok");
		
		file_result.Read(dat, 1);
		if(*dat == '0' || *dat == '1')
			switch(range){
				case 0: n_dn = n; 
						range++;
						break;
				case 1: n_dp = n; 
						range++;
						break;
			//	case 2: n_rxd = n; 
			//			break;
				default:break;
		}
		//debug_string("ok");
		n++;
	}while(*dat != '\n'); // n is length of one row
	file_result.SeekToBegin();
	int num = 0;
	int len = file_result.GetLength();
	do{
		file_result.Seek(n_dn + num, CFile::begin);
		file_result.Read(dat, 1);
		file_tmp.Write(dat, 1);

		file_result.Seek(n_dp + num, CFile::begin);
		file_result.Read(dat, 1);
		file_tmp.Write(dat, 1);

		file_tmp.Write("\n", 1);

		num += n;
		len -= n;
	}while(len>n);

		file_result.Seek(n_dn + num, CFile::begin);
		file_result.Read(dat, 1);
		file_tmp.Write(dat, 1);

		file_result.Seek(n_dp + num, CFile::begin);
		file_result.Read(dat, 1);
		file_tmp.Write(dat, 1);

		
		//file_result.Seek(n_rxd + num, CFile::begin);
		//file_result.Read(dat, 1);
		//file_tmp.Write(dat, 1);

	file_tmp.Close();
	file_result.Close();
}

int CCrc_uiDlg::Transfer_handler(void)
{
	transfer_info.direc = m_direction.GetCurSel();
	transfer_info.mode = m_mode.GetCurSel();
	
	int len;

	len = GetDlgItem(IDC_DN)->GetWindowTextLength();
	//debug("len is",len);
	if(len)
		GetDlgItemText(IDC_DN,transfer_info.dn_str,len + 1);
//	else
//		strcpy(transfer_info.dn_str,"rxdn = ");
	if(len = GetDlgItem(IDC_DP)->GetWindowTextLength())
		GetDlgItemText(IDC_DP,transfer_info.dp_str,len + 1);
//	else
//		strcpy(transfer_info.dp_str, "rxdp = ");
	if(len = GetDlgItem(ID_RXD)->GetWindowTextLength())
		GetDlgItemText(ID_RXD,transfer_info.rxd_str,len + 1);
//	else
//		strcpy(transfer_info.rxd_str, "rxd = ");

	if(len = GetDlgItem(IDC_DELAY)->GetWindowTextLength())
		GetDlgItemText(IDC_DELAY,transfer_info.rxd_delay,len + 1);

	if(transfer_info.direc == TX)
	{	
		if(file_input_select == SELECT_ALREADY){
			if( m_pathname.IsEmpty() ){
				AfxMessageBox("Please select a file for transferation!");
				return -1;
			}
		
			transfer_info.source = m_pathname.GetBuffer(m_pathname.GetLength());
		}
		else {
			if(GetDlgItem(IDC_DATA)->GetWindowTextLength() == 0)
			{
				AfxMessageBox("Please input data for transferation!");
				return -1;
			}
			move_to_tmp_file_for_editbox();
			transfer_info.source = FILE_FOR_TMP_DATA;
		}

		if(transfer_info.mode == CRC16_FLAG)
			
			crc_feature->crc16_tx();
		else if(transfer_info.mode == CRC5_FLAG)
			crc_feature->crc5_tx();
	}
	
	else if(transfer_info.direc == RX)
	{
		if( m_file_rx.IsEmpty() ){
			AfxMessageBox("Please select a file for transferation!");
				return -1;
		}
		transfer_info.source_for_rx = m_file_rx.GetBuffer(m_file_rx.GetLength());
		transfer_info.speed = m_speed_mode.GetCurSel();
		move_to_tmp_file_for_rx();
		if(transfer_info.mode == CRC16_FLAG)
		{
			
			crc_feature->crc16_rx();
			
		}
		else if(transfer_info.mode == CRC5_FLAG)
			crc_feature->crc5_rx();
	}
	return 0;
}

void CCrc_uiDlg::OnTransfer() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	Transfer_handler();
	
}

void CCrc_uiDlg::OnFileInputSelected() 
{
	// TODO: Add your control notification handler code here
	if(file_input_select == UNSELECT_ALREADY){
		((CButton*)GetDlgItem(IDC_FILE_INPUT))->SetCheck(TRUE);
		file_input_select = SELECT_ALREADY;
		((CButton*)GetDlgItem(IDC_DLG_INPUT))->SetCheck(FALSE);
		dlg_input_select = UNSELECT_ALREADY;
	
	}
	else 
		((CButton*)GetDlgItem(IDC_FILE_INPUT))->SetCheck(TRUE);

}

void CCrc_uiDlg::OnDlgInputSelected() 
{
	// TODO: Add your control notification handler code here
		if(dlg_input_select == UNSELECT_ALREADY){
		((CButton*)GetDlgItem(IDC_DLG_INPUT))->SetCheck(TRUE);
		dlg_input_select = SELECT_ALREADY;
		((CButton*)GetDlgItem(IDC_FILE_INPUT))->SetCheck(FALSE);
		file_input_select = UNSELECT_ALREADY;
		//strcmp(transfer_info.source , "data.txt");
		}
		else
			((CButton*)GetDlgItem(IDC_DLG_INPUT))->SetCheck(TRUE);
}

void CCrc_uiDlg::OnFileForRX() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
	if(dlg.DoModal()==IDOK)
		m_file_rx = dlg.GetPathName();
	//MessageBox(m_pathname);
	SetDlgItemText(IDC_FILE_FOR_RX,m_file_rx);
}

void CCrc_uiDlg::OnCbnSelchangeCombo1()
{
	// TODO: Add your control notification handler code here
}

⌨️ 快捷键说明

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