📄 cf_test_dlg.cpp
字号:
// CF_TEST_Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "CF_TEST_.h"
#include "CF_TEST_Dlg.h"
#include "commport.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; //设备总扇区数
/* 定义一个串口通信类 */
CCommPort Comm;
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
CString strtemp;
unsigned int Sector=0;
unsigned char Count=0;
UpdateData(TRUE); //读取编辑框内容
int TYPE =m_ByteType.GetCurSel();
m_Show="";
if(GetSectorAndCount( TYPE,m_EditSector,m_EditSectorCount,&Sector)==TRUE)
{
if(Sector<TotlaSectors)
{
//=============== 发送命令 ===============
BYTE sendbuff[16] = {'Z','L','G',':',
'2','0',
0x30+(UCHAR)(Sector>>28),0x30+(UCHAR)(Sector>>24),0x30+(UCHAR)(Sector>>20),0x30+(UCHAR)(Sector>>16),0x30+(UCHAR)(Sector>>12),0x30+(UCHAR)(Sector>>8),0x30+(UCHAR)(Sector>>4),0x30+(UCHAR)Sector,
'0','1'};
int ret = Comm.SendData(sendbuff, NULL, 16, 500, 1, 0, FALSE); //发送数据
if (ret != 0)
{ MessageBox("与LPC2200 连接失败,请检查硬件!");
m_Edit_State = "与LPC2200 通信失败!";
UpdateData(FALSE);
return;
}
}
else
AfxMessageBox("需要操作的扇区号,超出寻址范围!");
}
else return;
/********************************************* 读回数据 ***********************************************/
BYTE rxdata[530] ;
int ret = Comm.ReadBlock(rxdata, 512, 500); //读出数据
if (ret != 0)
{
MessageBox("与LPC2200 连接失败,请检查硬件连接、串口号、波特率!");
m_Edit_State = "与LPC2200 通信失败!";
UpdateData(FALSE);
return;
}
ret = Comm.ReadBlock(&rxdata[512], 7, 500); //读出数据
if (ret != 0)
{
MessageBox("与LPC2200 连接失败,请检查硬件连接、串口号、波特率!");
m_Edit_State = "与LPC2200 通信失败!";
UpdateData(FALSE);
return;
}
if( (rxdata[0]=='S')&&(rxdata[1]=='T')&&
(rxdata[2]=='A')&&(rxdata[3]=='T')&&
(rxdata[4]=='E')&&(rxdata[5]==':'))
{
RXDATALEN=0;
CommandCode = 0xff;
if(rxdata[6]=='N')
m_Edit_State="读扇区指令执行失败,请检查设备是否连接好!"; //指令执行出错
else
m_Edit_State="通讯出错请重新运行开发板程序!"; //通讯出错,请重新开始
UpdateData(FALSE);
return;
}
if( (rxdata[512+0]=='S')&&(rxdata[512+1]=='T')&&
(rxdata[512+2]=='A')&&(rxdata[512+3]=='T')&&
(rxdata[512+4]=='E')&& (rxdata[512+5]==':'))
{
if(rxdata[512+6]=='Y')
{
int i = m_ComboRXData.GetCurSel();
if(i==0)// 16进制显示
{
for(int k=0;k<512;k++)
{ if((k%0x10)==0)
{
strtemp.Format("%08X:",k); //将字符送入临时变量strtemp存放
m_Show+=strtemp; //加入接收编辑框对应字符串
}
strtemp.Format("%02X ",rxdata[k]); //将字符送入临时变量strtemp存放
m_Show+=strtemp; //加入接收编辑框对应字符串
}
}
else// ASCII显示
{
for(int k=0;k<512;k++)
{
strtemp.Format("%c",rxdata[k]); //将字符送入临时变量strtemp存放
m_Show+=strtemp; //加入接收编辑框对应字符串
}
}
// IntToASCII
strtemp.Format("%c%c",13,10); //回车,换行
m_Show+=strtemp;
strtemp.Format("%c%c",13,10); //回车,换行
m_Show+=strtemp;
m_Edit_State="读扇区指令正常完成。"; //指令执行正确
}
}
UpdateData(FALSE);
}
/********************************************************************************************************/
/********************************************************************************************************/
/********************************************************************************************************/
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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -