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

📄 multithreadserialappdlg.cpp

📁 动态库(dll)中建立串口监视线程的实时串口通信
💻 CPP
字号:
// MultithreadSerialAppDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultithreadSerialApp.h"
#include "MultithreadSerialAppDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMultithreadSerialAppDlg dialog

CMultithreadSerialAppDlg::CMultithreadSerialAppDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMultithreadSerialAppDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMultithreadSerialAppDlg)
	m_SendData = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	//m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMultithreadSerialAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMultithreadSerialAppDlg)
	DDX_Control(pDX, IDC_COMSTATUS, m_ComStatus);
	DDX_Control(pDX, IDC_THREADSTATUS, m_ThreadStatus);
	DDX_Control(pDX, IDC_LISTACTIVE, m_ListActive);
	DDX_Control(pDX, IDC_LISTCHECK, m_ListCheck);
	DDX_Text(pDX, IDC_EDIT1, m_SendData);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMultithreadSerialAppDlg, CDialog)
	//{{AFX_MSG_MAP(CMultithreadSerialAppDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(IDC_CLEARAQUIRELIST, OnClearaquirelist)
	ON_COMMAND(IDC_CLEARATCTIVELIST, OnClearatctivelist)
	ON_COMMAND(IDC_CLOSECOM, OnClosecom)
	ON_COMMAND(IDC_OPENCOM1, OnOpencom1)
	ON_COMMAND(IDC_OPENCOM2, OnOpencom2)
	ON_COMMAND(IDC_STARTTHREAD, OnStartthread)
	ON_BN_CLICKED(IDC_SENDCOMND, OnSendcomnd)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_ACTIVE_TRANS,OnActiveTrans)
	ON_MESSAGE(WM_SEND_AND_RETURN, OnSendAndReturn)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMultithreadSerialAppDlg message handlers

BOOL CMultithreadSerialAppDlg::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
	
	LVCOLUMN  icoloum231;
	m_ListCheck.InsertColumn(0, &icoloum231);
    memset(&sItem231, 0, sizeof(LVITEM));
	memset(&icoloum231, 0, sizeof(LVCOLUMN ));
	icoloum231.mask=LVCF_TEXT | LVCF_WIDTH | LVCF_FMT | LVCF_SUBITEM;
	icoloum231.fmt = LVCFMT_CENTER;//LVCFMT_LEFT ;
	icoloum231.cx = 110;
	icoloum231.pszText= "   项 目";
	icoloum231.iSubItem = 0;  
	m_ListCheck.InsertColumn(0, &icoloum231);
    icoloum231.pszText = "查询测量结果";
	icoloum231.cx = 200;
	icoloum231.iSubItem = 1;
    m_ListCheck.InsertColumn(1, &icoloum231);
    DWORD dstyle231 = m_ListCheck.GetStyle();
    m_ListCheck.SetExtendedStyle(dstyle231 | LVS_EX_FULLROWSELECT);
    sItem231.mask=LVIF_TEXT;
   	sItem231.cchTextMax = 25;

	LVCOLUMN  iActive;
	m_ListActive.InsertColumn(0, &iActive);
    memset(&sItem231, 0, sizeof(LVITEM));
	memset(&iActive, 0, sizeof(LVCOLUMN ));
	iActive.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT | LVCF_SUBITEM;
	iActive.fmt = LVCFMT_CENTER;//LVCFMT_LEFT ;
	iActive.cx = 120;
	iActive.pszText= "    项 目";
	iActive.iSubItem = 0;  
	m_ListActive.InsertColumn(0, &iActive);
    iActive.pszText = "主动上传结果";
	iActive.cx = 290;
	iActive.iSubItem = 1;
    m_ListActive.InsertColumn(1, &iActive);
    DWORD dstyleActive = m_ListActive.GetStyle();
    m_ListActive.SetExtendedStyle(dstyleActive | LVS_EX_FULLROWSELECT);
    sItem231.mask=LVIF_TEXT;
   	sItem231.cchTextMax = 25;

	CString l_dt;
	l_dt.Format("%s", "未启动");
    m_ThreadStatus.SetWindowText(l_dt);

	CString l_dtCom;
	l_dtCom.Format("%s", "关 闭");
    m_ComStatus.SetWindowText(l_dtCom);

	this->m_Hwnd = this->m_MSG.hwnd = this->GetSafeHwnd(); //必须取得句柄
	Com1Status = false;
	Com2Status = false;
	ThreadStatus = false;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMultithreadSerialAppDlg::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 CMultithreadSerialAppDlg::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 CMultithreadSerialAppDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
/*------------------------------------------
//说明:清查询列表
--------------------------------------------*/
void CMultithreadSerialAppDlg::OnClearaquirelist() 
{
	m_ListCheck.DeleteAllItems();		
}
/*------------------------------------------
//说明:清主动接收列表
--------------------------------------------*/
void CMultithreadSerialAppDlg::OnClearatctivelist() 
{
	m_ListActive.DeleteAllItems();		
}
/*------------------------------------------
//说明:关闭串口
--------------------------------------------*/
void CMultithreadSerialAppDlg::OnClosecom() 
{
	CString l_dtCom;
	if(this->m_tThreadSerial.Com1IsOpen == true)
	{
		this->m_tThreadSerial.CloseComm();
		l_dtCom.Format("%s", "关 闭");
		m_ComStatus.SetWindowText(l_dtCom);
		Com1Status = false;
	}
	if(this->m_tThreadSerial.Com1IsOpen == true)
	{
		this->m_tThreadSerial.CloseComm();
		l_dtCom.Format("%s", "关 闭");
		m_ComStatus.SetWindowText(l_dtCom);
		Com2Status = false;
	}
}
/*------------------------------------------
//说明:打开COM1
--------------------------------------------*/
void CMultithreadSerialAppDlg::OnOpencom1() 
{
	CString l_dtCom;
	if(Com1Status == true)
		return;
	bool l_bReturn = this->m_tThreadSerial.InitComm(true, 1, m_Hwnd);
	if(l_bReturn == true)
	{
		l_dtCom.Format("%s", "打 开");
		m_ComStatus.SetWindowText(l_dtCom);
		Com1Status = true;
	}
	else
	{	
		l_dtCom.Format("%s", "关 闭");
		m_ComStatus.SetWindowText(l_dtCom);
		Com1Status = false;
		AfxMessageBox("打开COM1失败");
	}	
}
/*------------------------------------------
//说明:打开COM2
--------------------------------------------*/
void CMultithreadSerialAppDlg::OnOpencom2() 
{
	CString l_dtCom;
	if(Com2Status == true)
	  return;
	bool l_bReturn = this->m_tThreadSerial.InitComm(true, 2, m_Hwnd);
	if(l_bReturn == true)
	{
		l_dtCom.Format("%s", "打 开");
		m_ComStatus.SetWindowText(l_dtCom);
		Com2Status = true;
	}
	else
	{	
		l_dtCom.Format("%s", "关 闭");
		m_ComStatus.SetWindowText(l_dtCom);
		Com2Status = false;
		AfxMessageBox("打开COM2失败");
	}		
}
/*------------------------------------------
//说明:启动线程
--------------------------------------------*/
void CMultithreadSerialAppDlg::OnStartthread() 
{
	 bool l_bReturn;
	 CString l_dt;
     l_bReturn = this->m_tThreadSerial.BeginComm();//启动线程
	 if(l_bReturn == TRUE)
	 {
		l_dt.Format("%s", "启 动");
        m_ThreadStatus.SetWindowText(l_dt);
		ThreadStatus = true;
	 }
	 else
	 {
		l_dt.Format("%s", "未启动");
        m_ThreadStatus.SetWindowText(l_dt);
		ThreadStatus = false;
		AfxMessageBox("启动线程失败,串行通信不能进行");
	 }	
}
/*------------------------------------------
//说明:发送查询命令
--------------------------------------------*/
void CMultithreadSerialAppDlg::OnSendcomnd() 
{
	if((Com1Status == false) && (Com2Status == false))
	{ 
		AfxMessageBox("请先打开串口!");
		return;
	}
	if(ThreadStatus == false)
	{
       AfxMessageBox("请线启动线程!");
	}
	UpdateData(TRUE);
	BYTE l_strData[100] = {0};
	//int l_iLength = 1;
	int l_iLength = strlen(m_SendData);
	for(int i = 0; i < l_iLength; i++)
	{
	   l_strData[i] = m_SendData[i];
	}
	int l_iReturn;
	l_iReturn = this->m_tThreadSerial.WriteComm(l_strData, l_iLength);
    if(l_iReturn != l_iLength)
	{
		AfxMessageBox("发送数据失败");
	}			
}
/*------------------------------------------
//说明:主动上传的响应函数
--------------------------------------------*/
void CMultithreadSerialAppDlg::OnActiveTrans()
{
	BYTE l_iReturnData[512] = {0};
    this->m_tThreadSerial.m_bReData;
	memcpy(l_iReturnData, this->m_tThreadSerial.m_bReData, sizeof(this->m_tThreadSerial.m_bReData));
    m_ListActive.DeleteAllItems();
	CString  pszText[1]; 
	pszText[0].Format("%s", l_iReturnData);
	int aiReturn[1];
	aiReturn[0]=m_ListActive.InsertItem(0," 下位机主动上传值");
	m_ListActive.SetItemText(aiReturn[0], 1, pszText[0]);

}
/*------------------------------------------
//说明:查询方式的返回
--------------------------------------------*/
void CMultithreadSerialAppDlg::OnSendAndReturn()
{
	BYTE l_iReturnData[512] = {0};
    this->m_tThreadSerial.m_bReData;
	memcpy(l_iReturnData, this->m_tThreadSerial.m_bReData, sizeof(this->m_tThreadSerial.m_bReData));
    m_ListCheck.DeleteAllItems();
	CString  pszText[1]; 
	pszText[0].Format("%s", l_iReturnData);
	int aiReturn[1];
	aiReturn[0]=m_ListCheck.InsertItem(0,"  查询返回值");
	m_ListCheck.SetItemText(aiReturn[0], 1, pszText[0]);
}

⌨️ 快捷键说明

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