📄 comdlg.cpp
字号:
// comDlg.cpp : implementation file
//
#include "stdafx.h"
#include "com.h"
#include "comDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CComDlg dialog
CComDlg::CComDlg(CWnd* pParent /*=NULL*/)
: CDialog(CComDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CComDlg)
m_addrh = -1;
m_addrl = -1;
m_baud = -1;
m_port = -1;
m_D1h = -1;
m_D1l = -1;
m_D2h = -1;
m_D2l = -1;
m_strReceive = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CComDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CComDlg)
DDX_Control(pDX, IDC_COMTXD, m_filename);
DDX_Control(pDX, IDC_COMRXD, m_nEdit);
DDX_CBIndex(pDX, IDC_ADDRh, m_addrh);
DDX_CBIndex(pDX, IDC_ADDRl, m_addrl);
DDX_CBIndex(pDX, IDC_ComBaud, m_baud);
DDX_CBIndex(pDX, IDC_ComPort, m_port);
DDX_CBIndex(pDX, IDC_D1h, m_D1h);
DDX_CBIndex(pDX, IDC_D1l, m_D1l);
DDX_CBIndex(pDX, IDC_D2h, m_D2h);
DDX_CBIndex(pDX, IDC_D2l, m_D2l);
DDX_Control(pDX, IDC_MSCOMM, m_com);
DDX_Text(pDX, IDC_COMRXD, m_strReceive);
DDV_MaxChars(pDX, m_strReceive, 1024);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CComDlg, CDialog)
//{{AFX_MSG_MAP(CComDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OPEN, OnOpen)
ON_BN_CLICKED(IDC_CLOSE, OnClose)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_SAVE, OnSave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CComDlg message handlers
BOOL CComDlg::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_port=0;
m_baud=0;
m_addrh=0;
m_addrl=0;
m_D1h=0;
m_D1l=0;
m_D2h=0;
m_D2l=0;
text_RXD[0] = '\0';
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CComDlg::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 CComDlg::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 CComDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CComDlg::OnOpen()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_com.SetCommPort(m_port+1); //选择COM1
m_com.SetInBufferSize(1024); //设置输入缓冲区的大小,Bytes
m_com.SetOutBufferSize(256); //设置输出缓冲区的大小,Bytes
if(!m_com.GetPortOpen()) //打开串口
m_com.SetPortOpen(TRUE);
m_com.SetInputMode(1); //设置输入方式为二进制方式
switch (m_baud)//设置波特率等参数
{
case 0: m_com.SetSettings("4800,s,8,1"); break;
case 1: m_com.SetSettings("19200,s,8,1"); break;
default :break;
}
m_com.SetRThreshold(1); //为1表示有一个字符引发一个事件
m_com.SetInputLen(0);
GetDlgItem(IDC_CLOSE)->EnableWindow(TRUE);
GetDlgItem(IDC_OPEN)->EnableWindow(FALSE);
GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
}
void CComDlg::OnClose()
{
// TODO: Add your control notification handler code here
m_com.SetPortOpen(FALSE);
GetDlgItem(IDC_CLOSE)->EnableWindow(FALSE);
GetDlgItem(IDC_OPEN)->EnableWindow(TRUE);
GetDlgItem(IDC_SEND)->EnableWindow(FALSE);
GetDlgItem(IDC_COMRXD)->SetWindowText(0);
GetDlgItem(IDC_COMTXD)->SetWindowText(0);
GetDlgItem(IDC_SAVE)->EnableWindow(FALSE);
}
void CComDlg::OnSend()
{
// TODO: Add your control notification handler code here
char TxData[2];
CByteArray array;
CString m_strT;
char text_TXD[10];
UpdateData(TRUE);
//发ADDR
m_com.SetPortOpen(FALSE);
m_com.SetCommPort(m_port+1); //选择COM1
m_com.SetInBufferSize(1024); //设置输入缓冲区的大小,Bytes
m_com.SetOutBufferSize(256); //设置输出缓冲区的大小,Bytes
if(!m_com.GetPortOpen()) //打开串口
m_com.SetPortOpen(TRUE);
m_com.SetInputMode(1); //设置输入方式为二进制方式
switch (m_baud)
{
case 0: m_com.SetSettings("4800,m,8,1"); break;
case 1: m_com.SetSettings("19200,m,8,1"); break;
default :break;
}
m_com.SetRThreshold(1); //为1表示有一个字符引发一个事件
m_com.SetInputLen(0);
TxData[0] = m_addrh*16+m_addrl;
array.RemoveAll();
array.SetSize(1);
array.SetAt(0,TxData[0]);
m_com.SetOutput(COleVariant(array));
//发DATA
m_com.SetPortOpen(FALSE);
m_com.SetCommPort(m_port+1); //选择COM1
m_com.SetInBufferSize(1024); //设置输入缓冲区的大小,Bytes
m_com.SetOutBufferSize(256); //设置输出缓冲区的大小,Bytes
if(!m_com.GetPortOpen()) //打开串口
m_com.SetPortOpen(TRUE);
m_com.SetInputMode(1); //设置输入方式为二进制方式
switch (m_baud)
{
case 0: m_com.SetSettings("4800,s,8,1"); break;
case 1: m_com.SetSettings("19200,s,8,1"); break;
default :break;
}
m_com.SetRThreshold(1); //为1表示有一个字符引发一个事件
m_com.SetInputLen(0);
TxData[0] = m_D1h*16+m_D1l;
TxData[1] = m_D2h*16+m_D2l;
array.RemoveAll();
array.SetSize(2);
array.SetAt(0,TxData[0]);
array.SetAt(1,TxData[1]);
m_com.SetOutput(COleVariant(array));
//显示发送数据
if(m_addrh>9)
text_TXD[0]=m_addrh+55;
else
text_TXD[0]=m_addrh+48;
if(m_addrl>9)
text_TXD[1]=m_addrl+55;
else
text_TXD[1]=m_addrl+48;
if(m_D1h>9)
text_TXD[2]=m_D1h+55;
else
text_TXD[2]=m_D1h+48;
if(m_D1l>9)
text_TXD[3]=m_D1l+55;
else
text_TXD[3]=m_D1l+48;
if(m_D2h>9)
text_TXD[4]=m_D2h+55;
else
text_TXD[4]=m_D2h+48;
if(m_D2l>9)
text_TXD[5]=m_D2l+55;
else
text_TXD[5]=m_D2l+48;
text_TXD[6] = '\0';
m_strT = text_TXD;
GetDlgItem(IDC_COMTXD)->SetWindowText(m_strT);
GetDlgItem(IDC_SAVE)->EnableWindow(TRUE);
}
void CComDlg::OnSave()
{
// TODO: Add your control notification handler code here
CFile file;
CString strName;
CString strData;
CTime t;
int nLength;
m_filename.GetWindowText(strName);
strName += ".txt";
nLength=m_nEdit.GetWindowTextLength();
m_nEdit.GetWindowText(strData);
file.Open(strName,CFile::modeCreate | CFile::modeWrite);
file.Write(strData,nLength);
file.Close();
GetDlgItem(IDC_SAVE)->EnableWindow(FALSE);
}
BEGIN_EVENTSINK_MAP(CComDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CComDlg)
ON_EVENT(CComDlg, IDC_MSCOMM, 1 /* OnComm */, OnOnCommMscomm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CComDlg::OnOnCommMscomm()
{
// TODO: Add your control notification handler code here
VARIANT vResponse;
unsigned char *str;
// char *str1;
// char *str2;
int k, nEvent, i,j;
unsigned char high,low;
//unsigned char bin[100];
i=m_nEdit.GetWindowTextLength();
nEvent = m_com.GetCommEvent();
switch(nEvent)
{
case 2: //收到大于RTHresshold个字符
k = m_com.GetInBufferCount(); //接收到的字符数目
if(k > 0)
{
//AfxMessageBox("have received");
vResponse=m_com.GetInput(); //read
//对数据进行其他处理
str = (unsigned char*)(unsigned char*) vResponse.parray->pvData;
}
if(i>1024-(3*k))
i=0;
j = 0;
while (j < k)
{
j++;
high=*str/16;
low=*str%16;
if(high>9)
text_RXD[i]=high+55;
else
text_RXD[i]=high+48;
i++;
if(low>9)
text_RXD[i]=low+55;
else
text_RXD[i]=low+48;
i++;
text_RXD[i]=' ';
i++;
str++;
}
text_RXD[i] = '\0';
m_strReceive = text_RXD;
GetDlgItem(IDC_COMRXD)->SetWindowText(m_strReceive);
//清除字符串中的不必要字符
break;
case 3: //CTS线状态发生了变化
break;
case 4: //DSR线状态发生了变化
break;
case 5: //CD线状态发生了变化
break;
case 6: //Ring Indicator发生变化
break;
default:
break;
}
//m_nEdit.GetWindowTextLength();
//AfxMessageBox("have received");
//UpdateData(FALSE);
//m_nEdit.Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -