📄 testdlg.cpp
字号:
// TestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Test.h"
#include "TestDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
bCom1Open = FALSE;
TimerID = -1;
bCom2Open = FALSE;
//{{AFX_DATA_INIT(CTestDlg)
m_CommInfo = _T("");
m_Cmd = _T("");
m_circle = 0;
m_strout3 = _T("");
m_strin3 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
memset(&CommInfo,0,sizeof(CommInfo));
}
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Control(pDX, IDC_PROMPT, m_prompt);
DDX_Control(pDX, IDC_COMBO1, m_CommCombo);
DDX_Text(pDX, IDC_EDIT1, m_CommInfo);
DDX_Text(pDX, IDC_EDIT2, m_Cmd);
DDX_Radio(pDX, IDC_RADIO1, m_circle);
DDX_Text(pDX, IDC_EDIT5, m_strout3);
DDX_Text(pDX, IDC_EDIT3, m_strin3);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Send, OnSend)
ON_BN_CLICKED(IDC_Receive, OnReceive)
ON_BN_CLICKED(IDC_Open, OnOpen)
ON_BN_CLICKED(IDC_CloseComm, OnCloseComm)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_COMMAND(ID_EXIT, OnExit)
ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
ON_BN_CLICKED(IDC_Read, OnRead)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers
BOOL CTestDlg::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
m_CommCombo.SetCurSel (0);
m_prompt.SetWindowText ("端口状态:空闲");
GetDlgItem(IDC_CloseComm)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestDlg::OnSend()
{
char Data[200];
UpdateData(TRUE);
memset(Data,0,200);
CString Cmd;
DWORD len = 0;
if ( m_circle == 0)
{
Cmd = m_Cmd;
}
if ( m_circle == 1)
{
Cmd += "A";
Cmd += m_strin3;
}
if ( m_circle == 2)
{
Cmd = m_Cmd;
Cmd += "A";
Cmd += m_strin3;
}
int size = Cmd.GetLength ();
sprintf(Data,"%s",Cmd);
AssembleData(Data,&size,m_circle);
if ( CommInfo.hComm <= 0)
return;
if ( TimerID >0)
KillTimer(TimerID);
for ( int i = 0 ; i < size; i +=1)
{
Sleep(2);
while ( WriteBlock(&CommInfo,Data + i,1) != 1);
}
TimerID = SetTimer(NULL,1000,NULL);
/*
char pData[200];
memset(pData,0,200);
while(len ==0)
{
len = Read(&CommInfo,(unsigned char*)pData);
};
if ( pData[len-1] == 'q')
{
MessageBox("错误 ");
}
m_CommInfo = pData;
UpdateData(FALSE);
//len = ReadBlock(&CommInfo,pData, 200);
*/
}
void CTestDlg:: AssembleData(char *p, int* size , int circle)
{
//circle ==0 //2磁道
//circle ==1 //3磁道
//circle ==2--其他 //2磁道同写
char temp[200];
memset(temp,0,200);
memcpy(temp,p,200);
memcpy(temp,p,*size);
memset(p,0,200);
int start = 2;
p[0] = 0x1b;
p[1] = 't';
memcpy( p + start,temp,*size);
start += *size;
p[start ++] = 0x1d;
p[start ++] = 0x1b;
p[start ++] = '\\';
p[start ++] = 0x1b;
p[start ] = 'j';
*size = start + 1;
return;
}
void CTestDlg:: AssembleCmd(char *p,int* size ,int circle)
{
//circle ==0 //2磁道
//circle ==1 //3磁道
//circle ==2--其他 //2磁道同写
memset(p,0,*size);
*size = 3;
if ( circle == 0)
{
p[0] = 0x1b;
p[1] = ']';
*size = 2;
}
if ( circle == 1)
{
p[0] = 0x1b;
p[1] = 'T';
p[2] = ']';
}
if ( circle == 2)
{
p[0] = 0x1b;
p[1] = 'B';
p[2] = ']';
}
}
void CTestDlg::OnReceive()
{
m_strout3 = "";
m_CommInfo = "";
UpdateData(FALSE);
}
void CTestDlg::OnOpen()
{
CString t , prompt = "端口状态: ";
m_CommCombo.GetWindowText (t);
BOOL ret = InitComm (&CommInfo,LPCSTR(t));
prompt += t;
if (ret)
prompt += "打开";
else
prompt += "失败";
m_prompt.SetWindowText (prompt);
if (ret)
{
if (t == "com1")
bCom1Open = TRUE;
else
bCom2Open = TRUE;
GetDlgItem(IDC_Open)->EnableWindow(FALSE);
GetDlgItem(IDC_CloseComm)->EnableWindow(TRUE);
}
}
void CTestDlg::PostNcDestroy()
{
// TODO: Add your specialized code here and/or call the base class
CDialog::PostNcDestroy();
}
BOOL CTestDlg::DestroyWindow()
{
if ( TimerID >0)
{
KillTimer(TimerID);
}
if ( bCom1Open ||bCom2Open )
DestroyComm (&CommInfo);
return CDialog::DestroyWindow();
}
void CTestDlg::OnCloseComm()
{
BOOL ret = FALSE;
if ( TimerID >0)
{
KillTimer(TimerID);
}
if ( bCom1Open ||bCom2Open )
ret = DestroyComm (&CommInfo);
if (ret)
{
CString t , prompt = "端口状态: ";
m_CommCombo.GetWindowText (t);
BOOL ret = InitComm (&CommInfo,LPCSTR(t));
prompt += t;
prompt += "空闲";
m_prompt.SetWindowText (prompt);
GetDlgItem(IDC_Open)->EnableWindow(TRUE);
GetDlgItem(IDC_CloseComm)->EnableWindow(FALSE);
if (t == "com1")
bCom1Open = FALSE;
else
bCom2Open = FALSE;
}
}
void CTestDlg::OnReset()
{
char p[4];
p[0] = 0x1b;
p[1] = '0';
WriteBlock(&CommInfo,p,2);
}
void CTestDlg::OnExit()
{
PostQuitMessage(0);
}
void CTestDlg::OnSelchangeCombo1()
{
if ( TimerID >0)
{
KillTimer(TimerID);
}
CString t;
m_CommCombo.GetWindowText (t);
if (t == "com1")
if ( bCom1Open == FALSE )
{
GetDlgItem(IDC_Open)->EnableWindow(TRUE);
GetDlgItem(IDC_CloseComm)->EnableWindow(FALSE);
}
else
{
GetDlgItem(IDC_Open)->EnableWindow(TRUE);
GetDlgItem(IDC_CloseComm)->EnableWindow(FALSE);
}
if (t == "com2")
if ( bCom2Open == FALSE )
{
GetDlgItem(IDC_Open)->EnableWindow(TRUE);
GetDlgItem(IDC_CloseComm)->EnableWindow(FALSE);
}
else
{
GetDlgItem(IDC_Open)->EnableWindow(FALSE);
GetDlgItem(IDC_CloseComm)->EnableWindow(TRUE);
}
CString prompt = "端口状态: ";
if ( (t=="com1" && bCom1Open == TRUE)
||(t=="com2" && bCom2Open == TRUE) )
prompt += "打开";
else
prompt += "空闲";
m_prompt.SetWindowText (prompt);
}
void CTestDlg::OnRead()
{
if ( bCom1Open == FALSE && bCom2Open == FALSE)
return;
UpdateData(TRUE);
int size = 200;
unsigned char Cmd[200];
memset(Cmd,0,200);
AssembleCmd((char*)Cmd, &size ,m_circle);
WriteBlock(&CommInfo,Cmd,size);
if ( TimerID == -1)
TimerID = SetTimer(NULL,1000,NULL);
}
int CTestDlg::ResolveData(char*data,char* data1,char* data2,int len,int circle)
{
// if ( data[0] != 0x1b || data[1] != 's')
// return 0;
char* dat ;
dat = data1;
int j = 0,i =0 ,start = 2;
if ( circle==0)
{
if ( len - 5 > 0 )
{
int i1,i2,i3;
for (i1=0;i1<len;++i1)
if(*(data+i1)=='s') break;
++i1;
i3=i1;
for(i2=0;i2<len-i3;++i2)
{
if(*(data+i1)=='?') break;
*(data1+i2)=*(data+i1);
++i1;
}
//memcpy(data1,data + 2 ,len -5);
}
else
memcpy(data1,data ,len);
TRACE(" the length len-5 is %d ", len -5 );
}
if (circle ==1)
{
if ( len - 6 > 0 )
{
int k1,k2,k3;
for (k1=0;k1<len;++k1)
if(*(data+k1)=='A') break;
++k1;
k3=k1;
for(k2=0;k2<len-k3;++k2)
{
if(*(data+k1)=='?') break;
*(data2+k2)=*(data+k1);
++k1;
}
//memcpy(data2,data + 3 ,len -6);
}
else
memcpy(data2,data ,len);
TRACE(" the length len-6 is %d ", len -6 );
}
if (circle ==2 )
{
if ( len < 5 )
{
memcpy(data1,data ,len);
}
else
{
for ( j = 0; j < len; j ++ )
{
if ( *( data +j ) == 'A')
{
memcpy(data1,data + 2,j-2);
i = j;
}
if (*( data +j ) == '?')
{
memcpy(data2,data + i +1,j-i-1);
break;
}
}
}
//TRACE(" the length len-5 is %d ", len -5 );
}
return (circle==2?2:1);
}
void CTestDlg::OnTimer(UINT nIDEvent)
{
CDialog::OnTimer(nIDEvent);
unsigned char Cmd[200];
memset(Cmd,0,200);
int len = Read(&CommInfo,Cmd);
if (len >= 200)
{
KillTimer(TimerID);
TimerID =-1;
}
if ( len != 0)
{
KillTimer(TimerID);
TimerID = -1;
int len1 = Read(&CommInfo,Cmd + len );
char data1[200],data2[200];
memset(data1,0,200);
memset(data2,0,200);
if ( len + len1 > 200)return;
CString string = Cmd;
if (string.Find('q') > 0)
{
MessageBox("写卡错误 ");
}
m_strout3= "";
UpdateData(FALSE);
ResolveData((char*)Cmd,data1,data2,len + len1,m_circle);
m_CommInfo = data1;
m_strout3= data2;
UpdateData(FALSE);
// PurgeComm (CommInfo.hComm, PURGE_TXCLEAR|PURGE_RXCLEAR);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -