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

📄 dpjcommview.cpp

📁 这是一个单片机与PC机之间串口通信的程序
💻 CPP
字号:
// DPJCommView.cpp : implementation of the CDPJCommView class
//

#include "stdafx.h"
#include "DPJComm.h"

#include "DPJCommDoc.h"
#include "DPJCommView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDPJCommView
///////////////////进制转换////////////////////
//转换二进制为十进制
CString BinaryToDecimal(CString strBinary)
{
int nLenth = strBinary.GetLength();
char* Binary = new char[nLenth];
Binary = strBinary.GetBuffer(0);
int nDecimal = 0;
for(int i=0;i<nLenth;i++)
{
char h = Binary[nLenth-1-i];
char str[1];
str[0] = h;
int j = atoi(str);
for(int k=0;k<i;k++)
{
j=j*2;
}
nDecimal += j;
}
CString strDecimal;
strDecimal.Format("%d",nDecimal);
return strDecimal;
}

//转换十进制为二进制
CString DecimalToBinary(CString strDecimal)
{
int nDecimal = atoi(strDecimal.GetBuffer(0));
int nYushu; //余数
int nShang; //商
CString strBinary = "";
char buff[2];
CString str = "";
BOOL bContinue = TRUE;
while(bContinue)
{
nYushu = nDecimal%2;
nShang = nDecimal/2;
sprintf(buff,"%d",nYushu);
str = strBinary;
strBinary.Format("%s%s",buff,str);
nDecimal = nShang;
if(nShang==0)
bContinue = FALSE;
}
return strBinary;
}

//转换二进制为十六进制
CString BinaryToHex(CString strBinary)
{
int nLength = strBinary.GetLength();
CString str = strBinary;
//位数不是四的倍数时补齐
switch(nLength%4)
{
case 0:
break;
case 1:
strBinary.Format("%d%d%d%s",0,0,0,str);
break;
case 2:
strBinary.Format("%d%d%s",0,0,str);
break;
case 3:
strBinary.Format("%d%s",0,str);
break;
default:
return "";
break;
}
CString strHex,str1;
str1 = "";
nLength = strBinary.GetLength();
for(int i=1;i<=(nLength/4);i++)
{
//每四位二进制数转换为一十六进制数
str = strBinary.Left(4);
CString strDecimal = BinaryToDecimal(str);
int nDecimal = atoi(strDecimal.GetBuffer(0));
if(nDecimal<10)
str1.Format("%d",nDecimal);
else
{
char c = 'A' + (nDecimal-10);
str1.Format("%c",c);
}
strHex += str1;
strBinary = strBinary.Right(strBinary.GetLength()-str.GetLength());
}
return strHex;
}

/////////////////////////
//******************************
char HexChar(char c)//检测一个字符是不是十六进制字符,若是返回相应的值,否则返回0x10;
{
	if((c>='0')&&(c<='9'))
	return c-0x30;
	else if((c>='A')&&(c<='F'))
	return c-'A'+10;
	else if((c>='a')&&(c<='f'))
	return c-'a'+10;
	else return 0x10;
}
//将一个字符串作为十六进制串转化为一个字节数组,字节间可用空格分隔,返回转换后的字节数组长度,同时字节数组长度自动设置。
int Str2Hex(CString str,CByteArray &data)
{
	int t,t1;
	int rlen=0,len=str.GetLength();
	data.SetSize(len/2);
	for(int i=0;i<len;)
	{char l,h=str[i];
	if(h==' ')
	{i++;
	continue;
	}
	i++;
	if(i>=len)break;
	l=str[i];
	t=HexChar(h);
	t1=HexChar(l);
	if((t==16)||(t1==16))
		break;
	else t=t*16+t1;
	i++;
	data[rlen]=(char)t;
	rlen++;
	}
	data.SetSize(rlen);
	return rlen;
}
//************************************

//字符串转换为整型
int strtoint(char c)
{
	int i;
	if((c>='0')&&(c<='9'))
	 i=c-'0';
	else if((c>='A')&&(c<='F'))
	 i=c-'A'+10;
	else if((c>='a')&&(c<='f'))
	 i=c-'a'+10;
	return i;
}


IMPLEMENT_DYNCREATE(CDPJCommView, CFormView)

BEGIN_MESSAGE_MAP(CDPJCommView, CFormView)
	//{{AFX_MSG_MAP(CDPJCommView)
	ON_BN_CLICKED(IDC_CLEAN, OnClean)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDPJCommView construction/destruction

CDPJCommView::CDPJCommView()
	: CFormView(CDPJCommView::IDD)
{
	//{{AFX_DATA_INIT(CDPJCommView)
	m_fasongqu = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CDPJCommView::~CDPJCommView()
{
}

void CDPJCommView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDPJCommView)
	DDX_Control(pDX, IDC_MSCOMM1, m_comm);
	DDX_Text(pDX, IDC_RECEIVE, m_receive);
	DDX_Text(pDX, IDC_DADENG, m_dadeng);
	DDX_Text(pDX, IDC_DAINCHI, m_dianchi);
	DDX_Text(pDX, IDC_FABAOSI, m_fanbaosi);
	DDX_Text(pDX, IDC_FADONGJI, m_fadongji);
	DDX_Text(pDX, IDC_jiyou, m_jiyou);
	DDX_Text(pDX, IDC_QINANG, m_qinang);
	DDX_Text(pDX, IDC_SACHE, m_shache);
	DDX_Text(pDX, IDC_shacheshiling, m_shacheshiling);
	DDX_Text(pDX, IDC_SHUIWENDENG, m_shuiwendeng);
	DDX_Text(pDX, IDC_YOUDENG, m_youdeng);
	DDX_Text(pDX, IDC_tingchezhudong, m_tingchezhidong);
	DDX_Text(pDX, IDC_ZUODENG, m_zuodeng);
	DDX_Text(pDX, IDC_SHUIWEN, m_nshuiwen);
	DDX_Text(pDX, IDC_CHESU, m_nchesu);
	DDX_Text(pDX, IDC_youwei, m_nyouwei);
	DDX_Text(pDX, IDC_ZHUANSU, m_nzhuansu);
	DDX_Text(pDX, IDC_SENDqu, m_fasongqu);
	//}}AFX_DATA_MAP
}

BOOL CDPJCommView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CDPJCommView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	////////////////
	m_dadeng="0";
	m_dianchi="0";
	m_fanbaosi="0";
	m_fadongji="0";
	m_jiyou="0";
	m_qinang="0";
	m_shache="0";
	m_shacheshiling="0";
	m_shuiwendeng="0";
	m_youdeng="0";
	m_tingchezhidong="0";
	m_zuodeng="0";
	m_nshuiwen="50";
	m_nchesu="70";
	m_nyouwei="40";
	m_nzhuansu="7500";
	 UpdateData(false );
	 SetTimer(1,1200,NULL);
    //communcation
	m_comm.SetCommPort(2);
	if(!m_comm.GetPortOpen())
		m_comm.SetPortOpen(true);
	m_comm.SetSettings("4800,n,8,1");
	m_comm.SetInputMode(1);
	m_comm.SetInputLen(0);
	m_comm.GetInput();
	m_comm.SetRThreshold(15);

}

/////////////////////////////////////////////////////////////////////////////
// CDPJCommView printing

BOOL CDPJCommView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CDPJCommView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CDPJCommView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CDPJCommView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CDPJCommView diagnostics

#ifdef _DEBUG
void CDPJCommView::AssertValid() const
{
	CFormView::AssertValid();
}

void CDPJCommView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CDPJCommDoc* CDPJCommView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDPJCommDoc)));
	return (CDPJCommDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDPJCommView message handlers

void CDPJCommView::OnClean() 
{   m_fasongqu.Empty();//清空数据
	m_receive.Empty();//清空数据
	UpdateData( false);
}

void CDPJCommView::OnSend() 
{
   
	//m_receive=m_nchesu+"/"+m_nzhuansu+"/"+m_nshuiwen+"/"+m_nyouwei+"/"+m_zuodeng+"/"+m_dianchi+"/"+m_shacheshiling+"/"+m_dadeng+"/"+m_tingchezhidong+"/"+m_shache+"/"+m_youdeng+"/"+m_shuiwendeng+"/"+m_jiyou+"/"+m_fadongji+"/"+m_fanbaosi+"/"+m_qinang;
//	UpdateData( true);
	ShangchuanDanpianji1Data();
	//m_comm.SetOutput(COleVariant(m_receive));//发送数据
}

BEGIN_EVENTSINK_MAP(CDPJCommView, CFormView)
    //{{AFX_EVENTSINK_MAP(CDPJCommView)
	ON_EVENT(CDPJCommView, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CDPJCommView::OnComm() 
{
	// TODO: Add your control notification handler code here
	VARIANT m_input1;
	COleSafeArray m_input2;
	long length,i;
	CString str;
	m_receive.Empty();//清空数据
	UpdateData(false);
	if(m_comm.GetCommEvent()==2)//接收区中有数据
	{
		m_input1=m_comm.GetInput();//读取数据
		m_input2=m_input1;//将VARIANT 型变量转换为COleSafeArray型数据
		length=m_input2.GetOneDimSize();//确定数据长度
		for(i=0;i<length;i++)
		{
			m_input2.GetElement(&i,data+i);
			BYTE a=*(char*)(data+i);
			str.Format("%02x",a);
			m_receive+=str;
		}
		UpdateData(false);
		m_comm.SetInputLen(0);//清空接收区的数据等待下组数据
		//下传数据
	//	if(m_receive.Left(8)=="ffa5aa00")
	//	{
		//	Sleep(200);
		  if(m_receive.Left(8)=="ffccaa01")
		{
			TRACE("hello");
			 // Sleep(1000);
			  ShangchuanDanpianji1Data();//上传单片机1数据
		}
	//	}

		UpdateData(false);
	}

}

void CDPJCommView::ShangchuanDanpianji1Data()
{
    UpdateData(true );

	/////////////////////////////////
	CByteArray senddata;
	senddata.SetSize(15);
	CString shangchuandate;	
	shangchuandate="ff3301aa0f";
	int length=Str2Hex(shangchuandate,senddata);
	////////数字量///////////////
	CString	 shuziliangl8,shuziliangh4;
	shuziliangl8=BinaryToHex(m_zuodeng+m_dianchi+m_shacheshiling+m_dadeng+m_tingchezhidong+m_shache+m_youdeng+m_shuiwendeng);
	shuziliangh4=BinaryToHex("0000"+m_jiyou+m_fadongji+m_fanbaosi+m_qinang);
	shangchuandate+=shuziliangl8;
	shangchuandate+=shuziliangh4;
   	int length1=Str2Hex(shangchuandate,senddata);
	/////////模拟量////////
    int lenDB,j;
	CString chesu=DecimalToBinary(m_nchesu);
	lenDB=chesu.GetLength();
	TRACE("hhhh%d",lenDB);
	//补足空余数据位
	for( j=0;j<16-lenDB;j++)
	{
		chesu="0"+chesu;
	}
    CString m_chesu=BinaryToHex(chesu);
	////////////////////////
    CString zhuansu=DecimalToBinary(m_nzhuansu);
	lenDB=zhuansu.GetLength();
	//补足空余数据位
	for(j=0;j<16-lenDB;j++)
	{
		zhuansu="0"+zhuansu;
	}
    CString m_zhuansu=BinaryToHex(zhuansu);
	////////////////////
	CString shuiwen=DecimalToBinary(m_nshuiwen);
    lenDB=shuiwen.GetLength();
	//补足空余数据位
	for( j=0;j<16-lenDB;j++)
	{
		shuiwen="0"+shuiwen;
	}
    CString m_shuiwen=BinaryToHex(shuiwen);
	////////////////
   	CString youwei=DecimalToBinary(m_nyouwei);
	lenDB=youwei.GetLength();
	//补足空余数据位
	for( j=0;j<16-lenDB;j++)
	{
	   youwei="0"+youwei;
	}
    CString m_youwei=BinaryToHex(youwei);
	/////////////////////////////////////
    shangchuandate=shangchuandate+m_chesu+m_zhuansu+m_shuiwen+m_youwei;
	//shangchuandate="ff3301aa0f00010000000000000000";////////////
	int sendlength=Str2Hex(shangchuandate,senddata);

	m_comm.SetOutput(COleVariant(senddata));
	m_fasongqu=shangchuandate;
	UpdateData( false);
	}


void CDPJCommView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	//ShangchuanDanpianji1Data();//上传单片机1数据
	CFormView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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