?? cf_test_dlg.cpp
字號:
// 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 + -