📄 myserialfourdlg.cpp
字号:
// mySerialFourDlg.cpp : implementation file
//
#include "stdafx.h"
#include "mySerialFour.h"
#include "mySerialFourDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BOOL MARK=FALSE;
UINT m_nTimer;
/////////////////////////////////////////////////////////////////////////////
// CMySerialFourDlg dialog
CMySerialFourDlg::CMySerialFourDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMySerialFourDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMySerialFourDlg)
m_ReceiveString = _T("");
m_AutoSend = FALSE;
MARK=FALSE;
m_Delay = 1000;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMySerialFourDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMySerialFourDlg)
DDX_Control(pDX, IDC_STATICstu, m_ConnectStatu);
DDX_Control(pDX, IDC_COMBO1, m_BaudRate);
DDX_Control(pDX, IDC_NUM, m_numText);
DDX_Control(pDX, IDC_STATUS, m_portStatus);
DDX_Control(pDX, IDC_RECEIVE, m_CReStr);
DDX_Control(pDX, IDC_TRANS, m_transWnd);
DDX_Control(pDX, IDC_COMBO, m_comboCom);
DDX_Text(pDX, IDC_RECEIVE, m_ReceiveString);
DDX_Check(pDX, IDC_CHECK1, m_AutoSend);
DDX_Text(pDX, IDC_EDIT1, m_Delay);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMySerialFourDlg, CDialog)
//{{AFX_MSG_MAP(CMySerialFourDlg)
ON_BN_CLICKED(IDC_BTRANS, OnBtrans)
ON_BN_CLICKED(IDC_BOPEN, OnBopen)
ON_BN_CLICKED(IDC_BCLOSE, OnBclose)
ON_CBN_CLOSEUP(IDC_COMBO, OnCloseupCombo)
ON_MESSAGE(WM_COMM_RCHAR, showReceiveStr)
ON_EN_MAXTEXT(IDC_RECEIVE, OnMaxtextReceive)
ON_EN_MAXTEXT(IDC_TRANS, OnMaxtextTrans)
ON_EN_CHANGE(IDC_TRANS, OnChangeTrans)
ON_CBN_CLOSEUP(IDC_COMBO1, OnCloseupCombo1)
ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
ON_WM_TIMER()
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_EN_KILLFOCUS(IDC_EDIT1, OnKillfocusEdit1)
ON_BN_CLICKED(IDC_BClear, OnBClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMySerialFourDlg message handlers
BOOL CMySerialFourDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
m_comboCom.SetCurSel(0);//设置下拉框里的初始值为"COM1:"
m_BaudRate.SetCurSel(5);
m_strCom="COM1:";
m_strCom1=9600;
ab=new CSerial();//初始化类CSerial
m_CReStr.SetLimitText(2*1024);//设置该控件最大容量
m_transWnd.SetLimitText(2*1024);//设置该控件最大容量
m_ConnectStatu.SetWindowText(_T("没有打开串口"));//开始未连接
// TODO: Add extra initialization here
OnBopen();
return TRUE; // return TRUE unless you set the focus to a control
}
void CMySerialFourDlg::OnBtrans()
{
// TODO: Add your control notification handler code here
TCHAR *tp=new TCHAR[2048];//为发送数据暂存设置空间
DWORD num;
BOOL MASK=FALSE;
CString str;//为发送数据暂存设置空间
m_transWnd.GetWindowText(str);//取得编辑框里的数据
num=(DWORD)str.GetLength();
lstrcpy(tp, str);//将str内容给指针所指的内存,由于WritePort只接收TCHAR类型
MASK=ab->WritePort(tp,num);//写出去
/* if(MASK)
{
m_CReStr.ReplaceSel(_T("\r\nSend<<\r\n"),FALSE);
m_CReStr.ReplaceSel(tp,FALSE);//显示已经发送的数据
//m_transWnd.SetWindowText(_T(""));//清空发送框
}
*/
delete tp;
//::MessageBox(NULL,_T("数据已发送!"),_T("提示!"),MB_ICONEXCLAMATION);
}
void CMySerialFourDlg::OnBopen()
{
// TODO: Add your control notification handler code here
if(!(MARK))
{
if (!(MARK=(ab->OpenPort(m_strCom,m_hWnd,m_strCom1))))
{
ab->ClosePort();//关闭串口
m_ConnectStatu.SetWindowText(_T("没有打开串口"));
return;
}
}
else
{
::MessageBox(NULL, TEXT("串口已经打开!"),TEXT("警告!"), MB_OK);
}
//显示此时已有连接
//m_portStatus.SetWindowText(_T("The "+ m_strCom+"has been connected"));
m_ConnectStatu.SetWindowText(_T(""+ m_strCom+" 打开"));
}
void CMySerialFourDlg::OnBclose()
{
// TODO: Add your control notification handler code here
delete ab;//删除CSreial对象
MARK=FALSE;
m_ConnectStatu.SetWindowText(_T("没有打开串口"));
//显示此时没有连接
}
void CMySerialFourDlg::OnCloseupCombo()
{
// TODO: Add your control notification handler code here
int a=m_comboCom.GetCurSel();//取得当前游标
if (a==0) m_strCom="COM1:" ;//根据游标值得到相应的字符
else if (a==1) m_strCom="COM2:";
else if (a==2) m_strCom="COM3:";
m_comboCom.UpdateData(true);//刷新
}
void CMySerialFourDlg::showReceiveStr(WPARAM wParam)//接收到消息,准备显示字符
{
//m_CReStr.ReplaceSel(_T("\r\nReceive>>\r\n"),FALSE);//表示这时显示的是接收的字符
m_CReStr.ReplaceSel((TCHAR *)wParam,FALSE);//显示接收的字符
}
void CMySerialFourDlg::OnMaxtextReceive() //控件IDC_RECEIVE中数据过量时执行
{
// TODO: Add your control notification handler code here
m_CReStr.SetWindowText(_T(""));//设置里面数据为空
}
void CMySerialFourDlg::OnMaxtextTrans() //控件IDC_TRANS中数据过量时执行
{
// TODO: Add your control notification handler code here
::MessageBox (NULL, TEXT("输入字符数量不能超过2048个字符!"),TEXT("警告!"), MB_OK);//提示用户已输入过量信息
}
void CMySerialFourDlg::OnChangeTrans() //每输入一个字符都会执行
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
CString str;//为发送数据暂存设置空间
char te[3];
m_transWnd.GetWindowText(str);//取得编辑框里的数据
sprintf(te,"%d",(DWORD)str.GetLength());//计算数据长度,并将其转化为字符串
str=(CString)te;//转化为CString结构
m_numText.SetWindowText((LPCTSTR)str);//用静态文本控件显示,还能输入的字符数目
}
void CMySerialFourDlg::OnCloseupCombo1()
{
// TODO: Add your control notification handler code here
int b=m_BaudRate.GetCurSel();//取得当前游标
if (b==0) m_strCom1=300 ;//根据游标值得到相应的字符
else if (b==1) m_strCom1=600;
else if (b==2) m_strCom1=1200;
else if (b==3) m_strCom1=2400;
else if (b==4) m_strCom1=4800;
else if (b==5) m_strCom1=9600;
else if (b==6) m_strCom1=19200;
else if (b==7) m_strCom1=28800;
else if (b==8) m_strCom1=38400;
else if (b==9) m_strCom1=43000;
else if (b==10) m_strCom1=56000;
else if (b==11) m_strCom1=57600;
else if (b==12) m_strCom1=115200;
m_BaudRate.UpdateData(true);//刷新
if(MARK) ab->InitDCB(m_strCom1);
}
void CMySerialFourDlg::OnCheck1()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(m_AutoSend)
{
OnStartTimer();
}
else
{
OnStopTimer();
}
}
void CMySerialFourDlg::OnStartTimer()
{
m_nTimer = SetTimer(1, m_Delay, 0);
}
void CMySerialFourDlg::OnStopTimer()
{
KillTimer(m_nTimer);
}
void CMySerialFourDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(MARK)
{
OnBtrans();
}
CDialog::OnTimer(nIDEvent);
}
void CMySerialFourDlg::OnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(true);
}
void CMySerialFourDlg::OnKillfocusEdit1()
{
// TODO: Add your control notification handler code here
if(m_AutoSend)
{
OnStopTimer();
OnStartTimer();
}
}
void CMySerialFourDlg::OnBClear()
{
// TODO: Add your control notification handler code here
m_CReStr.SetWindowText(_T(""));//设置里面数据为空
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -