📄 chuankoutestdlg.cpp
字号:
// ChuankouTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ChuankouTest.h"
#include "ChuankouTestDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CChuankouTestDlg dialog
CChuankouTestDlg::CChuankouTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChuankouTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChuankouTestDlg)
m_strFileSavePath = _T("");
m_strReceiveMsg = _T("");
m_strSendFilePath = _T("");
m_strSendMsg = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bSendFile = FALSE;
m_bOpenPort = FALSE;
m_strFileSavePath = "C:\\COMDATA";
m_nFileLength = 0;
}
void CChuankouTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChuankouTestDlg)
DDX_Control(pDX, IDC_STATIC_TXCOUNT, m_ctrlTxcount);
DDX_Control(pDX, IDC_EDIT_SENDMSG, m_ctrlEditSendMsg);
DDX_Control(pDX, IDC_COMBO_PORTSELECT, m_Com);
DDX_Control(pDX, IDC_BUTTON_CLOSE, m_ctrlClose);
DDX_Control(pDX, IDC_BUTTON_OPENPORT, m_ctrlOpenPort);
DDX_Control(pDX, IDC_BUTTON_SEND, m_ctrlManualSend);
DDX_Control(pDX, IDC_BUTTON_SENDFILE, m_ctrlSendFile);
DDX_Control(pDX, IDC_EDIT_SENDFILEBROWER, m_ctrlEditSendFilePath);
DDX_Control(pDX, IDC_EDIT_RECEIVEMSG, m_ctrlEditReceiveMsg);
DDX_Control(pDX, IDC_EDIT_FILESAVEPATH, m_ctrlEditFileSavePath);
DDX_Control(pDX, IDC_STATIC_RXCOUT, m_ctrlRxcount);
DDX_Control(pDX, IDC_STATIC_STATUS, m_ctrlPortStatus);
DDX_Text(pDX, IDC_EDIT_FILESAVEPATH, m_strFileSavePath);
DDX_Text(pDX, IDC_EDIT_RECEIVEMSG, m_strReceiveMsg);
DDX_Text(pDX, IDC_EDIT_SENDFILEBROWER, m_strSendFilePath);
DDX_Text(pDX, IDC_EDIT_SENDMSG, m_strSendMsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChuankouTestDlg, CDialog)
//{{AFX_MSG_MAP(CChuankouTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_MESSAGE(WM_COMM_RXCHAR,OnCommunication)
ON_CBN_EDITCHANGE(IDC_COMBO_PORTSELECT, OnEditchangeComboPortselect)
ON_MESSAGE(WM_COMM_TXEMPTY_DETECTED,OnFileSendingEnded)
ON_BN_CLICKED(IDC_BUTTON_OPENPORT, OnButtonOpenport)
ON_BN_CLICKED(IDC_BUTTON_CLOSEPORT, OnButtonCloseport)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_BUTTON_FILESELECT, OnButtonFileselect)
ON_BN_CLICKED(IDC_BUTTON_SENDFILE, OnButtonSendfile)
ON_BN_CLICKED(IDC_BUTTON_DIRFILEBROWER, OnButtonDirfilebrower)
ON_BN_CLICKED(IDC_BUTTON_FILESAVE, OnButtonFilesave)
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChuankouTestDlg message handlers
BOOL CChuankouTestDlg::OnInitDialog()
{
BOOL b = 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_Com.SetCurSel(0);
m_nCom = 1;
if (m_Port.InitPort(this,m_nCom, 9600,'N',8,1, EV_RXFLAG | EV_RXCHAR,512))
{
m_Port.StartMonitoring();
}
else
{
AfxMessageBox("没有发现该串口");
}
UpdateData(FALSE);
return b; // return TRUE unless you set the focus to a control
}
void CChuankouTestDlg::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 CChuankouTestDlg::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 CChuankouTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LONG CChuankouTestDlg::OnCommunication(WPARAM ch,LPARAM port)
{
long rxdatacount=0;
if (port <= 0 || port > 4)
return -1;
rxdatacount++; //接收的字节计数
CString strTemp;
strTemp.Format("%ld",rxdatacount);
strTemp="RX:"+strTemp;
m_ctrlRxcount.SetWindowText(strTemp);
return 0;
}
void CChuankouTestDlg::OnEditchangeComboPortselect()
{
// TODO: Add your control notification handler code here
if(m_Port.m_hComm!=NULL)
{
m_Port.ClosePort();
}
m_nCom=m_Com.GetCurSel()+1;
if (m_Port.InitPort(this,m_nCom, 9600,'N',8,1, EV_RXFLAG | EV_RXCHAR,512))
{
m_Port.StartMonitoring();
if(!m_bOpenPort)
{
m_Port.StartMonitoring();
}
}
else
{
AfxMessageBox("没有发现此串口或被占用");
}
}
void CChuankouTestDlg::OnButtonOpenport()
{
// TODO: Add your control notification handler code here
m_nCom = m_Com.GetCurSel() + 1;
if(m_Port.InitPort(this,m_nCom, 9600,'N',8,1, EV_RXFLAG | EV_RXCHAR,512))
{
m_Port.StartMonitoring();
m_bOpenPort = TRUE;
}
else
{
AfxMessageBox("没有发现此串口或被占用");
m_bOpenPort = FALSE;
}
GetDlgItem(IDC_BUTTON_OPENPORT)->EnableWindow(!m_bOpenPort);
GetDlgItem(IDC_BUTTON_CLOSEPORT)->EnableWindow(m_bOpenPort);
}
void CChuankouTestDlg::OnButtonCloseport()
{
// TODO: Add your control notification handler code here
m_Port.ClosePort();
m_bOpenPort = FALSE;
GetDlgItem(IDC_BUTTON_OPENPORT)->EnableWindow(!m_bOpenPort);
GetDlgItem(IDC_BUTTON_CLOSEPORT)->EnableWindow(m_bOpenPort);
}
void CChuankouTestDlg::OnButtonClear()
{
// TODO: Add your control notification handler code here
m_strReceiveMsg.Empty();
UpdateData(FALSE);
}
void CChuankouTestDlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
if(m_Port.m_hComm==NULL)
{
AfxMessageBox("串口没有打开,请打开串口");
return;
}
else
{
UpdateData(TRUE);
m_Port.WriteToPort((LPCSTR)m_strSendMsg);
}
}
void CChuankouTestDlg::OnButtonFileselect()
{
// TODO: Add your control notification handler code here
LPCSTR lpszPath = "c:\\comdata";
SetCurrentDirectory(lpszPath);
static char BASED_CODE szFilter[] = "文本文件(*.txt) | *.txt | 所有文件(*.*) | *.*||";
CFileDialog FileDlg (TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
FileDlg.m_ofn.lpstrInitialDir = lpszPath;
if(FileDlg.DoModal() == IDOK)
{
CString strFileName = FileDlg.GetFileName();
CString strFileExt = FileDlg.GetFileExt();
CString lpstrName = FileDlg.GetPathName();
m_strSendFilePath = lpstrName;
UpdateData(FALSE);
}
}
void CChuankouTestDlg::OnButtonSendfile()
{
// TODO: Add your control notification handler code here
CFile fp;
if(fp.Open((LPCSTR)m_strSendFilePath,CFile::modeRead) == NULL)
{
AfxMessageBox("Open file failes!");
return;
}
fp.SeekToEnd();
unsigned long fplength = fp.GetLength();
m_nFileLength = fplength;
char *fpBuff;
fpBuff = new char[fplength];
fp.SeekToBegin();
if(fp.Read(fpBuff,fplength)<1)
{
fp.Close();
return;
}
fp.Close();
m_nCom=m_Com.GetCurSel()+1;
if (m_Port.InitPort(this,m_nCom, 9600,'N',8,1, EV_RXFLAG | EV_RXCHAR,512))
{
m_Port.StartMonitoring();
if(!m_bOpenPort)
{
m_Port.StartMonitoring();
m_bOpenPort = TRUE;
m_Port.WriteToPort((LPCSTR)fpBuff,fplength);
}
}
else
{
AfxMessageBox("没有发现此串口或被占用");
}
delete fpBuff;
}
void CChuankouTestDlg::OnButtonDirfilebrower()
{
// TODO: Add your control notification handler code here
static char displayname[MAX_PATH];
static char path[MAX_PATH];
LPITEMIDLIST pidlBrowse; // PIDL selected by user
BROWSEINFO bi;
bi.hwndOwner = this->m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = displayname;
bi.lpszTitle = "请选择要保存接收数据的文件夹";
bi.ulFlags = BIF_EDITBOX ;
bi.lpfn = NULL;
pidlBrowse=SHBrowseForFolder( &bi);
if(pidlBrowse!=NULL)
{
SHGetPathFromIDList(pidlBrowse,path);
}
CString str=path; //得到路径
if(str.IsEmpty()) return; //如果没有选择,就返回
m_strFileSavePath=str; //接收路径编辑框对应变量
UpdateData(FALSE);
}
void CChuankouTestDlg::OnButtonFilesave()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
int nLength;
nLength = m_strFileSavePath.GetLength();
for(int nCount = 0;nCount<nLength;nCount++)
{
if(m_strFileSavePath.GetAt(nCount) == '\\')
CreateDirectory(m_strFileSavePath.Left(nCount+1),NULL);
}
CreateDirectory(m_strFileSavePath,NULL);
CFile m_rFile;
LPCSTR lpszPath = m_strFileSavePath;
SetCurrentDirectory(lpszPath);
CString filename;
filename = "rec.txt";
if(m_rFile.Open(filename,CFile::modeCreate | CFile::modeWrite ) == NULL)
{
AfxMessageBox("create file failed!");
}
m_rFile.Write((LPCSTR)m_strReceiveMsg,m_strReceiveMsg.GetLength());
m_rFile.Flush();
m_rFile.Close();
CString str;
str = "OK,";
str += filename;
str += " saved";
m_ctrlEditFileSavePath.SetWindowText(str);
SetTimer(1,5000,NULL);
}
void CChuankouTestDlg::OnButtonClose()
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
LONG CChuankouTestDlg::OnFileSendingEnded(WPARAM wPARAM,LPARAM port)
{
if(m_bSendFile)
{
m_ctrlEditSendFilePath.SetWindowText("发送完毕");
long TX_count=0;
TX_count+=m_nFileLength;
SetTimer(2,3000,NULL);
CString strTemp;
strTemp.Format("TX:%d",TX_count);
m_ctrlTxcount.SetWindowText(strTemp);
m_bSendFile=FALSE;
}
return 0;
}
void CChuankouTestDlg::OnTimer(UINT nIDEVENT)
{
switch(nIDEVENT)
{
case 1:
m_ctrlEditFileSavePath.SetWindowText(m_strFileSavePath);
KillTimer(1);
break;
case 2:
m_ctrlSendFile.EnableWindow(TRUE);
//m_strSendFilePath = m_strTempSendFilePath;
m_ctrlEditSendFilePath.SetWindowText(m_strSendFilePath);
KillTimer(2);
break;
default:
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -