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

📄 mainfrm.cpp

📁 串口收发工具
💻 CPP
📖 第 1 页 / 共 4 页
字号:
			}
		}
		break;
	default:
		break;
}
}

void CMainFrame::SetActionExpain(int nIndex, CString str)
{
	((CPreviewPane2*)m_wndSplitter2.GetPane(1,0))->SetActionExpain(nIndex,str);
}
bool CMainFrame::IsACK()
{
	if((byRecBuf[0]==0x01)&&
	   (byRecBuf[1]==0x00)&&
	   (byRecBuf[2]==0x00)&&
	   (byRecBuf[3]==0x06)&&
	   (byRecBuf[4]==0x00)&&
	   (byRecBuf[5]==0x00))
		return true;
	else
		return false;
}

BYTE CMainFrame::IsNAK()
{
	if((byRecBuf[0]==0x01)&&
	   (byRecBuf[1]==0x02)&&
	   (byRecBuf[2]==0x00)&&
	   (byRecBuf[3]==0x21)&&
	   (byRecBuf[5]==0x00)
	   )
	   if((byRecBuf[4]==0x02)||(byRecBuf[4]==0x04)||(byRecBuf[4]==0x01)||(byRecBuf[4]==0x08))

		return byRecBuf[4];
	return 0;
}


//生成BMP文件
void CMainFrame::CreateBMP()
{
	BYTE bmpHead[62];
	OrganiseBmpHead(bmpHead);
	if(strFileName.IsEmpty())
		return;
	CFile file;
	file.Open(strFileName,CFile::modeReadWrite|CFile::modeCreate);
	file.Write(bmpHead,62);
	for(int i=0;i<240;i++)
	{
		for(int j=0;j<40;j++)
		{
			BYTE a = ~dataBuf[9600 - (i + 1) * 40 + j];
			file.Write(&a,1);
		}
	}
	file.Close();
}
void CMainFrame::CreateParaFile()
{
	if(strFileName.IsEmpty())
		return;
	ChangeFileAttribute(strFileName);
	CFile file;
	file.Open(strFileName,CFile::modeReadWrite|CFile::modeCreate);
	int nFileSize;
	if (nCanshuUploadFlag == CANSHU_SEND_COUNT)
		nFileSize = sizeof(ZLP_COE);
	else
		nFileSize = sizeof(DF0241_COE);
	file.Write(&dataBuf,nFileSize);	
	file.Close();
}

//校验验证
bool CMainFrame::ValidateParity()
{
	WORD Parity = CManager::Crc16Parity(&byRecBuf[1],nCurRecDataLen-3);
	
	if((byRecBuf[nCurRecDataLen-2]==LOBYTE(Parity))&&(byRecBuf[nCurRecDataLen-1]==HIBYTE(Parity)))
		return true;
	return false;
}


//320*240 单色BMP位图前62个字节为固定的
void CMainFrame::OrganiseBmpHead(BYTE *bmpHead)
{
	ZeroMemory(bmpHead,62);
	bmpHead[0] = 'B';
	bmpHead[1] = 'M';
	bmpHead[2] = 190;
	bmpHead[3] = 37;
	bmpHead[10] = 62;
	bmpHead[14] = 40;
	bmpHead[18] = 64;
	bmpHead[19] = 1;
	bmpHead[22] = 240;
	bmpHead[26] = 1;
	bmpHead[28] = 1;
	bmpHead[34] = 128;
	bmpHead[35] = 37;
	bmpHead[38] = 196;
	bmpHead[39] = 14;
	bmpHead[42] = 196;
	bmpHead[43] = 14;
	bmpHead[58] = 255;
	bmpHead[59] = 255;
	bmpHead[60] = 255;
}

bool CMainFrame::ChangeFileAttribute(CString strFile)
{
	WIN32_FIND_DATA  ffblk;
	HANDLE hd = FindFirstFile(strFile,&ffblk);
	
	if(hd&&(ffblk.dwFileAttributes!=0xcccccccc))
	{
		CFileStatus rStatus;
		CFile::GetStatus(strFile,rStatus);
		rStatus.m_attribute = 0x20;
		CFile::SetStatus(strFile,rStatus);
	}
	return true;
}
//功能:组织并发送数据
//参数:功能码,扩展功能码,操作的文件名称,发送次数

bool CMainFrame::OrganiseData(int extfunc,int nSendSerial,CString filename)
{
	if(!m_bOpened)
	{
		MessageBox(_T("串口还未打开,请先打开串口再进行操作!"),_T("串口错误"),MB_ICONQUESTION);
		return false;
	}
	//如果在维护时处于数据收发画面则关闭
//	m_view->CloseDataWnd();
		
	if(filename.GetLength() > 0)
		strFileName = filename;

	BYTE bySendBuf[512];//发送缓冲区
	memset(bySendBuf,0xff,512);

	BYTE byDataBuf[512];//数据缓冲区
	memset(byDataBuf,0x00,512);

	WORD wDataLen;//数据域长度

	switch(extfunc)
	{
	case 30://重启
		wDataLen = 0;
		break;
	case 31://键盘操作
		wDataLen = 1;
		byDataBuf[0] = (BYTE)nSendSerial;
		break;
	case 40://上装图像
	case 41://上装参数
		if(OrganiseSendData(byDataBuf,extfunc,nSendSerial,filename))
		{
			SetTimer(1,2000,NULL);
			wDataLen = 1;
		}
		else
		{
			MessageBox("组织数据出错!","错误",MB_ICONQUESTION);
			return false;
		}
		break;
	case 2://参数文件
	case 3://下发图像1
	case 4://下发图像2
	case 5://下发图像3
	case 6://下发图像4
	case 7://下发图像5
	case 8://下发图像6
	case 9://下发 ASCII字库
	case 10://汉字点阵
	case 11://维护程序
	case 12://ASCII8点阵
	case 20://主程序
		if(OrganiseSendData(byDataBuf,extfunc,nSendSerial,filename))
		{
			SetTimer(1,2000,NULL);
			wDataLen = 258;//256个字节数据+扇区序号低+扇区序号高
		}
		else
		{
			MessageBox("组织数据出错!","错误",MB_ICONQUESTION);
			return false;
		}
		break;
	default:
		break;
	}

	bySendBuf[0] = SOH;				//帧头
	bySendBuf[1] = LOBYTE(wDataLen);//报文长度低字节
	bySendBuf[2] = HIBYTE(wDataLen);//报文长度高字节
	bySendBuf[3] = FUNCCODE;		//功能码
	bySendBuf[4] = (BYTE)extfunc;	//扩展功能码
	bySendBuf[5] = 0x00;

	if(wDataLen > 0)//如果数据域不为空
		memcpy(&bySendBuf[6],byDataBuf,wDataLen);

	WORD Parity = CManager::Crc16Parity(&bySendBuf[1],wDataLen+5);
	bySendBuf[wDataLen+6] = LOBYTE(Parity);
    bySendBuf[wDataLen+7] = HIBYTE(Parity);

	CString strTemp;
	switch(extfunc)
	{
	case 30:
	case 31:
		strTemp.Format("发送%d帧",nSerialNo+1);
		break;
	case 40://上装图像
		strTemp.Format("总长度%d帧,接收%d帧",BMP_SEND_COUNT,nSerialNo+1);
		break;
	case 41://上装参数
		strTemp.Format("总长度%d帧,接收%d帧",nCanshuUploadFlag,nSerialNo+1);
		break;
	case 2://参数文件
	case 3://下发图像1
	case 4://下发图像2
	case 5://下发图像3
	case 6://下发图像4
	case 7://下发图像5
	case 8://下发图像6
	case 9://下发 ASCII字库
	case 10://汉字点阵
	case 11://维护程序
	case 12://ASCII8点阵
	case 20://主程序
		strTemp.Format("总长度%d帧,发送%d帧",nSendFileCount,nSerialNo+1);
		break;
	default:
		break;
	}
	

	if(!m_pDoc->SendData(bySendBuf,wDataLen+8,true))
	{
		KillTimer(1);
		return false;
	}
	else
		SetActionExpain(3,strTemp);
	return true;
}

//组织需要发送的文件数据
bool CMainFrame::OrganiseSendData(BYTE *buf,int extfunc, int nSendSerial, CString filename)
{
	CFile file;
	switch(extfunc)
	{
	case 40://上装图像
	case 41://上装参数
		buf[0] = nSendSerial;
		break;
	case 2://下发参数文件
	case 3://下发图像1
	case 4://下发图像2
	case 5://下发图像3
	case 6://下发图像4
	case 7://下发图像5
	case 8://下发图像6
	case 9://下发ASCII点阵
	case 10://下发汉字点阵
	case 11://下发维护程序
	case 20://下发主程序
	case 12://下发ASCII8点阵
		buf[0] = LOBYTE(nSerialNo);//扇区序号低
		buf[1] = HIBYTE(nSerialNo);//扇区序号高
		memcpy(&buf[2],&dataBuf[nSerialNo*256],256);
		break;
	}
	
	return true;

		
}

void CMainFrame::ActivateFrame(int nCmdShow) 
{
	// TODO: Add your specialized code here and/or call the base class
	m_view = (CMaintainView*)GetActiveView();
	if(m_view)
	{
		m_pDoc = m_view->GetDocument();
		if(!m_pDoc)	return;
	}
	CFrameWnd::ActivateFrame(nCmdShow);
	CManager::SetNotifyWnd(m_view->m_hWnd);
	int ComNo,Baut,DataBit,StopBit,Parity;
	m_pDoc->GetCommOption(ComNo,Baut,DataBit,StopBit,Parity);

	int i;
	switch(Parity)
	{
	case P_NONE://无
		i = 0;
		break;
	case P_EVEN://偶
		i = 2;
		break;
	case P_ODD://奇
		i = 1;
		break;
	default:
		i = 0;
		break;
	}
/*
	CString Text;
	Text.Format("串口%d:%s,%s,%s,%s",(m_pDoc->GetSerialLineInfo())->m_nComNo,
		BuadType[Baut],
		ParityType[i],
		DataBitType[DataBit],
		StopBitType[StopBit?1:0]
		);
	m_wndStatusBar.SetPaneText(1,Text);
	m_bOpened = TRUE;
*/	
}

void CMainFrame::OnClosecom() 
{
	// TODO: Add your command handler code here
	m_pDoc->CloseComm();
	m_bOpened = FALSE;
	m_wndStatusBar.SetPaneText(1,_T(""));
	SetComParaToStatusBar(0);
	KillTimer(1);
	StopSend();
}

void CMainFrame::OnUpdateClosecom(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_bOpened);
}

void CMainFrame::OnUpdateOpencom(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(!m_bOpened);
}

void CMainFrame::StopSend()
{
	((CMaintainView*)m_wndSplitter1.GetPane(0, 0))->StopSend();
	((CPreviewPane2*)m_wndSplitter2.GetPane(1, 0))->ClearList();
}

void CMainFrame::OnSize(UINT nType, int cx, int cy) 
{
	CFrameWnd::OnSize(nType, cx, cy);
	if(!nOpenComCount && nType == SIZE_MAXIMIZED)
	{
		nOpenComCount++;
		OnOpencom();
		
		m_MenuExt.LoadMenu(IDR_MAINFRAME);
		m_MenuExt.LoadToolbar(IDR_MAINFRAME);
		SetMenu(&m_MenuExt);

	}
}
void CMainFrame::ClearList()
{
	((CPreviewPane2*)m_wndSplitter2.GetPane(1, 0))->ClearList();
	ClearDataWindow();
}

void CMainFrame::OnGroup() 
{
	// TODO: Add your command handler code here
	CDlgGroupAddr dlg(nGroupNo,nAddrNo,modbus,this);
	dlg.DoModal();
}
void CMainFrame::SetGroupAddress(int nGroup, int nAddr)
{
	nGroupNo = nGroup;
	nAddrNo = nAddr;
	((CMaintainView*)m_wndSplitter1.GetPane(0, 0))->SetGroupAddress(nGroupNo,nAddrNo);
	if (dlgGongzhuang)
		dlgGongzhuang->SetAddress(nGroupNo,nAddrNo);
}



void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	nCount++;
	CString strTemp;
	if(nIDEvent == 1)
	{
		switch(nExtFunc)
		{
		case 41://上装参数
		case 40://上装图像
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
		case 20:

			if(OrganiseData(nExtFunc,nSerialNo,strFileName))
			{
				strTemp.Format("重发第%d帧%d次",nSerialNo+1,nCount);
				SetActionExpain(3,strTemp);
				
				WaitForSingleObject(hEvent1, INFINITE);
				nCurRecDataLen=0;
				SetEvent(hEvent1);
				strTemp = _T("返回超时,系统重发...");
				SetActionExpain(5,strTemp);
			}
			if(nCount == 10)
			{
				SetActionExpain(5,ENDERROR);
				KillTimer(1);
				nSerialNo = 0;
				nCount = 0;
				nExtFunc = 0;
				WaitForSingleObject(hEvent1, INFINITE);
				nCurRecDataLen=0;
				SetEvent(hEvent1);
			}
			break;
		
			break;
		default:
			break;

		}
	}
	CFrameWnd::OnTimer(nIDEvent);
}

void CMainFrame::SetModbusPara(MODBUS_PARA p)
{
	modbus = p;
	((CMaintainView*)m_wndSplitter1.GetPane(0, 0))->SetModbusPara(modbus);
}
void CMainFrame::AddSOEToList(int wMilliseconds, int wSecond, int wMinute, int wHour, int wDay, int wMonth, int yxno, int yxstatus)
{
	((CPreviewPane2*)m_wndSplitter2.GetPane(1, 0))->AddSOEToList(wMilliseconds,wSecond,wMinute,wHour,wDay,wMonth,yxno,yxstatus);
}
//void CMainFrame::SetModuleInfo(int nModType, int nModNo)
//{
//	((CMaintainView*)m_wndSplitter1.GetPane(0, 0))->SetModuleInfo(nModType,nModNo);
// }
//下发参数文件
void CMainFrame::OnDownCanshufile() 
{
	// TODO: Add your command handler code here
	CString szFilter = 
		_T("参数文件 (*.set)|*.set|All Files (*.*)|*.*||");
	CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
	if(dlg.DoModal()!=IDCANCEL)
	{
		nExtFunc = 2;
		CString str = dlg.GetPathName();
		nSendFileCount = GetFileSendCount(str);
		if(OrganiseData(nExtFunc,nSerialNo,str))
		{
			SetActionExpain(1,_T("下发参数文件"));
			SetActionExpain(2,str);
			SetActionExpain(5,GOING);
		}
	
	}
}
//得到发送文件需要发送的次数并读取文件内容至缓冲区
int CMainFrame::GetFileSendCount(CString str)
{
	CFile file;
	int i;
	if(!file.Open(str,CFile::modeRead))
		return -1;

	memset(dataBuf,0xff,DATALENGTH);
	
	int nLen = file.GetLength();
	if((nExtFunc>=3)&&(nExtFunc<=8))//图像文件
	{
		file.Seek(62,CFile::begin);
		nLen -= 62;
		BYTE bufTemp[9600],a;

⌨️ 快捷键说明

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