📄 serial portdlg.cpp
字号:
// serial portDlg.cpp : implementation file
//
#include "stdafx.h"
#include "serial port.h"
#include "serial portDlg.h"
#include "iostream.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()
/////////////////////////////////////////////////////////////////////////////
// CSerialportDlg dialog
CSerialportDlg::CSerialportDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialportDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialportDlg)
m_Receive = _T("");
m_Send = _T("");
m_State = _T("");
m_Periods = 0;
m_bcheck = FALSE;
m_Port=0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSerialportDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialportDlg)
DDX_Control(pDX, IDC_COMBOLIST2, m_Comb2);
DDX_Control(pDX, IDC_COMBOLIST, m_Comb);
DDX_Control(pDX, IDC_MSCOMM, m_Comm);
DDX_Text(pDX, IDC_RECEIVEDATA, m_Receive);
DDX_Text(pDX, IDC_SENDDATA, m_Send);
DDX_Text(pDX, IDC_SerialStateEdit, m_State);
DDX_Text(pDX, IDC_Time, m_Periods);
DDX_Check(pDX, IDC_AutoSend, m_bcheck);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialportDlg, CDialog)
//{{AFX_MSG_MAP(CSerialportDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_COMBOLIST, OnSelchangeCombolist)
ON_BN_CLICKED(IDC_OPENPORT, OnOpenport)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_WM_ERASEBKGND()
ON_BN_CLICKED(IDC_Quit, OnQuit)
ON_BN_CLICKED(IDC_INFORMATION, OnInformation)
ON_CBN_SELCHANGE(IDC_COMBOLIST2, OnSelchangeCombolist2)
ON_BN_CLICKED(IDC_ClearRe, OnClearRe)
ON_BN_CLICKED(IDC_ClearSend, OnClearSend)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_AutoSend, OnAutoSend)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialportDlg message handlers
BOOL CSerialportDlg::OnInitDialog()
{
CDialog::OnInitDialog();
strcpy(PortNum1,"COM1");
strcpy(PortNum2,"COM2");
strcpy(Rb1,"4800");
strcpy(Rb2,"9600");
this->m_Comb.AddString((LPCTSTR)PortNum1);
this->m_Comb.AddString((LPCTSTR)PortNum2);
this->m_Comb2.AddString((LPCTSTR)Rb1);
this->m_Comb2.AddString((LPCTSTR)Rb2);
this->m_Comb.SetCurSel(0);
this->m_Comb2.SetCurSel(0);
// 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
m_State="关 闭";
UpdateData(FALSE);
GetDlgItem(IDC_AutoSend)->EnableWindow(0);
GetDlgItem(IDC_Time)->EnableWindow(0);
m_bcheck=FALSE;
m_Periods=1000;
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSerialportDlg::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 CSerialportDlg::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 CSerialportDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSerialportDlg::InitComm()
{
m_Comm.SetCommPort(m_Port+1);//设置串口 设置或者返回通信端口号。。。一般默认是1,就是打开串口1 端口号的范围是1-16
m_Comm.SetInputMode(1);// 设置数据读取方式 是2进制
switch(m_Speed)
{
case 9600: m_Comm.SetSettings("9600,n,8,1");break;
case 4800: m_Comm.SetSettings("4800,n,8,1");break;
default: break;
}
// 9600是数据传输速率 , N是奇偶校验,8是数据位数,1 是停止位
m_Comm.SetRThreshold(1);//缓冲区 一个字符就可以进行 接受
m_Comm.SetInBufferSize(1024); // 接受缓冲区大小
m_Comm.SetOutBufferSize(1024); // 指定 发送缓冲区 的 大小 ..
m_Comm.SetInBufferCount(0); // 清空 接受缓冲区的
if (!m_Comm.GetPortOpen())
{
m_Comm.SetPortOpen(TRUE);
}
}
// 下面这个 函数 是组合框 选择了 某一个选项 的相应函数
void CSerialportDlg::OnSelchangeCombolist() // 当在对话框 中 选择下拉列表的端口的时候 ,会激发这个消息
{
CString m_str; // 中介
GetDlgItemText(IDC_COMBOLIST,m_str); //得 对话框中某一个控件的文本内容 , 通过 ID号确定哪个控件 , 放在 临时字符串类 变量中 ..
m_Port=atoi(LPCTSTR(m_str))-1; //LPCTSTR 是 组合框条框的 数值类型
// 得到了 串口号
}
void CSerialportDlg::OnOpenport() //"打开串口" 按钮的 相应函数
{
BOOL bState=m_Comm.GetPortOpen();
if (bState==FALSE)
{
InitComm();
GetDlgItem(IDC_OPENPORT)->SetWindowText("关闭串口");
m_State="打 开";
UpdateData(FALSE);
GetDlgItem(IDC_AutoSend)->EnableWindow(1);
}
else
{ SetTimer(1,INFINITE,NULL);
m_bcheck=FALSE;
GetDlgItem(IDC_AutoSend)->EnableWindow(0);
GetDlgItem(IDC_Time)->EnableWindow(0);
m_Comm.SetPortOpen(0);
GetDlgItem(IDC_OPENPORT)->SetWindowText("打开串口");
m_State="关 闭";
UpdateData(FALSE);
}
// GetDlgItem(IDC_OPENPORT)->SetWindowText(bState? "关闭串口" : "打开串口");
}
/*void CSerialportDlg::OnClostport() //"关闭串口"的按钮 相应函数
{
if (m_Comm.GetPortOpen())
{
m_Comm.SetPortOpen(0); // 作一个 关闭的 动作 ...
}
}*/
void CSerialportDlg::OnSend() // "发送按钮"的相应函数
{
UpdateData(TRUE); // 把发送EDIT控件 中的 文本数据 放在了 CString 类型 的m_send 中
int Count=m_Send.GetLength(); // 看看这次发送的数据 有多长 ....
CByteArray m_Array; //字节类,
m_Array.RemoveAll();
m_Array.SetSize(Count);
for (int i=0;i<Count;i++)
m_Array.SetAt(i,m_Send[i]);
m_Comm.SetOutput(COleVariant (m_Array)); //发送 处理
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -