📄 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
/////////////////////////////////////////////////////////////////////////////
// CMySerialFourDlg dialog
CMySerialFourDlg::CMySerialFourDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMySerialFourDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMySerialFourDlg)
m_ReceiveString = _T("");
//}}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_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);
//}}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)
//}}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_strCom="COM1:";//
ab=new CSerial();//初始化类CSerial
m_CReStr.SetLimitText(2*1024);//设置该控件最大容量
m_transWnd.SetLimitText(250);//设置该控件最大容量
m_portStatus.SetWindowText(_T("The port has not been connected"));//开始未连接
// TODO: Add extra initialization here
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[256];//为发送数据暂存设置空间
DWORD num;
CString str;//为发送数据暂存设置空间
m_transWnd.GetWindowText(str);//取得编辑框里的数据
num=(DWORD)str.GetLength();
lstrcpy(tp, str);//将str内容给指针所指的内存,由于WritePort只接收TCHAR类型
m_CReStr.ReplaceSel(_T("\r\nSend<<\r\n"),FALSE);
m_CReStr.ReplaceSel(tp,FALSE);//显示已经发送的数据
ab->WritePort(tp,num);//写出去
m_transWnd.SetWindowText(_T(""));//清空发送框
delete tp;
// ::MessageBox(NULL,_T("数据已发送!"),_T("提示!"),MB_ICONEXCLAMATION);
}
void CMySerialFourDlg::OnBopen()
{
// TODO: Add your control notification handler code here
if (!(ab->OpenPort(m_strCom,m_hWnd)))
{
ab->ClosePort();//打开串口
m_portStatus.SetWindowText(_T("The port has not been connected"));
return;
}
//显示此时已有连接
m_portStatus.SetWindowText(_T("The "+ m_strCom+"has been connected"));
}
void CMySerialFourDlg::OnBclose()
{
// TODO: Add your control notification handler code here
delete ab;//删除CSreial对象
m_portStatus.SetWindowText(_T("The port has not been connected"));
//显示此时没有连接
}
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:";
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("Can't input more data!"),
TEXT("NOTICE!"), 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",250-(DWORD)str.GetLength());//计算数据长度,并将其转化为字符串
str=(CString)te;//转化为CString结构
m_numText.SetWindowText((LPCTSTR)str);//用静态文本控件显示,还能输入的字符数目
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -