maintainview.cpp

来自「串口收发工具」· C++ 代码 · 共 1,199 行 · 第 1/2 页

CPP
1,199
字号
// maintainView.cpp : implementation of the CMaintainView class
//

#include "stdafx.h"
#include "maintain.h"
#include "mainfrm.h"
#include "maintainDoc.h"
#include "maintainView.h"
#include "manager.h"
#include "dlgsenddataedit1.h"
//#include "GroupStation.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern int nCurWorkStatus;
CWordArray m_ycArray,m_0250ycArray,m_jdycArray,m_jdxjMuxianArray,m_jdxjZhiluArray;
/*由于遥信上传的最后两个字节段(12字节)是告警和电池状态,所以在m_yxArray中首先放置5个字节*/
//BYTE1-电池状态 BYTE2-告警bit7-bit0 BYTE3告警bit15-bit8 BYTE4告警bit23-bit16 BYTE5告警bit31-bit24 
CByteArray m_yxArray;
extern bool bShowData;
HANDLE hEvent;
int ngyType=GY_DF0420;//规约类型 1-fdk 2-cdt 3-df0410和df0241 4-modbus 5-短消息模块
bool bSmsFlag = false;//短消息模块标志,不作为单独规约处理,本标志置位时收到数据按短消息规约处理
/************************************************************************/
/*0420和0241规约基本一致,只是查询信息返回时标识遥信个数的位置有所不同。*/
/*0420规约此位置意义为遥信个数而0241规约此位置的意义为遥信字节数。所以增*/
/*加此变量在规约类型都是3时区分是0420规约还是0241规约					*/
/************************************************************************/
int ngyTypeFlag = GY_TYPE_FLAG_DF0421; //1-0420 2-0241
static int nHead=0;
static int nHeadCount=0;
/////////////////////////////////////////////////////////////////////////////
// CMaintainView

IMPLEMENT_DYNCREATE(CMaintainView, CView)

BEGIN_MESSAGE_MAP(CMaintainView, CView)
	//{{AFX_MSG_MAP(CMaintainView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_MESSAGE(WM_COMMNOTIFY,OnGetCommMessage)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMaintainView construction/destruction

CMaintainView::CMaintainView()
{
	// TODO: add construction code here
		dlgYkYt = NULL;
	nDataLen = 0;
	ngyType = GY_DF0420;
	ngyTypeFlag=GY_TYPE_FLAG_DF0421;
	nGroup = 1;
	nAddress = 1;
	dlgUpDownLoad = NULL;
}

CMaintainView::~CMaintainView()
{
	m_ycArray.RemoveAll();
	m_0250ycArray.RemoveAll();
	m_jdycArray.RemoveAll();
	m_yxArray.RemoveAll();
	m_jdxjMuxianArray.RemoveAll();
	m_jdxjZhiluArray.RemoveAll();
	if(dlgYkYt)
	{
		delete dlgYkYt;
		dlgYkYt = NULL;
	}
	if (dlgUpDownLoad) {
		delete dlgUpDownLoad;
		dlgUpDownLoad = NULL;
	}
	
	if (hEvent)
		CloseHandle(hEvent);
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMaintainView drawing

void CMaintainView::OnDraw(CDC* pDC)
{
	CMaintainDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMaintainView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMaintainView diagnostics

#ifdef _DEBUG
void CMaintainView::AssertValid() const
{
	CView::AssertValid();
}

void CMaintainView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CMaintainView message handlers

int CMaintainView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_sendView = new CDataShowView();
	m_sendView->Create(NULL,NULL,WS_CHILD|WS_VISIBLE,
		CRect(0,0,0,0),this,IDW_SENDDATAEDIT);	
	hEvent = CreateEvent(NULL, FALSE, FALSE, "缓冲区");
	SetEvent(hEvent);
	
	hThread = NULL;
	SetTimer(1,20,NULL);
	return 0;
}

void CMaintainView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	CRect rc;
	GetClientRect(rc);
	m_sendView->SetWindowPos(&wndTop,0,0,rc.Width(),rc.Height(),SWP_SHOWWINDOW);
}

void CMaintainView::ShowData(BYTE *pData, int nLen)
{
	m_sendView->ShowData(pData,nLen);
}
//接收到的数据放入缓冲区
int CMaintainView::AddRecDataToBuf(BYTE *m_data,int Length)
{
	WaitForSingleObject(hEvent, INFINITE);
	if(MAX_BUF_LEN <= (nDataLen+Length))
	{
		//memmove(byRecBuf,&byRecBuf[],);
		ZeroMemory(byRecBuf,MAX_BUF_LEN);
		nDataLen = 0;
		nPtr = 0;
	}
	memcpy(&byRecBuf[nDataLen],m_data,Length);
	nDataLen += Length;
	
	SetEvent(hEvent);
	return nDataLen;
}
//中止发送FDK和0420的一般查询
void CMaintainView::StopSend()
{
	KillTimer(2);
	ClearArray();
}
void CMaintainView::ClearArray()
{
	m_ycArray.RemoveAll();
	m_0250ycArray.RemoveAll();
	m_jdycArray.RemoveAll();
	m_yxArray.RemoveAll();
	m_jdxjMuxianArray.RemoveAll();
	m_jdxjZhiluArray.RemoveAll();

	((CMainFrame*)AfxGetMainWnd())->ClearList();
	
}
//组号站号修改后传到规约组帧类中
void CMaintainView::SetGroupAddress(int group, int address)
{
	nGroup = group;
	nAddress = address;
	m_buildData.SetGroupAddrNo(group,address);
}

void CMaintainView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
		CString str,strTemp;

	if((nIDEvent == 2)&&(ngyType !=GY_CDT))//CDT规约时不发送召唤
	{
		nLen = m_buildData.BuildDataFrame(QUERY_YCYXSOE,sendBuf);
		if(!GetDocument()->SendData(sendBuf,nLen,true))
			KillTimer(2);
	}

	if(nIDEvent == 1)
		ProcessData();
	CView::OnTimer(nIDEvent);
}

void CMaintainView::ProcessData()
{	
	CString str,strTemp;
	
	if(nDataLen <= 0)
		return;
	
	if(bSmsFlag)//SMS
	{
		WaitForSingleObject(hEvent, INFINITE);
		for(int i=0;i<nDataLen;i++)
		{
			if(byRecBuf[i]==SOH)
			{
				if ((byRecBuf[i+1]==0x00)&&(byRecBuf[i+2]==0x00)&&(byRecBuf[i+3]==0x06)&&(byRecBuf[i+4]==0x00)) {
					WORD nMsgLen = MAKEWORD(byRecBuf[i+1],byRecBuf[i+2]);
					if((nDataLen-i)>=(nMsgLen+8))
					{
						WORD parity = CManager::Crc16Parity(&byRecBuf[i+1],5+nMsgLen);
						if((LOBYTE(parity) == byRecBuf[i+6+nMsgLen])&&
							(HIBYTE(parity) == byRecBuf[i+7+nMsgLen]))
						{
							ZeroMemory(recBuf,BUF_LEN);
							memcpy(recBuf,&byRecBuf[i+6],nMsgLen);
							ExplainSMS(recBuf,8);
							memmove(byRecBuf,&(byRecBuf[i+8+nMsgLen]),nDataLen-i-8-nMsgLen);
							nDataLen = nDataLen-i-8-nMsgLen;
							SetEvent(hEvent);
						}
					}
				}
				if((byRecBuf[i+3]==0x10)&&(byRecBuf[i+4]==0x05))
				{
					WORD nMsgLen = MAKEWORD(byRecBuf[i+1],byRecBuf[i+2]);
					if((nDataLen-i)>=(nMsgLen+8))
					{
						WORD parity = CManager::Crc16Parity(&byRecBuf[i+1],5+nMsgLen);
						if((LOBYTE(parity) == byRecBuf[i+6+nMsgLen])&&
							(HIBYTE(parity) == byRecBuf[i+7+nMsgLen]))
						{
							ZeroMemory(recBuf,BUF_LEN);
							memcpy(recBuf,&byRecBuf[i+6],nMsgLen);
							ExplainSMS(recBuf,nMsgLen);
							memmove(byRecBuf,&(byRecBuf[i+8+nMsgLen]),nDataLen-i-8-nMsgLen);
							nDataLen = nDataLen-i-8-nMsgLen;
							SetEvent(hEvent);
						}
					}
				}
			}
		}
		SetEvent(hEvent);
		return;
	}

	if(ngyType == GY_FDK)//fdk
	{
		WaitForSingleObject(hEvent, INFINITE);
		for(int i=0;i<nDataLen;i++)
		{
			if(byRecBuf[i]==SOH)
			{
				if((byRecBuf[i+3]==0x02)||
					(byRecBuf[i+3]==0x03)||
					(byRecBuf[i+3]==0x05)||
					(byRecBuf[i+3]==0x06)||
					(byRecBuf[i+3]==0x08))
				{
					
					WORD nMsgLen = MAKEWORD(byRecBuf[i+1],byRecBuf[i+2]);
					if((nDataLen-i)>=(nMsgLen+8))
					{
						WORD parity = CManager::Crc16Parity(&byRecBuf[i+1],5+nMsgLen);
						if((LOBYTE(parity) == byRecBuf[i+6+nMsgLen])&&
							(HIBYTE(parity) == byRecBuf[i+7+nMsgLen]))
						{
							ZeroMemory(recBuf,BUF_LEN);
							memcpy(recBuf,&byRecBuf[i],nMsgLen+8);
							ExplainFDK(recBuf,nMsgLen+8);
							memmove(byRecBuf,&(byRecBuf[i+8+nMsgLen]),nDataLen-i-8-nMsgLen);
							nDataLen = nDataLen-i-8-nMsgLen;
							SetEvent(hEvent);
						}
					}
				}
				if(byRecBuf[i+3]==0x21)
				{
					if((byRecBuf[i+1]==0x00)&&
						(byRecBuf[i+2]==0x00))
					{
						if((nDataLen-i)>=10)
						{
							WORD parity = CManager::Crc16Parity(&byRecBuf[i+1],7);
							if((LOBYTE(parity) == byRecBuf[i+8])&&
								(HIBYTE(parity) == byRecBuf[i+9]))
							{
								ZeroMemory(recBuf,BUF_LEN);
								memcpy(recBuf,&byRecBuf[i],10);
								ExplainFDK(recBuf,10);
								memmove(byRecBuf,&(byRecBuf[i+10]),nDataLen-i-10);
								nDataLen = nDataLen-i-10;
								SetEvent(hEvent);
							}
						}
					}
				}
			}
		}
		//	nDataLen = 0;
		SetEvent(hEvent);
	}
	
	
	if(ngyType == GY_CDT)//cdt
	{
		WaitForSingleObject(hEvent, INFINITE);
		for(int i=5;i<nDataLen;i++)
		{
			if((byRecBuf[i]==0x90)&&
				(byRecBuf[i-1]==0xEB)&&
				(byRecBuf[i-2]==0x90)&&
				(byRecBuf[i-3]==0xEB)&&
				(byRecBuf[i-4]==0x90)&&
				(byRecBuf[i-5]==0xEB))
			{
				nHeadCount++;
				if(nHeadCount == 2)
				{
					ZeroMemory(recBuf,BUF_LEN);
					memcpy(recBuf,&byRecBuf[nHead+1],i-nHead-6);
					
					ExplainCDT(recBuf,i-nHead-6);
					
					memmove(byRecBuf,&(byRecBuf[i-5]),nDataLen-i+5);
					nDataLen = nDataLen-i+5;
					
					nHead=0;
					nHeadCount=0;
					SetEvent(hEvent);
					break;
				}
				nHead = i;
			}
		}
		nHead=0;
		nHeadCount=0;
		//nDataLen = 0;
		SetEvent(hEvent);
	}
	
	
	if(ngyType == GY_DF0420)//0420
	{
		WaitForSingleObject(hEvent, INFINITE);
		for(int i=0;i<(nDataLen);i++)
		{
			if(byRecBuf[i]==HEAD_DF0420)
			{
				if(((byRecBuf[i+1]==GetGroup())&&(byRecBuf[i+2]==GetAddress()))
					||((byRecBuf[i+1]==8)&&(byRecBuf[i+2]==1)))
				{
					
					WORD nMsgLen = MAKEWORD(byRecBuf[i+3],byRecBuf[i+4]);
					if((nDataLen-i)>=(nMsgLen+9))
					{
						WORD parity = CManager::Crc16Parity(&byRecBuf[i+1],6+nMsgLen);
						if((LOBYTE(parity) == byRecBuf[i+7+nMsgLen])&&
							(HIBYTE(parity) == byRecBuf[i+8+nMsgLen]))
						{
							ZeroMemory(recBuf,BUF_LEN);
							memcpy(recBuf,&byRecBuf[i],nMsgLen+9);
							Explain0420(recBuf,nMsgLen+9);
							memmove(byRecBuf,&(byRecBuf[i+9+nMsgLen]),nDataLen-i-9-nMsgLen);
							nDataLen = nDataLen-i-9-nMsgLen;
							SetEvent(hEvent);
						}
					}
				}
				
			}
		}
		//nDataLen = 0;
		SetEvent(hEvent);
	}
	
	if(ngyType == GY_MODBUS)//modbus-0241
	{
		WaitForSingleObject(hEvent, INFINITE);
		for(int i=0;i<(nDataLen);i++)
		{
			if(byRecBuf[i]==GetAddress())
			{
				WORD nMsgLen;//不包含校验的数据长度
				BYTE byFuncCode = byRecBuf[i+1];
				switch(byFuncCode) {
				case 0x04://yc return
					nMsgLen = 3+byRecBuf[i+2];
					break;
				case 0x02://yx return
					nMsgLen = 3+byRecBuf[i+2];
					break;
				case 0x05://yk return
					nMsgLen = 6;
					break;
				case 0x06://yt return
					nMsgLen = 6;
					break;
				default:
					break;
				}
				
				if((nDataLen-i)>=(nMsgLen+2))
				{
					WORD parity = CManager::CRC16_Modbus(&byRecBuf[i],nMsgLen);
					if((LOBYTE(parity) == byRecBuf[i+nMsgLen])&&
						(HIBYTE(parity) == byRecBuf[i+1+nMsgLen]))
					{
						ZeroMemory(recBuf,BUF_LEN);
						memcpy(recBuf,&byRecBuf[i],nMsgLen+2);
						Explain0241Modbus(recBuf,nMsgLen+2);
						memmove(byRecBuf,&(byRecBuf[i+2+nMsgLen]),nDataLen-i-2-nMsgLen);
						nDataLen = nDataLen-i-2-nMsgLen;
						SetEvent(hEvent);
					}
				}
			}
			
			
		}
		//nDataLen = 0;
		SetEvent(hEvent);
	}
	
	if(ngyType == GY_JDXJ)//接地巡检仪
	{
		WaitForSingleObject(hEvent, INFINITE);
		for(int i=0;i<(nDataLen);i++)
		{
			if(	(byRecBuf[i]==0xEB)&&
				(byRecBuf[i+1]==0x90)&&
				(byRecBuf[i+2]==0xEB)&&
				(byRecBuf[i+3]==0x90))
			{
				int nMsgLen = 256*byRecBuf[i+6]+byRecBuf[i+7];//信息长度
				if((nDataLen-i)>=(nMsgLen+10))
				{
					BYTE byCheckSum = CManager::Check_sum(&byRecBuf[i+9],nMsgLen-2);
					if((byCheckSum == byRecBuf[i+7+nMsgLen] ))//校验和
					{
						ZeroMemory(recBuf,BUF_LEN);
						memcpy(recBuf,&byRecBuf[i],nMsgLen+10);
						ExplainJdxj(recBuf,nMsgLen+10);
						memmove(byRecBuf,&(byRecBuf[i+10+nMsgLen]),nDataLen-i-10-nMsgLen);
						nDataLen = nDataLen-i-10-nMsgLen;
						SetEvent(hEvent);
					}
				}
				
			}
		}
		//nDataLen = 0;
		SetEvent(hEvent);
	}
}
void CMaintainView::Explain0241Modbus(BYTE *buf, int len)
{
	CString str;

	if(len < 6)
		return;
	
	BYTE nFuncCode;
	nFuncCode = buf[1];
	int i,nCount;

	switch(nFuncCode)
	{
	case 0x04://yc return
		nCount = buf[2]/2;//yc count
		m_ycArray.SetSize(nCount);
		for(i=0;i<nCount;i++)
			m_ycArray.SetAt(i,MAKEWORD(buf[2*i+4],buf[2*i+3]));
		break;
	case 0x02://yx return
		nCount = buf[2];//yx 字节数量
		m_yxArray.SetSize(nCount);
		for(i=0;i<nCount;i++)
			m_yxArray.SetAt(i,buf[3+i]);
		break;
	case 0x05://yk return
		if(buf[5] == 0x00)
		{
			if(dlgYkYt&&dlgYkYt->m_hWnd)
				dlgYkYt->SendMessage(MSG_CONTROLRETURN_MODBUS,1,buf[4]);
		}
		break;
	case 0x06://yt return
		if(dlgYkYt&&dlgYkYt->m_hWnd)
			dlgYkYt->SendMessage(MSG_CONTROLRETURN_MODBUS,2,MAKEWORD(buf[5],buf[4]));
		break;
	default:
		break;
	}
}

void CMaintainView::ExplainJdxj(BYTE *buf, int len)
{
	if(len < 12)
		return;
	
	BYTE nFuncCode;
	nFuncCode = buf[8];
	int j;
	int nMuxianDataCount =0,nZhiluDataCount=0;

	switch(nFuncCode)
	{		
	case 0xC2://母线数据返回
		nMuxianDataCount = (len-12)/3;
		m_jdxjMuxianArray.SetSize(nMuxianDataCount);
		for (j=0;j<nMuxianDataCount;j++)
			m_jdxjMuxianArray.SetAt(j,WORD(10*CManager::Bcd2Hex_JDXJ(buf[9+j*3],buf[9+j*3+1],buf[9+j*3+2])));
		break;
	case 0xC4://支路电阻返回
		nZhiluDataCount = (len-12)/3;
		m_jdxjZhiluArray.SetSize(nZhiluDataCount*2);
		for (j=0;j<nZhiluDataCount;j++)
		{
			m_jdxjZhiluArray.SetAt(j*2,buf[9+j*3]);
			m_jdxjZhiluArray.SetAt(j*2+1,WORD(100*CManager::Bcd2Hex_JDXJ(buf[9+j*3+1]>>4,buf[9+j*3+1]&0x0f,buf[9+j*3+2])));
		}
		break;
	case 0xC5://无接地支路
		m_jdxjZhiluArray.RemoveAll();
		break;
	default:
		break;
	}
}

void CMaintainView::Explain0420(BYTE *buf, int len)
{
	if(len < 11)
		return;
	
	//如果是工装测试则把数据送至工装窗口显示
	if (((CMainFrame*)AfxGetMainWnd())->IsGongzhuangStatus())
	{
		((CMainFrame*)AfxGetMainWnd())->TransBuf(buf,len);
		return;
	}

	BYTE nFuncCode;
	nFuncCode = buf[5];
	int i,nCount;

	switch(nFuncCode)
	{
	case 0xB0://ycyx return
		nCount = buf[7];//yc count
		m_ycArray.SetSize(nCount);

⌨️ 快捷键说明

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