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

📄 rssianddatadlg.cpp

📁 是一个关于快速排序的算法的源代码...很有用的
💻 CPP
字号:
// RSSIAndDataDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RSSIAndData.h"
#include "RSSIAndDataDlg.h"
#include "Math.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()

/////////////////////////////////////////////////////////////////////////////
// CRSSIAndDataDlg dialog

CRSSIAndDataDlg::CRSSIAndDataDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRSSIAndDataDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRSSIAndDataDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	///////////////
	for(int i = 0;i<20;i++)
		pRxBuf[i] = 0;
}

void CRSSIAndDataDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRSSIAndDataDlg)
	DDX_Control(pDX, IDC_EDIT_PTrans, m_ptrans);
	DDX_Control(pDX, IDC_EDIT_AntGain, m_gain);
	DDX_Control(pDX, IDC_EDIT_K, m_k);
	DDX_Control(pDX, IDC_EDIT_F, m_f);
	DDX_Control(pDX, IDC_EDIT_RefLoss, m_refloss);
	DDX_Control(pDX, IDC_EDIT_RefDIST, m_refdist);
	DDX_Control(pDX, IDC_EDIT_POWR, m_powr);
	DDX_Control(pDX, IDC_EDIT_PLd, m_pld);
	DDX_Control(pDX, IDC_EDIT_DIST, m_dist);
	DDX_Control(pDX, IDC_EDIT_ADC, m_adc);
	DDX_Control(pDX, IDC_MSCOMM1, m_MSComm);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRSSIAndDataDlg, CDialog)
	//{{AFX_MSG_MAP(CRSSIAndDataDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BCACULATE, OnBcaculate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRSSIAndDataDlg message handlers

BOOL CRSSIAndDataDlg::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
	cnt=0;
	///////////////////MSComm Init////////////////
	if(m_MSComm.GetPortOpen()) //如果串口是打开的,则行关闭串口
	m_MSComm.SetPortOpen(FALSE);

	m_MSComm.SetCommPort(1); //选择COM1
	m_MSComm.SetInBufferSize(1024); //接收缓冲区
	m_MSComm.SetOutBufferSize(1024);//发送缓冲区
	m_MSComm.SetInputLen(0);//设置当前接收区数据长度为0,表示全部读取
	m_MSComm.SetInputMode(1);//以二进制方式读写数据
	m_MSComm.SetRThreshold(1);//接收缓冲区有1个及1个以上字符时,将引发接收数据的OnComm事件
	m_MSComm.SetSettings("57600,n,8,1");//波特率9600无检验位,8个数据位,1个停止位
	
	RxState = RXSTATE_NOSYNC;			//接收状态初始化
	RxByteCnt = 0;						//接收字节计数值置0
	RxRunningCRC = 0;					//CRC计算值置0

	if(!m_MSComm.GetPortOpen())//如果串口没有打开则打开
	m_MSComm.SetPortOpen(TRUE);//打开串口
	else
	AfxMessageBox("Open Serial Port Failure!");
	m_MSComm.GetInput(); //先预读缓冲区以清除残留数据
	//////////////////////////////////////////////

	m_wndChart.Create(WS_VISIBLE|GS_NOTIFY,CRect(20,210,580,610),this,IDR_CHART);
	m_wndChart.AddLine(1, RGB(141,238,238), LS_POINT | LS_HORZAUTO );
	m_wndChart.SetAutoinc(1,60);//设置两点之间的间隔
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CRSSIAndDataDlg::OnBcaculate() 
{
	// TODO: Add your control notification handler code here
	
	CString Cadc,Cpowr,Cpld,Cpowt,Cgain;
	float fadc,fpowr,fvrssi,fpowt,fgain,fpld;
	
	m_adc.GetWindowText(Cadc);
	fadc = atof(Cadc);

	fvrssi = fadc * 1.2 / 1024;//1.2 Vref
	fpowr = -51.3 * fvrssi - 49.2; //at 433MHZ recieve power
	
	Cpowr.Format("%f",fpowr);
	m_powr.SetWindowText(Cpowr);//show power value	

	
	//fpowt = 40;
	//fgain = 20;//assume radio transmit power and antenna gain 40 and 20
	m_ptrans.GetWindowText(Cpowt);
	fpowt = atof(Cpowt);
	m_gain.GetWindowText(Cgain);
	fgain = atof(Cgain);

	fpld = fpowt + fgain - fpowr;//经过距离d后的损耗
	
	Cpld.Format("%f",fpld);
	m_pld.SetWindowText(Cpld);//show the loss
	
	//free modal by to caculated the reference distance
	
	float refdist,refloss,f;
	int k;
	CString Crefd,CF,CK,Crefloss;

	m_refdist.GetWindowText(Crefd);
	m_f.GetWindowText(CF);
	m_k.GetWindowText(CK);

	refdist = atof(Crefd);
	f = atof(CF);
	k = atof(CK);
	
	refloss = 32.44 + 10 * k * log10(refdist) + 10 * k * log10(f); 
	
	/*int i=log10(10);
	CString test;
	test.Format("%d",i);
	AfxMessageBox(test);*/

	Crefloss.Format("%f",refloss);
	m_refloss.SetWindowText(Crefloss);
	//////////////////////////////////////////////////
	//////////real distance
	float d;
	CString Cd;
	//d = pow(2.3,5.666);
	//Cd.Format("%f",d);
	//AfxMessageBox(Cd);
	d = pow(10,((fpld - refloss)/(10*k) )) * refdist;
	Cd.Format("%f",d);
	m_dist.SetWindowText(Cd);
}

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

void CRSSIAndDataDlg::OnMscomm() 
{
	// TODO: Add your control notification handler code here
	//变量声明
	VARIANT variant_input;				//临时变量用于取出缓存中的VARIANT类型数据
	COleSafeArray safearray_input;		//临时变量用于变量转换之用
	short Count;
	Count=m_MSComm.GetInBufferCount();
	BYTE rxdata[100];						//BYTE型数据
	unsigned char data;

	//处理函数	
	if (m_MSComm.GetCommEvent()==2)
	{
		variant_input = m_MSComm.GetInput();			//如果接收缓存区内有字符就读取
		safearray_input = variant_input;			//将VARIANT型变量转换为COleSafeArray型		
		for (long i=0;i<Count;i++)
		{
			safearray_input.GetElement(&i,rxdata+i);			//将数据装换为BYTE型数据
			data = (unsigned char)rxdata[i];
			//CString strtmp;
			//strtmp.Format("%02X ",data);
			//m_Receive += strtmp;
			//m_Receive += "\r\n";
			//UpdateData(FALSE);
			switch (RxState)
			{
			case RXSTATE_NOSYNC:
				if (data == HDLC_FLAG_BYTE) {
					RxByteCnt = 0;
					RxRunningCRC = 0;
					RxState = RXSTATE_PROTO;
				}
				break;
				
			case RXSTATE_PROTO:
				if (data == HDLC_FLAG_BYTE) {
					RxByteCnt = 0;
					RxRunningCRC = 0;
					break;
				}
				RxRunningCRC = crcByte(RxRunningCRC,data);
				RxState = RXSTATE_INFO;
				break;
				
			case RXSTATE_INFO:
				if (data == HDLC_CTLESC_BYTE) {
					RxState = RXSTATE_ESC;
				}
				else if (data == HDLC_FLAG_BYTE) {
					if (RxByteCnt >= 2) {
						usRcvdCRC = (pRxBuf[(RxByteCnt-1)] & 0xff);
						usRcvdCRC = (usRcvdCRC << 8) | (pRxBuf[(RxByteCnt-2)] & 0xff);
						if (usRcvdCRC == RxRunningCRC)
						{
							ProceedMsg();//处理函数	
						}
						else
						{
							RxByteCnt = 0;
							RxRunningCRC = 0;
							RxState = RXSTATE_PROTO;
						}
					}
				}
				else {
					pRxBuf[RxByteCnt] = data;
					if (RxByteCnt >= 2) {
						RxRunningCRC = crcByte(RxRunningCRC,pRxBuf[(RxByteCnt-2)]);
					}
					RxByteCnt++;
				}
				break;
				
			case RXSTATE_ESC:
				if (data == HDLC_FLAG_BYTE) {
					RxByteCnt = 0;
					RxRunningCRC = 0;
					RxState = RXSTATE_NOSYNC;
				}
				else {
					data = data ^ 0x20;
					pRxBuf[RxByteCnt] = data;
					if (RxByteCnt >= 2) {
						RxRunningCRC = crcByte(RxRunningCRC,pRxBuf[(RxByteCnt-2)]);
					}
					RxByteCnt++;
					RxState = RXSTATE_INFO;
				}
				break;
				
			default:
				RxState = RXSTATE_NOSYNC;
				break;
			}
		}
	}
}


unsigned short int CRSSIAndDataDlg::crcByte(unsigned short crc, unsigned char b)
{
	int i;
	crc = crc ^ b << 8;
	i = 8;
	do
	{
		if (crc & 0x8000)
			crc = crc << 1 ^ 0x1021;
		else
			crc = crc << 1;
	}
	while (--i);
	return crc;
}

void CRSSIAndDataDlg::ProceedMsg()
{
	unsigned short int Adc;
	Adc = pRxBuf[5] + pRxBuf[6]*255;


	m_wndChart.AddData(1,Adc);
	//////////////////////////////
	SYSTEMTIME st;
	CString strDate,strTime;
	GetLocalTime(&st);
	//unsigned short int ADC;
	//ADC = pRxBuf[(RxByteCnt-4)] + (pRxBuf[(RxByteCnt-3)] * 255);
	
	//CString str_explain = "        \r\n 当前年月     当前具体时间     消息发送的次数\r\n";
	CString str_explain = "";
	CString str_parameter="";
	str_parameter.Format("\r\n%4d-%2d-%2d      %2d:%2d:%2d            %d\r\n",\
		st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond,Adc);
	CString str_save_parameter = "";
	
	str_save_parameter = str_explain + str_parameter;
	CString str_save_path = "D:\\RSSIvsADC.txt";
	CStdioFile file(str_save_path,CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate);
	file.SeekToEnd();
	file.WriteString(str_save_parameter);
	file.Close();
	//RxState = RXSTATE_NOSYNC;


}

⌨️ 快捷键说明

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