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

📄 recorderconsoleview.cpp

📁 汽车行驶记录仪串口的数据采集、设置、通讯功能
💻 CPP
字号:
// RecorderConsoleView.cpp : implementation of the CRecorderConsoleView class
//

#include "stdafx.h"
#include "RecorderConsole.h"

#include "RecorderConsoleDoc.h"
#include "RecorderConsoleView.h"

char ConvertHexChar(char ch);
int String2Hex(CString str, BYTE * pByte);

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

/////////////////////////////////////////////////////////////////////////////
// CRecorderConsoleView

IMPLEMENT_DYNCREATE(CRecorderConsoleView, CFormView)

BEGIN_MESSAGE_MAP(CRecorderConsoleView, CFormView)
	//{{AFX_MSG_MAP(CRecorderConsoleView)
	ON_BN_CLICKED(IDC_TEST, OnTest)
	ON_MESSAGE(WM_COMMNOTIFY,OnComm)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRecorderConsoleView construction/destruction

CRecorderConsoleView::CRecorderConsoleView()
	: CFormView(CRecorderConsoleView::IDD)
{
	//{{AFX_DATA_INIT(CRecorderConsoleView)
	m_iType = -1;
	m_strTime = _T("");
	m_iCFL = 0;
	m_iCFM = 0;
	m_iCFH = 0;
	m_strConductorID = _T("");
	m_strPassportCode = _T("");
	m_strVIN = _T("");
	m_strCarCode = _T("");
	m_strCarType = _T("");
	m_strReceiveData = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	curTime=CTime::GetCurrentTime();
	m_strTime.Format("%2d:%2d:%2d %2d-%2d-%2d",
			curTime.GetHour(),curTime.GetMinute(),curTime.GetSecond(),
			curTime.GetYear()%100,curTime.GetMonth(),curTime.GetDay());

	for(int i=0;i<m_strTime.GetLength()-2;i+=3)
	{
		if(m_strTime[i]==' ') m_strTime.SetAt(i,'0');
	}
	
}

CRecorderConsoleView::~CRecorderConsoleView()
{
}

void CRecorderConsoleView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRecorderConsoleView)
	DDX_Radio(pDX, IDC_TYPE, m_iType);
	DDX_Text(pDX, IDC_EDIT1, m_strTime);
	DDX_Text(pDX, IDC_EDIT2, m_iCFL);
	DDX_Text(pDX, IDC_EDIT9, m_iCFM);
	DDX_Text(pDX, IDC_EDIT10, m_iCFH);
	DDX_Text(pDX, IDC_EDIT3, m_strConductorID);
	DDX_Text(pDX, IDC_EDIT4, m_strPassportCode);
	DDX_Text(pDX, IDC_EDIT5, m_strVIN);
	DDX_Text(pDX, IDC_EDIT6, m_strCarCode);
	DDX_Text(pDX, IDC_EDIT7, m_strCarType);
	DDX_Text(pDX, IDC_EDIT8, m_strReceiveData);
	//}}AFX_DATA_MAP
}

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

	return CFormView::PreCreateWindow(cs);
}

void CRecorderConsoleView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CRecorderConsoleView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CRecorderConsoleView message handlers

void CRecorderConsoleView::OnTest() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CRecorderConsoleDoc * pDoc=GetDocument();
	if(!pDoc->m_bConnected)
	{
		AfxMessageBox("串口没有连接");
		return;
	}
	if(m_iType==-1)
		return;
	switch(m_iType)
	{
	case 0:
		SetRecorderTime();
		break;
	case 1:
		SetCarFeature();
		break;
	case 2:
		SetConductorInfor();
		break;
	case 3:
		SetCarInfor();
		break;
	case 4:
		GetConductorInfor();
		break;
	case 5:
		GetRecorderTime();
		break;
	case 6:
		Get_360H_Mileage();
		break;
	case 7:
		GetCarFeature();
		break;
	case 8:
		Get_360H_SpeedData();
		break;
	case 9:
		GetCarInfor();
		break;
	case 10:
		GetDangerData();
		break;
	case 11:
		Get_2Days_Mileage();
		break;
	case 12:
		Get_2Days_SpeedData();
		break;
	case 13:
		GetFatigueRecord();
		break;
	default:
		break;
	}
}

void CRecorderConsoleView::SetRecorderTime()
{
	int iLen=m_strTime.GetLength();
	BYTE * pByte =  new BYTE[iLen+7];
	CRecorderConsoleDoc * pDoc=GetDocument();
	CString str="AA 75 C2 07 00 00";
	int iTotal = String2Hex(str,pByte);
	str=m_strTime;
	for(int i=0;i<(iLen+1)/3;i++)
		pByte[iTotal+i]=atoi(m_strTime.Left(i*3+2).Right(2));
	pByte[iLen+6]=00;
	pDoc->WriteComm(pByte,iLen+7);
	delete pByte;
}

void CRecorderConsoleView::SetCarFeature()
{	
	int iLen=m_strConductorID.GetLength()+m_strPassportCode.GetLength();
	BYTE * pByte =  new BYTE[10];
	CRecorderConsoleDoc * pDoc=GetDocument();
	CString str="AA 75 C3 04 00 00";
	int iTotal = String2Hex(str,pByte);
	pByte[iTotal]=m_iCFL;
	pByte[iTotal+1]=m_iCFM;
	pByte[iTotal+2]=m_iCFH;
	pByte[iTotal+3]=00;
	pDoc->WriteComm(pByte,10);
	delete pByte;
}

void CRecorderConsoleView::SetConductorInfor()
{
	BYTE * pByte =  new BYTE[28];
	CRecorderConsoleDoc * pDoc=GetDocument();
	CString str="AA 75 81 15 00 00";
	int iTotal = String2Hex(str,pByte);
	int tmpId=atoi(m_strConductorID);
	pByte[iTotal]=tmpId/65536;
	pByte[iTotal+1]=tmpId%65536/256;
	pByte[iTotal+2]=tmpId%256;
	for(int i=0;i<m_strPassportCode.GetLength();i++)
		pByte[iTotal+2+i]=m_strPassportCode.GetAt(i);
	pByte[iTotal+3]=00;
	pDoc->WriteComm(pByte,28);
	delete pByte;
}

void CRecorderConsoleView::SetCarInfor()
{
	CString strTotal=m_strVIN+m_strCarCode+m_strCarType;
	int iLen=strTotal.GetLength();
	BYTE * pByte =  new BYTE[iLen+7];
	CRecorderConsoleDoc * pDoc=GetDocument();
	CString str="AA 75 82 30 00 00";
	int iTotal = String2Hex(str,pByte);
	for(int i=0;i<str.GetLength();i++)
		pByte[iTotal+i]=strTotal.GetAt(i);
	pByte[iLen+7]=00;
	pDoc->WriteComm(pByte,iLen+7);
	delete pByte;
}

void CRecorderConsoleView::GetConductorInfor()
{
	SendCommondPacket(0x01);
}

void CRecorderConsoleView::GetRecorderTime()
{
	SendCommondPacket(0x01);	
}

void CRecorderConsoleView::Get_360H_Mileage()
{
	SendCommondPacket(0x01);
}

void CRecorderConsoleView::Get_360H_SpeedData()
{
	SendCommondPacket(0x01);
}

void CRecorderConsoleView::Get_2Days_Mileage()
{
	SendCommondPacket(0x01);
}

void CRecorderConsoleView::Get_2Days_SpeedData()
{
	SendCommondPacket(0x01);
}

void CRecorderConsoleView::GetCarFeature()
{
	SendCommondPacket(0x01);
}

void CRecorderConsoleView::GetFatigueRecord()
{
	SendCommondPacket(0x01);
}

void CRecorderConsoleView::GetDangerData()
{
	SendCommondPacket(0x01);
}

void CRecorderConsoleView::GetCarInfor()
{
	SendCommondPacket(0x01);
}
LRESULT CRecorderConsoleView::OnComm(WPARAM wParam,LPARAM lParam)
{
	BYTE pByte[MAXBLOCK/4];
	CString str;
	int nLength;
	CRecorderConsoleDoc * pDoc=GetDocument();
	//CEdit& edit=GetEditCtrl();
	if(!pDoc->m_bConnected||(wParam&EV_RXCHAR)!=EV_RXCHAR)
	{
		SetEvent(pDoc->m_hPostMsgEvent);
		return 0L;
	}
	nLength=pDoc->ReadComm(pByte,100);
	int i;
	if(CompleteFlag)
	{
		if(pByte[0]=0x0AA)
		{
			m_Packet.Header[0]=0x0AA;
			delete m_Packet.pContent;
			m_Packet.pContent=new BYTE[nLength];
			CompleteFlag=FALSE;
			for(i=0;i<nLength;i++)
				m_Packet.pContent[i]=pByte[i];
			m_Packet.tmpLength=nLength;
		}
	}
	else
	{
		BYTE * pTmp=new BYTE[nLength+m_Packet.tmpLength];
		for(int i=0;i<m_Packet.tmpLength;i++)
		{
			pTmp[i]=m_Packet.pContent[i];
		}
		for(i=0;i<nLength;i++)
		{
			pTmp[i+m_Packet.tmpLength]=pByte[i];
		}
		delete m_Packet.pContent;
		m_Packet.pContent=pTmp;
		m_Packet.tmpLength+=nLength;
		if(m_Packet.pContent[1]==0x75)
		{
			if(m_Packet.tmpLength>=5)
				m_Packet.iLen=m_Packet.pContent[4]+m_Packet.pContent[4]*256;
			if(m_Packet.tmpLength>=m_Packet.iLen)
			{
				CompleteFlag=TRUE;
				m_Packet.bValid=TRUE;
			}
		}
		else
		{
			CompleteFlag=TRUE;
			m_Packet.bValid=FALSE;
			m_Packet.tmpLength=7;
			m_Packet.iLen=5;
		}
	}
	if(m_Packet.bValid)
	{
		m_strReceiveData="";
		CString strTmp;
		for(i=0;i<m_Packet.tmpLength;i++)
		{
			strTmp.Format("%2d",m_Packet.pContent[i]);
			strReceiveData+=strTmp;
		}
		UpdateData(FALSE);
	}
	SetEvent(pDoc->m_hPostMsgEvent);
	return 0L;
}

int String2Hex(CString str, BYTE * pByte)
{
	int hexdata,lowhexdata;
	int hexdatalen=0;
	int len=str.GetLength();
	for(int i=0;i<len;)
	{
		char lstr,hstr=str[i];
		if(hstr==' ')
		{
			i++;
			continue;
		}
		i++;
		if(i>=len)
			break;
		lstr=str[i];
		hexdata=ConvertHexChar(hstr);
		lowhexdata=ConvertHexChar(lstr);
		if((hexdata==16)||(lowhexdata==16))
			break;
		else 
			hexdata=hexdata*16+lowhexdata;
		i++;
		pByte[hexdatalen]=(char)hexdata;
		hexdatalen++;
	}
	return hexdatalen;

}

char ConvertHexChar(char ch)
{
	if((ch>='0')&&(ch<='9'))
		return ch-0x30;
	else if((ch>='A')&&(ch<='F'))
			return ch-'A'+10;
		 else if((ch>='a')&&(ch<='f'))
				return ch-'a'+10;
			  else return (-1);

}

void CRecorderConsoleView::SendCommondPacket(BYTE cmd)
{
	CString str="AA 75 00 00 00 00 00";
	CRecorderConsoleDoc * pDoc=GetDocument();
	BYTE * pByte=new BYTE[8];
	int iTotal = String2Hex(str,pByte);
	pByte[2]=cmd;
	pDoc->WriteComm(pByte,8);
	delete pByte;
}

⌨️ 快捷键说明

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