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

📄 cf_test_dlg.cpp

📁 arm lpc2000 应用历程 arm lpc2000 应用历程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// CF_TEST_Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "CF_TEST_.h"
#include "CF_TEST_Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CCF_TEST_Dlg dialog

CCF_TEST_Dlg::CCF_TEST_Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCF_TEST_Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCF_TEST_Dlg)
	m_Show = _T("");
	m_Send = _T("");
	m_type = _T("");
	m_Model_Number = _T("");
	m_Serial_Number = _T("");
	m_PIO_Modle = _T("");
	m_TotalSector = _T("");
	m_EditSectorCount = _T("");
	m_EditSector = _T("");
	m_Edit_State = _T("");
	m_Model_Number2 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCF_TEST_Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCF_TEST_Dlg)
	DDX_Control(pDX, IDC_COMBO_RXDATA, m_ComboRXData);
	DDX_Control(pDX, IDC_BYTE_TYPE, m_ByteType);
	DDX_Control(pDX, IDC_COMPORT, m_COMPort);
	DDX_Control(pDX, IDC_COMM, m_COMM);
	DDX_Text(pDX, IDC_Show, m_Show);
	DDX_Text(pDX, IDC_Send, m_Send);
	DDX_Text(pDX, IDC_MODEL_NUMBER, m_Model_Number);
	DDV_MaxChars(pDX, m_Model_Number, 40);
	DDX_Text(pDX, IDC_SERIAL_NUMBER, m_Serial_Number);
	DDV_MaxChars(pDX, m_Serial_Number, 20);
	DDX_Text(pDX, IDC_TOTAL_SECTOR, m_TotalSector);
	DDX_Text(pDX, IDC_ReadSec, m_EditSector);
	DDX_Text(pDX, IDC_EDIT_STATE, m_Edit_State);
	DDX_Text(pDX, IDC_MODEL_NUMBER2, m_Model_Number2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCF_TEST_Dlg, CDialog)
	//{{AFX_MSG_MAP(CCF_TEST_Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnReadIn)
	ON_BN_CLICKED(IDC_BUTTON2, OnWrite)
	ON_BN_CLICKED(IDC_IDESTOP, OnIdestop)
	ON_BN_CLICKED(IDC_IDESTRAT, OnIDEstrat)
	ON_CBN_SELCHANGE(IDC_COMPORT, OnSelchangeComport)
	ON_CBN_SELCHANGE(IDC_BYTE_TYPE, OnSelchangeByteType)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCF_TEST_Dlg message handlers
UCHAR rxdata[2048];							//设置BYTE数组 An 8-bit integerthat is not signed.
UINT	RXDATALEN=0;						//接收数据长度
UCHAR	CommandCode=0xff;					//执行指令代码
USHORT	SectorCount=0;						//扇区数
UINT	TotlaSectors=0;						//设备总扇区数
BOOL CCF_TEST_Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

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

	if(m_COMM.GetPortOpen())
		m_COMM.SetPortOpen(FALSE);

	m_COMM.SetCommPort(1);				//选择com1
	if( !m_COMM.GetPortOpen())
		m_COMM.SetPortOpen(TRUE);		//打开串口
	else
		AfxMessageBox("cannot open serial port");

	m_COMM.SetSettings("19200,m,8,1");	//波特率115200,标记 (Mark),8个数据位,1个停止位 

	m_COMM.SetInputMode(1);				//1:表示以二进制方式检取数据;为什么不是ASCII码?
	m_COMM.SetRThreshold(1); 
	//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
	m_COMM.SetInputLen(0);				//设置当前接收区数据长度为0
	m_COMM.GetInput();					//先预读缓冲区以清除残留数据

	//获取设备信息
	CString COMMAND;
	CommandCode = 0xEC;
	SectorCount = 1;
	COMMAND.Format("ZLG:%02X%08X%02X",CommandCode,NULL,SectorCount);//将字符送入临时变量strtemp存放
	m_COMM.SetOutput(COleVariant(COMMAND));			//发送数据
	m_Model_Number="";
	m_Model_Number2="";
	m_Serial_Number="";
	// 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
	m_COMPort.AddString("COM1");
	m_COMPort.AddString("COM2");
	m_COMPort.SetCurSel(0);

	m_ByteType.AddString("HEX");
	m_ByteType.AddString("ASCII");
	m_ByteType.SetCurSel(0);
	
	m_ComboRXData.AddString("HEX");
	m_ComboRXData.AddString("ASCII");
	m_ComboRXData.SetCurSel(0);

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

//这是一个将字符转换为相应的十六进制值的函数
//好多C语言书上都可以找到
//功能:若是在0-F之间的字符,则转换为相应的十六进制字符,否则返回-1
char ConvertHexChar(char ch) 
{
	if((ch>='0')&&(ch<='9'))
		return ch-0x30;
	else if((ch>='A')&&(ch<='F'))
		return ch-'A'+10;
	else if((ch>='a')&&(ch<='f'))
		return ch-'a'+10;
	else return (-1);
}
//由于这个转换函数的格式限制,在发送框中的十六制字符应该每两个字符之间插入一个空隔
//如:A1 23 45 0B 00 29
//CByteArray是一个动态字节数组,可参看MSDN帮助
int 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,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]=(char)hexdata;
		hexdatalen++;
	}
	senddata.SetSize(hexdatalen);
	return hexdatalen;
}

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

// 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 CCF_TEST_Dlg::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 CCF_TEST_Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//读写扇区操作参数转换
char GetSectorAndCount(	int i ,					// 0为16进制,1为10进制
						CString  SectorNumber,	// 起出扇区号
						CString  SectorCount,	// 扇区数
						unsigned int * Sector		// 输出HEX扇区数
					    )	
{	
	char char_temp;
	int temp_len;
	char RetData=FALSE;
	int	BeiShu[10]={1,
					10,
					100,
					1000,
					10000,
					100000,
					1000000,
					10000000,
					100000000,
					1000000000,
					};
	* Sector=0;
	if(SectorNumber.IsEmpty())
	{
		AfxMessageBox("操作扇区号不能为空!");
		return RetData;
	}
	if(i==0)//16进制数
	{
		int  SectorLen= SectorNumber.GetLength();
		if(SectorLen>8)
		{
			AfxMessageBox("扇区号,格式无效!");

		}
		else
		{		
			RetData = TRUE;
			for(temp_len=0;temp_len<SectorLen;temp_len++)
			{
				char_temp = SectorNumber[SectorLen-temp_len-1];
				char_temp = ConvertHexChar(char_temp);
				if(char_temp!=-1)
					*Sector+= char_temp<<(temp_len*4);
				else
				{	
					AfxMessageBox("扇区号,格式无效!");
					RetData = FALSE;
					break;
				}
			}
				
		}
	
	}
	else	//10进制数
	{
		int  SectorLen= SectorNumber.GetLength();
		if(SectorLen>10)
		{
			AfxMessageBox("扇区号,格式无效!");
			return RetData;
		}
		else
		{
			RetData = TRUE;
			for(temp_len=0;temp_len<SectorLen;temp_len++)
			{
				char_temp = SectorNumber[SectorLen-temp_len-1];
				char_temp = ConvertHexChar(char_temp);
				if((char_temp!=-1)&&(char_temp<=9))
					*Sector+= char_temp*BeiShu[temp_len];
				else
				{	
					AfxMessageBox("扇区号,格式无效!");
					RetData = FALSE;
					break;
				}
			}
		}
	}

	return RetData;
}

void CCF_TEST_Dlg::OnReadIn() //读扇区操作
{
	// TODO: Add your control notification handler code here
	unsigned int Sector=0;
	unsigned char Count=0;
	if(CommandCode!=0xff)
	{
		AfxMessageBox("系统正忙!");
		return;
	}
	UpdateData(TRUE);			//读取编辑框内容
	int TYPE =m_ByteType.GetCurSel();
	m_Show="";
	if(GetSectorAndCount( TYPE,
						  m_EditSector,
						  m_EditSectorCount,
						  &Sector
						  )==TRUE
						  )
	{
		if(Sector<TotlaSectors)
		{
			CString COMMAND;
			CommandCode = 0x20;
			SectorCount = 1;
			COMMAND.Format("ZLG:%02X%08X%02X",CommandCode,Sector,SectorCount);//将字符送入临时变量strtemp存放
			m_COMM.SetOutput(COleVariant(COMMAND));			//发送数据
		}
		else
			AfxMessageBox("需要操作的扇区号,超出寻址范围!");
	}

	
}

void CCF_TEST_Dlg::OnWrite() //写扇区操作
{
	unsigned int Sector=0;
	unsigned char Count=0;
	if(CommandCode!=0xff)
	{
		AfxMessageBox("系统正忙!");
		return;
	}
	UpdateData(TRUE);			//读取编辑框内容
	int TYPE =m_ByteType.GetCurSel();
	if(GetSectorAndCount( TYPE,
						  m_EditSector,
						  m_EditSectorCount,
						  &Sector)==TRUE
						  )
	{
		//if(Sector<(TotlaSectors=10))
		if(Sector<TotlaSectors)
		{
			UpdateData(TRUE);
			CString COMMAND,strtemp;
			CommandCode = 0x30;
			SectorCount = 1;
			COMMAND.Format("ZLG:%02X%08X%02X",CommandCode,Sector,SectorCount);//将字符送入临时变量strtemp存放
			m_COMM.SetOutput(COleVariant(COMMAND));			//发送数据
			//	m_Send.
			int len=m_Send.GetLength();
			int i;

			COMMAND="";
			if(len<512)
			{
				len = 512-len;
				COMMAND=m_Send;
				for(i=0;len>i;i++)
				{	
					strtemp.Format("%C",0x20);//填充数0x00
					COMMAND += strtemp;
					//m_COMM.SetOutput(COleVariant(strtemp));
				}
			}

						for(i=0;32>i;i++)
						{	UINT k;
							strtemp=COMMAND.Mid(0x10*i,0x10);
							for(k=0;k<0x1fffff;k++);
							m_COMM.SetOutput(COleVariant(strtemp));			//发送数据
							
						}
			
//test
//			CommandCode=0x20;

//			m_COMM.SetOutput(COleVariant("STATE:Y"));

		}
		else
			AfxMessageBox("需要操作的扇区号,超出寻址范围!");
	}
}


void CCF_TEST_Dlg::OnIDEstrat() //硬盘开始转动
{
	if(CommandCode!=0xff)
	{
		AfxMessageBox("系统正忙!");
		return;
	}
		CString COMMAND;
		CommandCode = 0xe1;
		SectorCount = 0;
		COMMAND.Format("ZLG:%02X%08X%02X",CommandCode,NULL,SectorCount);//将字符送入临时变量strtemp存放
		m_COMM.SetOutput(COleVariant(COMMAND));			//发送数据
}


void CCF_TEST_Dlg::OnIdestop() //硬盘停止转动
{
	if(CommandCode!=0xff)
	{
		AfxMessageBox("系统正忙!");
		return;
	}
	CString COMMAND;
	CommandCode = 0xe0;
	SectorCount = 0;

⌨️ 快捷键说明

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