⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testserialport2dlg.cpp

📁 串口开发的另一种方式.rar
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// TestSerialPort2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestSerialPort2.h"
#include "TestSerialPort2Dlg.h"

#include "superSP.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()

/////////////////////////////////////////////////////////////////////////////
// CTestSerialPort2Dlg dialog

CTestSerialPort2Dlg::CTestSerialPort2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestSerialPort2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestSerialPort2Dlg)
	m_comName = _T("");
	m_uComRate = 0;
	m_uComParity = 0;
	m_uComDataBits = 0;
	m_bKeepSend = FALSE;
	m_bKeepSave = FALSE;
	m_bUseCal = FALSE;
	m_uKeepSendTime = 0;
	m_uKeepSaveTime = 0;
	m_fComStopBits = 0.0f;
	m_strSend = _T("");
	m_receiveBytes = _T("");
	m_sendBytes = _T("");
	m_transferSpeed = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestSerialPort2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestSerialPort2Dlg)
	DDX_Control(pDX, IDC_BUTTON_RECEIVE, m_btnReceive);
	DDX_Control(pDX, IDC_BUTTON_CLOSE, m_btnClose);
	DDX_Control(pDX, IDC_BUTTON_OPEN, m_btnOpen);
	DDX_Text(pDX, IDC_EDIT_COM_NAME, m_comName);
	DDX_Text(pDX, IDC_EDIT_COM_RATE, m_uComRate);
	DDX_Text(pDX, IDC_EDIT_COM_PARITY, m_uComParity);
	DDX_Text(pDX, IDC_EDIT_COM_DATABITS, m_uComDataBits);
	DDX_Check(pDX, IDC_CHECK_KEEP_SEND, m_bKeepSend);
	DDX_Check(pDX, IDC_CHECK_KEEP_SAVE, m_bKeepSave);
	DDX_Check(pDX, IDC_CHECK_USE_CAL, m_bUseCal);
	DDX_Text(pDX, IDC_EDIT_KEEP_TIME, m_uKeepSendTime);
	DDV_MinMaxUInt(pDX, m_uKeepSendTime, 50, 600000);
	DDX_Text(pDX, IDC_EDIT_SAVE_TIME, m_uKeepSaveTime);
	DDV_MinMaxUInt(pDX, m_uKeepSaveTime, 10000, 3600000);
	DDX_Text(pDX, IDC_EDIT_COM_STOPBITS, m_fComStopBits);
	DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
	DDX_Text(pDX, IDC_STATIC_RECEIVE_BYTES, m_receiveBytes);
	DDX_Text(pDX, IDC_STATIC_SEND_BYTES, m_sendBytes);
	DDX_Text(pDX, IDC_STATIC_TRANSFER_SPEED, m_transferSpeed);
	DDX_Control(pDX, IDC_TEXTBOX_RECEIVE, m_mdcReceive);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestSerialPort2Dlg, CDialog)
	//{{AFX_MSG_MAP(CTestSerialPort2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
	ON_BN_CLICKED(IDC_BUTTON_RECEIVE, OnButtonReceive)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_RADIO_HEX, OnRadioHex)
	ON_BN_CLICKED(IDC_RADIO_TEXT, OnRadioText)
	ON_BN_CLICKED(IDC_CHECK_KEEP_SEND, OnCheckKeepSend)
	ON_BN_CLICKED(IDC_CHECK_KEEP_SAVE, OnCheckKeepSave)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CHECK_USE_CAL, OnCheckUseCal)
	ON_EN_CHANGE(IDC_EDIT_KEEP_TIME, OnChangeEditKeepTime)
	ON_EN_CHANGE(IDC_EDIT_SAVE_TIME, OnChangeEditSaveTime)
	ON_BN_CLICKED(IDC_BUTTON_MODEM_STATE, OnButtonModemState)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR_RECEIVE, OnButtonClearReceive)
	ON_BN_CLICKED(IDC_BUTTON_FILE_TRANSFER, OnButtonFileTransfer)
	ON_BN_CLICKED(IDC_BUTTON_FILE_RECEIVE, OnButtonFileReceive)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR_SEND, OnButtonClearSend)
	ON_COMMAND(IDM_TOOL_SENDFILE, OnToolSendfile)
	ON_COMMAND(IDM_TOOL_RECEIVEFILE, OnToolReceivefile)
	ON_COMMAND(IDM_TOOL_MODEMCHECK, OnToolModemcheck)
	ON_COMMAND(IDM_HELP_VERSION, OnHelpVersion)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_UM_RECEIVE_CHAR, OnReceiveChar)
END_MESSAGE_MAP()


//////////////////////////////////////////////////////////////////////////
// Message Do

void CTestSerialPort2Dlg::OnReceiveChar(WPARAM pPackage, LPARAM lParam)
{
	pPACKAGE pPack = (pPACKAGE)pPackage;
	ASSERT(pPack);
	ASSERT(pPack->pData);

    CString str;
	if (m_ctrlEm.bShowAsHex)
	{
		for (int i=0; i<pPack->iLen; i++)
		{
			char hex[3];
			if (pPack->pData[i] > 0x0f)
			{
				sprintf(hex, _T("%x "), pPack->pData[i]);
			}
			else
			{
				sprintf(hex, _T("0%x "), pPack->pData[i]);
			}
			
			str.Insert(i*3, hex);
		}
	}
	else
	{
		for (int i=0; i<pPack->iLen; i++)
		{
			str.Insert(i,pPack->pData[i]);
		}
	}
    
	// 更新接收控件

	m_storeText += str;
	m_mdcReceive.SetText(m_storeText);


	int len = m_storeText.GetLength();
	m_mdcReceive.SetScrollPos(SB_VERT, 100);

	pPack->Clear();
	delete pPack;
}


/////////////////////////////////////////////////////////////////////////////
// CTestSerialPort2Dlg message handlers

BOOL CTestSerialPort2Dlg::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
	if (!Init())
	{
		AfxMessageBox("初始化失败!");
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}


// 初始化
BOOL CTestSerialPort2Dlg::Init()
{
	m_pSSP = new CSuperSerialPort();
	m_pSSP->Attach(this, WM_UM_RECEIVE_CHAR);
	m_preRecByte = m_pSSP->GetReadedBytes();
	

	// Init com data
	m_comName      = _T("COM1");
	m_uComRate     = 9600;
	m_uComParity   = 0;
	m_uComDataBits = 8;
	m_fComStopBits = 1;

	m_uKeepSaveTime = 10000;
	m_uKeepSendTime = 50;

	((CButton *)GetDlgItem(IDC_RADIO_TEXT))->SetCheck(1);
	GetDlgItem(IDC_CHECK_KEEP_SEND)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK_KEEP_SAVE)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK_USE_CAL)->EnableWindow(FALSE);

	m_btnReceive.EnableWindow(FALSE);

	DWORD pathLen = 256;
	TCHAR path[256] = {0};
	GetCurrentDirectory(pathLen, path);
	m_curPath.Format("%s\\", path);

	// modem对话框初始化
	m_pModemStatusDlg = new CModemStatusDlg();
	m_pModemStatusDlg->pSSP = m_pSSP;
	m_pModemStatusDlg->m_atCmdFile = m_curPath + _T("AT\\ATCommands.txt"); 	
	m_pModemStatusDlg->Create(IDD_DIALOG_MODEM_STATUS, this);
	m_pModemStatusDlg->ShowWindow(SW_HIDE);

	// 设置接收文本框的滚动范围
	m_mdcReceive.SetScrollRange(SB_VERT, 1, 100);

	UpdateData(FALSE);
	return true;
}

//

void CTestSerialPort2Dlg::ShowErrorInfo()
{
	CString errInfo;
	DWORD   dwErrCode = m_pSSP->GetErrorCode();
	errInfo.Format("%s", 
		m_pSSP->FormatErrorCodeToString(dwErrCode));
	AfxMessageBox(errInfo);	
}

//
// 保存之前接收到的文本到日志文件
// 并清空文本框 
//
void CTestSerialPort2Dlg::SaveToLog()
{
	CStdioFile f;
	CString fileName;
	if (m_storeText.IsEmpty())
	{
		return;
	}

	CTime   curTime = CTime::GetCurrentTime();
	fileName.Format(_T("%d%d%d-%d%d%d.txt"), curTime.GetYear(),
		curTime.GetMonth(),
		curTime.GetDay(),
		curTime.GetHour(),
		curTime.GetMinute(),
		curTime.GetSecond());

    if (f.Open(m_curPath + fileName, CFile::modeCreate | CFile::modeWrite))
	{
		f.WriteString(m_storeText);
		f.WriteString(m_transferSpeed);
		f.Flush();
		f.Close();
		m_storeText.Empty();
		m_mdcReceive.SetText("");
		UpdateData(false);
	}
	else
	{
		AfxMessageBox(_T("文件创建失败"));
	}
}

void CTestSerialPort2Dlg::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 CTestSerialPort2Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -