📄 commtestdlg.cpp
字号:
// commtestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "commtest.h"
#include "commtestDlg.h"
#include "direct.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MAXBLOCK 4096
#define XON 0x11
#define XOFF 0x13
long baudrate[]={300,600,1200,2400,4800,9600,19200,38400,43000,56000,57600,115200};
int databit[]={8,7,6};
UINT ComProce(LPVOID pParam);
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CCommtestDlg dialog
CCommtestDlg::CCommtestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCommtestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCommtestDlg)
m_SeriouStr = _T("");
m_BaudStr = _T("");
m_StopStr = _T("");
m_savefilename = _T("");
m_strTXData = _T("");
m_filepath = _T("");
m_strRXData = _T("");
m_JiaoyanStr = _T("");
m_DataStr = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_txlen=0;
m_rxlen=0;
m_bConnected=1;
m_bAutoSend=FALSE;
m_portstatu_but=TRUE;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCommtestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCommtestDlg)
DDX_Control(pDX, IDC_BUTTON_EMPTYRX, m_emptyrx);
DDX_Control(pDX, IDC_STATIC_FILENAME, m_filename);
DDX_Control(pDX, IDC_CHECK_HEXSEND, m_cHexSend);
DDX_Control(pDX, IDC_BUTTON_EMPTYTEXT, m_emptytx);
DDX_Control(pDX, IDC_BUTTON_TRANSFILE, m_transfile);
DDX_Control(pDX, IDC_BUTTEN_MENUALSEND, m_sendbutten);
DDX_Control(pDX, IDC_EDIT_TXDATA, m_edittxdata);
DDX_Control(pDX, IDC_STATIC_FILEPAHT, m_sFilePath);
DDX_Control(pDX, IDC_EDIT_RXDATA, m_EditRXData);
DDX_Control(pDX, IDC_CHECK_HEXDISPLAY, m_ctrlHexDisplay);
DDX_Control(pDX, IDC_CHECK_WRITETOFILE, m_judgesend);
DDX_Control(pDX, IDC_COMBO_STOP, m_ComboStop);
DDX_Control(pDX, IDC_COMBO_JIAOYAN, m_ComboJiaoyan);
DDX_Control(pDX, IDC_COMBO_DATA, m_ComboData);
DDX_Control(pDX, IDC_COMBO_BAUD, m_ComboBaud);
DDX_Control(pDX, IDC_COMBO_SERIOU, m_ComboSeriou);
DDX_CBString(pDX, IDC_COMBO_SERIOU, m_SeriouStr);
DDX_CBString(pDX, IDC_COMBO_BAUD, m_BaudStr);
DDX_CBString(pDX, IDC_COMBO_STOP, m_StopStr);
DDX_Text(pDX, IDC_STATIC_FILENAME, m_savefilename);
DDX_Text(pDX, IDC_EDIT_TXDATA, m_strTXData);
DDX_Text(pDX, IDC_STATIC_FILEPAHT, m_filepath);
DDX_Text(pDX, IDC_EDIT_RXDATA, m_strRXData);
DDX_CBString(pDX, IDC_COMBO_JIAOYAN, m_JiaoyanStr);
DDX_CBString(pDX, IDC_COMBO_DATA, m_DataStr);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCommtestDlg, CDialog)
//{{AFX_MSG_MAP(CCommtestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_COMBO_SERIOU, OnSelchangeComboSeriou)
ON_BN_CLICKED(IDC_BUTTEN_MENUALSEND, OnButtenMenualsend)
ON_BN_CLICKED(IDC_BUTTON_TRANSFILE, OnButtonTransfile)
ON_BN_CLICKED(IDC_CHECK_AUTOSEND, OnCheckAutosend)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_EMPTYTEXT, OnButtonEmptytext)
ON_BN_CLICKED(IDC_CHECK_WRITETOFILE, OnCheckWritetofile)
ON_BN_CLICKED(IDC_BUTTON_FILENAME, OnButtonFilename)
ON_BN_CLICKED(IDC_BUTTON_SAVEEDIT, OnButtonSaveedit)
ON_BN_CLICKED(IDC_BUTTON_EMPTYRX, OnButtonEmptyrx)
ON_BN_CLICKED(IDC_BUTTON_PORTSTATU, OnButtonPortstatu)
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
ON_CBN_SELCHANGE(IDC_COMBO_BAUD, OnSelchangeComboBaud)
ON_CBN_SELCHANGE(IDC_COMBO_DATA, OnSelchangeComboData)
ON_CBN_SELCHANGE(IDC_COMBO_JIAOYAN, OnSelchangeComboJiaoyan)
ON_CBN_SELCHANGE(IDC_COMBO_STOP, OnSelchangeComboStop)
ON_WM_SIZE()
ON_BN_CLICKED(IDC_BUTTON_RESET, OnButtonReset)
//}}AFX_MSG_MAP
// ON_MESSAGE(WM_COMMNOTIFY,ProcessCOMMNotification)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCommtestDlg message handlers
BOOL CCommtestDlg::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
addall();
// OnSelchangeComboSeriou();
OpenComm(0);
//初始化重叠读事件和重叠写事件
/* m_hPostMsgEvent=CreateEvent(NULL,TRUE,TRUE,NULL);
if(m_hPostMsgEvent==NULL)
{
AfxMessageBox("wrong1");
return FALSE;
}*/
memset(&m_osRead,0,sizeof(OVERLAPPED));
memset(&m_osWrite,0,sizeof(OVERLAPPED));
m_osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if(m_osRead.hEvent==NULL)
{
AfxMessageBox("wrong2");
return FALSE;
}
m_osWrite.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if(m_osWrite.hEvent==NULL)
{
AfxMessageBox("wrong3");
return FALSE;
}
DispStatus();
return TRUE; // return TRUE unless you set the focus to a control
}
void CCommtestDlg::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 CCommtestDlg::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 CCommtestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCommtestDlg::addall()
{
//设波特率组合列表框
TCHAR baudbuffer[][7]={"300","600","1200","2400","4800","9600","19200","38400","43000","56000","57600","115200"};
for(int i=0;i<12;i++)
{
int judge_tf=m_ComboBaud.AddString(baudbuffer[i]);
if((judge_tf==CB_ERR)||(judge_tf==CB_ERRSPACE))
MessageBox("build baud error!");
}
m_ComboBaud.SetCurSel(5);
//设串口组合列表框
TCHAR seriou[][5]={"COM1","COM2","COM3","COM4"};
for(i=0;i<4;i++)
m_ComboSeriou.AddString(seriou[i]);
m_ComboSeriou.SetCurSel(0);
//设校验位组合列表框
TCHAR jiaoyan[][7]={"N","O","E"};
for(i=0;i<3;i++)
m_ComboJiaoyan.AddString(jiaoyan[i]);
m_ComboJiaoyan.SetCurSel(0);
//设数据位组合列表框
TCHAR data[][2]={"8","7","6"};
for(i=0;i<3;i++)
m_ComboData.AddString(data[i]);
m_ComboData.SetCurSel(0);
//设停止位组合列表框
TCHAR stop[][2]={"1","2"};
for(i=0;i<2;i++)
m_ComboStop.AddString(stop[i]);
m_ComboStop.SetCurSel(0);
}
void CCommtestDlg::OnSelchangeComboSeriou()
{
// TODO: Add your control notification handler code here
if (m_bConnected)
CloseConnection();
if(m_portstatu_but)
{
int i=m_ComboSeriou.GetCurSel();
OpenComm(i);
}
DispStatus();
}
BOOL CCommtestDlg::SetCommParameter()
{
DCB dcb;
if(!GetCommState(m_hCom,&dcb))
return FALSE;
//设置基本参数
int baudindex=m_ComboBaud.GetCurSel();
m_ComboBaud.GetLBText(baudindex,m_BaudStr);
dcb.BaudRate=baudrate[baudindex];
// CString test1;
// test1.Format("%ld",dcb.BaudRate);
// AfxMessageBox(test1);
int dataindex=m_ComboData.GetCurSel();
m_ComboData.GetLBText(dataindex,m_DataStr);
dcb.ByteSize=databit[dataindex];
// CString test2;
// test2.Format("%ld",dcb.ByteSize);
// AfxMessageBox(test2);
int jiaoyanindex=m_ComboJiaoyan.GetCurSel();
m_ComboJiaoyan.GetLBText(jiaoyanindex,m_JiaoyanStr);
switch(jiaoyanindex)
{
case 0:
dcb.Parity=NOPARITY;
break;
case 1:
dcb.Parity=ODDPARITY;
break;
case 2:
dcb.Parity=EVENPARITY;
break;
default:;
}
// CString test3;
// test3.Format("%ld",dcb.Parity);
// AfxMessageBox(test3);
int stopindex=m_ComboStop.GetCurSel();
m_ComboStop.GetLBText(stopindex,m_StopStr);
switch(stopindex)
{
case 0:
dcb.StopBits=ONESTOPBIT;
break;
case 1:
dcb.StopBits=TWOSTOPBITS;
break;
default:;
}
// CString test4;
// test4.Format("%d",dcb.StopBits);
// AfxMessageBox(test4);
//流控制
dcb.fInX=TRUE;
dcb.fOutX=TRUE;
dcb.XonChar=XON;
dcb.XoffChar=XOFF;
dcb.XonLim=50;
dcb.XoffLim=50;
dcb.fNull=TRUE;
return(SetCommState(m_hCom,&dcb));
}
BOOL CCommtestDlg::OpenComm(int Num)
{
COMMTIMEOUTS TimeOuts;
// int i;
// i=m_ComboSeriou.GetCurSel();
// if(i==CB_ERR)
// return FALSE;
m_ComboSeriou.GetLBText(Num,m_SeriouStr);
m_hCom=CreateFile(m_SeriouStr,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
if(m_hCom==INVALID_HANDLE_VALUE)
{
AfxMessageBox("打开串口失败!");
m_bConnected=0;
return FALSE;
}
// if(!m_bConnected)
// return FALSE;
//设置工作者线程响应的事件
SetCommMask(m_hCom,EV_RXCHAR);
//设置读写缓冲区
SetupComm(m_hCom,MAXBLOCK,MAXBLOCK);
//设置超时
TimeOuts.ReadIntervalTimeout=MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
TimeOuts.WriteTotalTimeoutMultiplier=0;
SetCommTimeouts(m_hCom,&TimeOuts);
//创建工作者线程
if(SetCommParameter())
{
m_pThread=AfxBeginThread(ComProce,this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
if(m_pThread==NULL)
{
CloseHandle(m_hCom);
AfxMessageBox("线程创建失败!");
m_bConnected=0;
return FALSE;
}
else
{
// m_bConnected=1;
m_pThread->ResumeThread();
}
}
else
{
CloseHandle(m_hCom);
AfxMessageBox("参数设置失败!");
m_bConnected=0;
return FALSE;
}
m_bConnected=1;
return TRUE;
}
UINT ComProce(LPVOID pParam)
{
// AfxMessageBox("建立线程开始!");
OVERLAPPED os;
DWORD dwMask,dwTrans;
COMSTAT ComStat;
DWORD dwErrorFlags;
CCommtestDlg *pDlg=(CCommtestDlg *)pParam;
// m_ThredConnected=pDlg->m_bConnected;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if(os.hEvent==NULL)
{
AfxMessageBox("不能建立事件对象!");
return (UINT)-1;
}
/* BYTE abIn[MAXBLOCK];
CString AcceptRead,StrTemp;*/
// AfxMessageBox("已建立成功!");
while(pDlg->m_bConnected)
{
ClearCommError(pDlg->m_hCom,&dwErrorFlags,&ComStat);
if(ComStat.cbInQue)
{
// AfxMessageBox("receive");
pDlg->ProcessCOMMNotification(EV_RXCHAR,0);
}
dwMask=0;
// AfxMessageBox("no data in!");
if(!WaitCommEvent(pDlg->m_hCom,&dwMask,&os))
{
// AfxMessageBox("wait event");
if(GetLastError()==ERROR_IO_PENDING)
{
// AfxMessageBox("begin wait a data in!");
GetOverlappedResult(pDlg->m_hCom,&os,&dwTrans,TRUE);
// AfxMessageBox("now,there is!");
}
else
{
CloseHandle(os.hEvent);
return(UINT)-1;
}
// AfxMessageBox("wait end");
}
}
CloseHandle(os.hEvent);
// AfxMessageBox("线程结束!");
return 0;
}
BOOL CCommtestDlg::ProcessCOMMNotification (UINT wParam,long lParam)
{
CFile getdatafile;
CFileException e;
// AfxMessageBox("begin read!");
BOOL judgefile=m_judgesend.GetCheck();
//判断串口是否已关闭
if(!m_bConnected)
return FALSE;
//判断收到的是否读缓冲消息
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -