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

📄 scommsenderdlg.cpp

📁 发送串口数据
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SCommSenderDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SCommSender.h"
#include "SCommSenderDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BYTE download_mod=0;
unsigned int KUr;
int valid_time;
unsigned int IDb;
unsigned int IDa;
unsigned int KRs=53;
unsigned int num=299;
unsigned int Keys[8];
unsigned int en_N2;
unsigned int en_de_N1;
unsigned int N1=126;
int times=0;
unsigned int de_N2=1;

#define		WM_SENDCA	       WM_USER + 11
#define     WM_SENDHELLODONE   WM_USER + 12 
#define     WM_SENDKEY         WM_USER + 13
#define     WM_RE_EN_N2        WM_USER + 14
#define     WM_EN_DE_N1        WM_USER + 15
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CSCommSenderDlg dialog

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

void CSCommSenderDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSCommSenderDlg)
	DDX_Control(pDX, IDC_MSCOMM1, m_ctrlComm);
	DDX_Text(pDX, IDC_EDIT_RXDATA, m_strEditRXData);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSCommSenderDlg, CDialog)
	//{{AFX_MSG_MAP(CSCommSenderDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_MESSAGE(WM_SENDCA,OnSendCa)
	ON_MESSAGE(WM_SENDHELLODONE,OnSendDone)
    ON_MESSAGE(WM_SENDKEY,OnKUrKey)
    ON_MESSAGE(WM_RE_EN_N2,OnReEnN2)
    ON_MESSAGE(WM_EN_DE_N1,OnEnDeN1)
	ON_BN_CLICKED(IDC_BUTTON_SEND1, OnButtonSend1)
	ON_BN_CLICKED(IDC_BUTTON_SENDKEY, OnButtonSendkey)
	ON_BN_CLICKED(IDC_BUTTON_SENDN2, OnButtonSendn2)
	ON_BN_CLICKED(IDC_BUTTON_SENDKEYS, OnButtonSendkeys)
	ON_BN_CLICKED(IDC_BUTTON_FINISHED, OnButtonFinished)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSCommSenderDlg message handlers

BOOL CSCommSenderDlg::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_ctrlComm.SetCommPort(1);
	m_ctrlComm.SetInputMode(1);
	m_ctrlComm.SetInBufferSize(8192);
	m_ctrlComm.SetOutBufferSize(8192);
	m_ctrlComm.SetSettings("115200,n,8,1");
	if(!m_ctrlComm.GetPortOpen())
		m_ctrlComm.SetPortOpen(TRUE);
	m_ctrlComm.SetRThreshold(1);
	m_ctrlComm.SetInputLen(0);
	m_ctrlComm.GetInput();
	m_ctrlComm.SetNullDiscard(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSCommSenderDlg::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 CSCommSenderDlg::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 CSCommSenderDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSCommSenderDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	PFlag=0;
	QFlag=0;
	
	CFileDialog  myDlg1(TRUE, "txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "txt Files (*.txt)|*.txt||");
	if(myDlg1.DoModal() == IDOK)
	{
		strPath_download_prog = myDlg1.GetPathName();
		PFlag = 1;
	}
	else strPath_download_prog = "";

	if(!(m_DownloadThread_Prog = AfxBeginThread(DownloadThreadProg,this)))
	{
		TRACE0("Warning : Start download thread failed.\n");
		return;
	}

	TRACE("Thread started\n");

	return;	
}

UINT CSCommSenderDlg::DownloadThreadProg(LPVOID pParam)
{
	int PosProg=0;
	if(pParam == NULL)
		return -1;
	CSCommSenderDlg *pDlg = (CSCommSenderDlg *) pParam;

	//if(download_mod==0)
	//{
	//	pDlg->MessageBox("Please choose the transmission mod!");
	//	return 0;
	//}
	

	CFileException ex;
	CFile destFile;
	if(!destFile.Open(pDlg->strPath_download_prog,CFile::modeRead | CFile::shareDenyWrite,&ex))
	{
		TCHAR szError[1024];
		ex.GetErrorMessage(szError,1024);    //retrun the error pointer and the maximum number of the error buffer size
		pDlg->MessageBox(szError,NULL,MB_ICONSTOP | MB_OK);
		return 0;
	}

	DWORD dwLength = destFile.GetLength();  //A count of the bytes in the file
	BYTE *pSizeBuf = (BYTE *)(&dwLength);

		if(download_mod==0)
	{
		unsigned int k;

		CByteArray binData;
		binData.RemoveAll();

		for(k=0;k<sizeof(DWORD);k++)
		binData.Add(pSizeBuf[k]);
		COleVariant var(binData);  
		pDlg->m_ctrlComm.SetOutput(var);    //send length of the file (why not send dwlength directely)
	
		Sleep(50);
		//PosProg = PosProg + 100;
		//pDlg->m_progress_prog.SetPos(PosProg);  //in the download progress

		DWORD dwPortWriteBuffSize = (DWORD)pDlg->m_ctrlComm.GetOutBufferSize();
		BYTE *pSendBuff = new BYTE[dwPortWriteBuffSize];  //create the new memory space
		memset(pSendBuff, 0, dwPortWriteBuffSize);  //set the default value zero

		destFile.SeekToBegin();  //set the value of the file poniter to the begining of the file

		for(DWORD dwI = 0; dwI < dwLength;){
			if((dwLength - dwI) >= dwPortWriteBuffSize){
				destFile.Read(pSendBuff, dwPortWriteBuffSize);
				binData.RemoveAll();
				for(k=0;k<dwPortWriteBuffSize;k++)
				binData.Add(pSendBuff[k]);
				COleVariant var(binData);
				pDlg->m_ctrlComm.SetOutput(var);   //send the file part unit and set the prog   
				dwI += dwPortWriteBuffSize;
				//PosProg = PosProg+(10000/dwLength)*dwPortWriteBuffSize+250;
				//pDlg->m_progress_prog.SetPos(PosProg);
				Sleep(50);
			}
			else{
				destFile.Read(pSendBuff,dwLength - dwI);
				binData.RemoveAll();
				for(k=0;k<dwLength - dwI;k++)
					binData.Add(pSendBuff[k]);
				COleVariant var(binData);
				pDlg->m_ctrlComm.SetOutput(var);  //send the last part of the file
				dwI = dwLength;
				//PosProg = 10000;
				//pDlg->m_progress_prog.SetPos(PosProg);
				Sleep(50);
			}
		}
		destFile.Close();
		delete []pSendBuff;
	
		//PosProg=10000;
		//pDlg->m_progress_prog.SetPos(PosProg);
	}
   return 0;
}


BEGIN_EVENTSINK_MAP(CSCommSenderDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CSCommSenderDlg)
	ON_EVENT(CSCommSenderDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CSCommSenderDlg::OnComm() 
{
	// TODO: Add your control notification handler code here
	VARIANT variant_inp;
		COleSafeArray safearray_inp;
		LONG len,k;
		BYTE rxdata[2048];
		CString strtemp;
		if(m_ctrlComm.GetCommEvent()==2)
		{
			variant_inp=m_ctrlComm.GetInput();
			safearray_inp=variant_inp;
			len=safearray_inp.GetOneDimSize();
			for(k=0;k<len;k++)
				safearray_inp.GetElement(&k,rxdata+k);
			
			for(k=0;k<len;k++)
			{
				BYTE bt = *(unsigned char*)(rxdata+k);
				strtemp.Format("%c",bt);
				if(times==2&&strtemp==36)
				{
				   en_N2=rxdata[k-1];
                   PostMessage(WM_RE_EN_N2); 
				}
				 if(times==2&&strtemp==35)
				{
					en_de_N1=rxdata[k-1];
                    PostMessage(WM_EN_DE_N1);
				}
				m_strEditRXData+=strtemp;
				UpdateData(FALSE);
			}
	       if(m_strEditRXData=="hello")

⌨️ 快捷键说明

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