// SerialCommDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SerialComm.h"
#include "SerialCommDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <atlconv.cpp>
#define MESSAGELENGTH 256
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CSerialCommDlg dialog
CSerialCommDlg::CSerialCommDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialCommDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialCommDlg)
m_szRecvData = _T("");
m_szSendData = _T("");
m_szCOMName = _T("");
m_szDataBits = _T("");
m_szParity = _T("");
m_szStopBits = _T("");
m_szBaud = _T("");
m_nSendCount = 0;
m_nRecvCount = 0;
m_szStatus = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_iRecvDataCount = 0;
}
void CSerialCommDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialCommDlg)
DDX_Control(pDX, IDC_BAUD, m_ctrlBaud);
DDX_Control(pDX, IDC_STOP_BIT, m_ctrlStopBits);
DDX_Control(pDX, IDC_PARITY, m_ctrlParity);
DDX_Control(pDX, IDC_DATA_BIT, m_ctrlDataBits);
DDX_Control(pDX, IDC_COM_NAME, m_ctrlCOMName);
DDX_Control(pDX, IDC_RECV_DATA, m_Recv_Ctrl);
DDX_Control(pDX, IDC_MSCOMM, m_COM);
DDX_Text(pDX, IDC_RECV_DATA, m_szRecvData);
DDX_Text(pDX, IDC_SEND_DATA, m_szSendData);
DDX_CBString(pDX, IDC_COM_NAME, m_szCOMName);
DDV_MaxChars(pDX, m_szCOMName, 4);
DDX_CBString(pDX, IDC_DATA_BIT, m_szDataBits);
DDV_MaxChars(pDX, m_szDataBits, 1);
DDX_CBString(pDX, IDC_PARITY, m_szParity);
DDV_MaxChars(pDX, m_szParity, 8);
DDX_CBString(pDX, IDC_STOP_BIT, m_szStopBits);
DDV_MaxChars(pDX, m_szStopBits, 1);
DDX_CBString(pDX, IDC_BAUD, m_szBaud);
DDV_MaxChars(pDX, m_szBaud, 10);
DDX_Text(pDX, IDC_SEND_COUNT, m_nSendCount);
DDX_Text(pDX, IDC_RECV_COUNT, m_nRecvCount);
DDX_Text(pDX, IDC_STATUS, m_szStatus);
DDV_MaxChars(pDX, m_szStatus, 8);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialCommDlg, CDialog)
//{{AFX_MSG_MAP(CSerialCommDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_CREATE()
ON_BN_CLICKED(IDC_CLEAR_SEND, OnClearSend)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_CLEAR_RECV, OnClearRecv)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_WM_VSCROLL()
ON_BN_CLICKED(IDC_OPEN_CLOSE, OnOpenClose)
ON_CBN_SELCHANGE(IDC_BAUD, OnSelchangeBaud)
ON_CBN_SELCHANGE(IDC_COM_NAME, OnSelchangeComName)
ON_CBN_SELCHANGE(IDC_DATA_BIT, OnSelchangeDataBit)
ON_CBN_SELCHANGE(IDC_PARITY, OnSelchangeParity)
ON_CBN_SELCHANGE(IDC_STOP_BIT, OnSelchangeStopBit)
ON_BN_CLICKED(IDC_CLEAR_COUNT, OnClearCount)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialCommDlg message handlers
BOOL CSerialCommDlg::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_COM.SetCommPort(1);
if (!m_COM.GetPortOpen())
{
m_COM.SetSettings("57600,N,8,1");
m_COM.SetPortOpen(true);
m_COM.SetInBufferCount(0);
SetTimer(1,10,NULL);
InBuffer.bstrVal = new unsigned short[MESSAGELENGTH];
OutBuffer.bstrVal = new unsigned short[MESSAGELENGTH];
OutBuffer.vt=VT_BSTR;
}
*/
if(m_COM.GetPortOpen())
{
m_COM.SetPortOpen(FALSE);
}
m_COM.SetCommPort(1); //选择com1
if( !m_COM.GetPortOpen())
{
m_COM.SetPortOpen(TRUE);//打开串口
m_szStatus = "打开";
}
else
{
AfxMessageBox("cannot open serial port");
}
m_COM.SetSettings("9600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位
m_COM.SetInputMode(1); //1:表示以二进制方式检取数据
m_COM.SetRThreshold(1);
//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
m_COM.SetInputLen(0); //设置当前接收区数据长度为0
m_COM.GetInput(); //先预读缓冲区以清除残留数据
m_nSendCount = 0;
m_nRecvCount = 0;
//m_szStatus = "关闭";
UpdateData(FALSE);
int min = 0;
int max = 10;
m_Recv_Ctrl.SetScrollRange (SB_VERT, min, max, TRUE);
//m_Recv_Ctrl.SetScrollPos (SB_VERT, 0, TRUE);
//m_ctrlCOMName.GetCount();
if (m_ctrlCOMName.SetCurSel(0) == CB_ERR ||
m_ctrlBaud.SetCurSel(5) == CB_ERR ||
m_ctrlDataBits.SetCurSel(3) == CB_ERR ||
m_ctrlParity.SetCurSel(0) == CB_ERR ||
m_ctrlStopBits.SetCurSel(0) == CB_ERR)
{
::AfxMessageBox("初始化失败");
}
if(m_COM.GetPortOpen())
{
GetDlgItem(IDC_OPEN_CLOSE)->SetWindowText("关闭串口");
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CSerialCommDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CSerialCommDlg::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 CSerialCommDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BEGIN_EVENTSINK_MAP(CSerialCommDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CSerialCommDlg)
ON_EVENT(CSerialCommDlg, IDC_MSCOMM, 1 /* OnComm */, OnCommMscomm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CSerialCommDlg::OnCommMscomm()
{
UpdateData(TRUE);//取控件的数据
switch(m_COM.GetCommEvent())
{
case 1: //comEvSend事件
{
//如有数据要发送,可采用以下代码:
//::AfxMessageBox("发送数据");
// OnSend();
}
break;
case 2: //comEvReceiv事件,有数据到达
{
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signe