📄 scomdlg.cpp
字号:
// SComDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SCom.h"
#include "SComDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CSComDlg dialog
CSComDlg::CSComDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSComDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSComDlg)
m_TxCount = _T("");
m_TxLength = _T("");
m_strSendFilePathName = _T("");
m_bDTR = FALSE;
m_bRTS = FALSE;
m_ReceiveData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSComDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSComDlg)
DDX_Control(pDX, IDC_CHECK_HEXRECEIVE, m_ctrlHexReceive);
DDX_Control(pDX, IDC_EDIT_RECEIVE, m_ctrlReceiveData);
DDX_Control(pDX, IDC_STATIC_STATUSDISP, m_strStatus);
DDX_Control(pDX, IDC_COMBO_SPEED, m_Speed);
DDX_Control(pDX, IDC_COMBO_COMPORT, m_Com);
DDX_Control(pDX, IDC_BUTTON_SENDFILE, m_ctrlSendFile);
DDX_Control(pDX, IDC_BUTTON_OPENPORT, m_ctrlOpenPort);
DDX_Text(pDX, IDC_EDIT_TXCOUNT, m_TxCount);
DDX_Text(pDX, IDC_EDIT_TXLENGTH, m_TxLength);
DDX_Text(pDX, IDC_EDIT_FILENAME, m_strSendFilePathName);
DDX_Check(pDX, IDC_CHECK_DTR, m_bDTR);
DDX_Check(pDX, IDC_CHECK_RTS, m_bRTS);
DDX_Text(pDX, IDC_EDIT_RECEIVE, m_ReceiveData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSComDlg, CDialog)
//{{AFX_MSG_MAP(CSComDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_COMM_RXCHAR,OnComm)
ON_BN_CLICKED(IDC_BUTTON_OPENFILE, OnButtonOpenfile)
ON_BN_CLICKED(IDC_BUTTON_OPENPORT, OnButtonOpenport)
ON_BN_CLICKED(IDC_BUTTON_SENDFILE, OnButtonSendfile)
ON_CBN_SELENDOK(IDC_COMBO_COMPORT, OnSelendokComboComport)
ON_WM_DESTROY()
ON_CBN_SELENDOK(IDC_COMBO_SPEED, OnSelendokComboSpeed)
ON_BN_CLICKED(IDC_BUTTON_CLEARRX, OnButtonClearrx)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSComDlg message handlers
BOOL CSComDlg::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_bFile=0;
m_gTxCount=0;
m_Com.SetCurSel(0);
m_Speed.SetCurSel(3);
m_nBaud=9600;
m_nCom=1;
m_bSendFile=FALSE;
CString strStatus;
if(m_Port.InitPort(this,m_nCom,m_nBaud,'N',8,1,EV_RXFLAG | EV_RXCHAR,512)){
m_Port.StartMonitoring();
m_bOpenPort=TRUE;
strStatus.Format("COM%d OPENED,%d,'N',8,1",m_nCom,m_nBaud);
}else{
AfxMessageBox("没有发现串口!");
m_bOpenPort=FALSE;
m_strStatus.SetWindowText("COM Closed!");
}
m_strStatus.SetWindowText(strStatus);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSComDlg::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 CSComDlg::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 CSComDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LONG CSComDlg::OnComm(WPARAM ch,LPARAM Port)
{
unsigned char uc0;
uc0 =ch;
if(m_ctrlReceiveData.GetLineCount()>400)
{
m_ReceiveData.Empty();
m_ReceiveData="***The Length of the Text is too long, Emptied Automaticly!!!***\r\n";
UpdateData(FALSE);
}
//如果选择了"十六进制显示",则显示十六进制值
CString str;
if(m_ctrlHexReceive.GetCheck())
str.Format("%02X ",ch);
else
str.Format("%c",ch);
//以下是将接收的字符加在字符串的最后,这里费时很多
//但考虑到数据需要保存成文件,所以没有用List Control
int nLen=m_ctrlReceiveData.GetWindowTextLength();
m_ctrlReceiveData.SetSel(nLen, nLen);
m_ctrlReceiveData.ReplaceSel(str);
nLen+=str.GetLength();
m_ReceiveData+=str;
//以下是发射部分.
if(m_bFile&&m_bSendFile){
if(m_gTxCount<m_gTxLength){
fp.Read(&uc0,1);
m_gTxCount++;
m_TxCount.Format("%ld",m_gTxCount);
m_Port.WriteToPort((LPCTSTR)&uc0,1);
}else{
fp.SeekToBegin();
m_bSendFile=FALSE;
m_ctrlSendFile.SetWindowText("开始发送");
m_gTxCount=0;
}
}
UpdateData(FALSE);
return 0;
}
void CSComDlg::OnButtonOpenfile()
{
// TODO: Add your control notification handler code here
LPCSTR lpszPath="c:\\";
SetCurrentDirectory(lpszPath);
static char BASED_CODE szFiler[]="文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||";
CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFiler);
FileDlg.m_ofn.lpstrInitialDir=lpszPath;
if(FileDlg.DoModal()==IDOK){
CString strFileName=FileDlg.GetFileName();
CString strFileExt=FileDlg.GetFileExt();
CString lpstrName=FileDlg.GetPathName();
m_strSendFilePathName=lpstrName;
if(!(fp.Open((LPCSTR)m_strSendFilePathName,CFile::modeRead))){
AfxMessageBox("Open File Failed!");
return;
}
m_bFile=1; //已经打开文件.
m_gTxLength=fp.GetLength();
m_TxLength.Format("%d",m_gTxLength);
fp.SeekToBegin();
}
UpdateData(FALSE);
}
void CSComDlg::OnButtonOpenport()
{
// TODO: Add your control notification handler code here
CString strStatus;
m_bOpenPort =!m_bOpenPort;
if(!m_bOpenPort){
m_ctrlOpenPort.SetWindowText("打开串口");
m_Port.ClosePort();
strStatus.Format("COM%d is Closed!",m_nCom);
}else{
m_ctrlOpenPort.SetWindowText("关闭串口");
if(m_Port.InitPort(this,m_nCom,m_nBaud,'N',8,1,EV_RXFLAG | EV_RXCHAR,512)){
m_Port.StartMonitoring();
strStatus.Format("COM%d OPENED,%d,'N',8,1",m_nCom,m_nBaud);
}else{
AfxMessageBox("没有发现串口!");
}
}
m_strStatus.SetWindowText(strStatus);
UpdateData(FALSE);
}
void CSComDlg::OnButtonSendfile()
{
// TODO: Add your control notification handler code here
if(!m_bFile){
AfxMessageBox("没有打开文件!");
return;
}
m_bSendFile =!m_bSendFile;
if(m_bSendFile){
m_ctrlSendFile.SetWindowText("停止发送");
if(m_bFile){
if(m_bOpenPort){
unsigned char ch;
fp.Read(&ch,1);
m_Port.WriteToPort((LPCTSTR)&ch,1);
m_gTxCount++;
}else{
AfxMessageBox("没有打开串口!");
}
}else{
AfxMessageBox("没有打开文件!");
}
}else{
m_ctrlSendFile.SetWindowText("开始发送");
}
}
void CSComDlg::OnSelendokComboComport()
{
// 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, m_nBaud,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
{
m_Port.StartMonitoring();
m_bOpenPort=TRUE;
}
else
{
AfxMessageBox("没有发现此串口或被占用");
}
}
void CSComDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
m_Port.ClosePort();
if(m_bFile){
fp.Close();
}
}
void CSComDlg::OnSelendokComboSpeed()
{
// TODO: Add your control notification handler code here
const int Baud[]={1200,2400,4800,9600,19200,38400,57600,115200};
CString temp;
int i=m_Speed.GetCurSel();
m_nBaud=Baud[i];
if (m_Port.InitPort(this, m_nCom, m_nBaud,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
{
m_Port.StartMonitoring();
}else{
AfxMessageBox("没有成功,请重试");
}
}
void CSComDlg::OnButtonClearrx()
{
// TODO: Add your control notification handler code here
m_ReceiveData.Empty();
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -