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

📄 rwsock.cpp

📁 一个串口通信的程序
💻 CPP
字号:
// RWSock.cpp: implementation of the CRWSock class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "datasvr.h"
#include "RWSock.h"
#include "iic.h"

#include "glovar.h"

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


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRWSock::CRWSock()
{

}

CRWSock::~CRWSock()
{

}

double CRWSock::Calculate(double x)
{
	return x;
}

CString GetAppPath()
{
	TCHAR exeFullPath[MAX_PATH]; 
	DWORD R=GetLastError();
	GetModuleFileName(NULL,exeFullPath,MAX_PATH);
	CString strpath;
	strpath=exeFullPath;
	int lasti=-1;
	while(1)
	{
		int found=strpath.Find('\\',lasti+1);
		if(found==-1)break;
		lasti=found;
	}
//	SYS_PATH=strpath.Left(lasti+1);
	return strpath.Left(lasti+1);
}
void WriteLog(CString str1,CString str2)
{
	CString path;
	CString ff;
//	COleDateTime datetime;
	ff=str2.Mid(48,8);
	ff.TrimRight();//.Trim();
	ff.TrimLeft();
	if(ff.GetLength()==0)ff="20020101";
	path=GetAppPath()+ff.Mid(0,4)+"\\"+ff+".log";
	CFile file;
	CString temp;
	CFileStatus filestatus;
	BOOL ret=CreateDirectory(GetAppPath()+ff.Mid(0,4),NULL);
	if(CFile::GetStatus(path,filestatus))
	{
	}
	else
	{
		file.Open(path,CFile::modeCreate | CFile::modeWrite);
		file.Close();
	}

	if(file.Open(path, CFile::modeWrite))
	{
		file.SeekToEnd();
		file.Write(str1,512);
		file.Write("\r\n",2);
		file.Write(str2,512);
		file.Write("\r\n",2);
		file.Close();
	}
}
_ConnectionPtr cn;
extern CString ComIP;

SMSG sendbuf[50];
RMSG receivebuf[50];
int sendhead;
int receivehead;

void initsendbuf()
{
	sendhead=0;
}
int getsendbuf()
{
	sendhead=(sendhead+1)%50;
	return sendhead;
}

void initreceivebuf()
{
	receivehead=0;
}
int getreceivebuf()
{
	receivehead=(receivehead+1)%50;
	return receivehead;
}
CString FormatStrR(CString str,int count,char ch=' ')
{
	if(str.GetLength()>count)str=str.Left(count);
	int len=str.GetLength();
	for(int i=len;i<count;i++)
	{
		str+=ch;
	}
	len=str.GetLength();
	return str;
}
CString FormatStrL(CString str,int count,char ch=' ')
{
	if(str.GetLength()>count)str=str.Left(count);
	int len=str.GetLength();
	for(int i=len;i<count;i++)
	{
		str=CString(ch)+str;
	}
	return str;
}
void CRWSock::OnReceive(int nErrorCode)
{
	bool haserror=false;
	CString errstring;
	errstring="";
	if(!haserror)
	{
		CString strin,strout;

		char temp[513];
		memset(temp,0,513);

		SENDMSG smsg;
		RECHIEVEMSG rmsg;
		Receive(&smsg,512);
		memcpy(temp,&smsg,512);

		::MakeRMsg(rmsg,smsg.msg_type);
		rmsg.msg_type[0]='R';
		memcpy(rmsg.msg_return,"T002",4);
		CString str1;
		str1="代理层:无法连接通信服务器!";
		memcpy(rmsg.error_info,str1.GetBuffer(str1.GetLength()),str1.GetLength());
		
		NPSENDMSG npsmsg;
//		npsmsg=new SMSG();
		npsmsg=&sendbuf[getsendbuf()];
		memcpy(npsmsg,&smsg,512);

		strout.Format("RRRRRR01P900%s%s%s%s%s%s%sZZZZ",FormatStrR("",16),FormatStrR("",4),FormatStrR("",16),FormatStrR("",16),FormatStrR("",324),FormatStrR("无效的消息类型!",60),FormatStrR("",60));
		int len=strout.GetLength();

		//实时数据
		if(strncmp(smsg.msg_type,"SR01",4)==0)
		{
			CString s_id;int rid;
			s_id=smsg.msg;
			s_id=s_id.Left(5);
			rid=atoi(s_id);

			NPRT nprt;
			NPYB npyb;
			NPCD npcd;
			NPCS npcs;
			for(int i=0;i<arrRT.GetSize();i++)
			{
				nprt=arrRT.GetAt(i);
				if(nprt->rtid==rid)break;
			}
			for(int j=0;j<arrCD.GetSize();j++)
			{
				npcd=arrCD.GetAt(j);
				if(npcd->cdid==nprt->cdid)break;
			}
			for(j=0;j<arrCS.GetSize();j++)
			{
				npcs=arrCS.GetAt(j);
				if(npcs->csid==nprt->csid)break;
			}
			for(j=0;j<arrYB.GetSize();j++)
			{
				npyb=arrYB.GetAt(j);
				if(npyb->ybid==npcd->ybid)break;
			}

			for( i=0;i<RTCount;i++)
			{
				if(xlfdata[i].rtid==rid)
				{
//					if(xlfdata[i].valuestate == 1)
					{
						switch(xlfdata[i].cstype)
						{
						case 0://long
							s_id.Format("%d%ld,%f,%f;",(xlfdata[i].valuestate==-1)?2:xlfdata[i].valuestate,xlfdata[i].lval,nprt->warn_low,nprt->warn_hi );
							break;
						case 1://short
							s_id.Format("%d%ld,%f,%f;",(xlfdata[i].valuestate==-1)?2:xlfdata[i].valuestate,xlfdata[i].sval,nprt->warn_low,nprt->warn_hi);
							break;
						case 2://float
							s_id.Format("%d%f,%f,%f;",(xlfdata[i].valuestate==-1)?2:xlfdata[i].valuestate,xlfdata[i].dval,nprt->warn_low,nprt->warn_hi);
							break;
						}
					}
					break;
				}
			}
			if(i<RTCount)
			{
				strout.Format("RRRRRR01P001%s%s%s%s%s%s%sZZZZ",FormatStrR(xlfdata[i].cdmc,16),FormatStrR("",4),FormatStrR(npcs->csname,16),FormatStrR("",16),FormatStrR(s_id,324),FormatStrR("",60),FormatStrR("",60));
				int len=strout.GetLength();
			}
			else
			{
				strout.Format("RRRRRR01P900%s%s%s%s%s%s%sZZZZ",FormatStrR("",16),FormatStrR("",4),FormatStrR("",16),FormatStrR("",16),FormatStrR(s_id,324),FormatStrR("RID未找到!",60),FormatStrR("",60));
				int len=strout.GetLength();
			}
		}
		else if(strncmp(smsg.msg_type,"SR11",4)==0)
		{
			CString s_id;int rid;
			CString cdname,csname;
			cdname=smsg.msg;
			cdname=cdname.Left(40);
			cdname.TrimLeft();
			cdname.TrimRight();
			csname=smsg.msg+40;
			csname=csname.Left(40);
			csname.TrimLeft();
			csname.TrimRight();
			NPRT nprt;
			NPYB npyb;
			NPCD npcd;
			NPCS npcs;
			bool found=false;
			for(int j=0;j<arrCD.GetSize();j++)
			{
				npcd=arrCD.GetAt(j);
				CString cd;
				cd=npcd->cdname;
				cd.TrimLeft();
				cd.TrimRight();
				if(cd==cdname)break;
			}
			for(j=0;j<arrCS.GetSize();j++)
			{
				npcs=arrCS.GetAt(j);
				CString cs;
				cs=npcs->csname;
				cs.TrimLeft();
				cs.TrimRight();
				if(cs==csname)break;
			}
			for(int i=0;i<arrRT.GetSize();i++)
			{
				nprt=arrRT.GetAt(i);
				if(nprt->cdid == npcd->cdid && nprt->csid == npcs->csid )
				{
					found=true;
					break;
				}
			}
			for(j=0;j<arrYB.GetSize();j++)
			{
				npyb=arrYB.GetAt(j);
				if(npyb->ybid==npcd->ybid)break;
			}
			CString cd;
			cd=npcd->cdname;
			cd.TrimLeft();
			cd.TrimRight();
			CString cs;
			cs=npcs->csname;
			cs.TrimLeft();
			cs.TrimRight();
			if(cs==csname && cd==cdname)found=true;
			else found=false;

			rid=nprt->rtid;

			for( i=0;i<RTCount;i++)
			{
				if(xlfdata[i].rtid==rid)
				{
//					if(xlfdata[i].valuestate == 1)
					{
						switch(xlfdata[i].cstype)
						{
						case 0://long
							s_id.Format("%d%ld,%f,%f;",(xlfdata[i].valuestate==-1)?2:xlfdata[i].valuestate,xlfdata[i].lval,nprt->warn_low,nprt->warn_hi );
							break;
						case 1://short
							s_id.Format("%d%ld,%f,%f;",(xlfdata[i].valuestate==-1)?2:xlfdata[i].valuestate,xlfdata[i].sval,nprt->warn_low,nprt->warn_hi);
							break;
						case 2://float
							s_id.Format("%d%f,%f,%f;",(xlfdata[i].valuestate==-1)?2:xlfdata[i].valuestate,xlfdata[i].dval,nprt->warn_low,nprt->warn_hi);
							break;
						}
					}
					break;
				}
			}
			if(i<RTCount && found)
			{
				strout.Format("RRRRRR01P001%s%s%s%s%s%s%sZZZZ",FormatStrR(xlfdata[i].cdmc,16),FormatStrR("",4),FormatStrR(npcs->csname,16),FormatStrR("",16),FormatStrR(s_id,324),FormatStrR("",60),FormatStrR("",60));
				int len=strout.GetLength();
			}
			else
			{
				strout.Format("RRRRRR01P900%s%s%s%s%s%s%sZZZZ",FormatStrR("",16),FormatStrR("",4),FormatStrR("",16),FormatStrR("",16),FormatStrR(s_id,324),FormatStrR("RID未找到!",60),FormatStrR("",60));
				int len=strout.GetLength();
			}


		}
		//历史数据
		else if(strncmp(smsg.msg_type,"SH01",4)==0)
		{
			CString s_id,s_count;int rid;
			CString datestr;
			s_id=smsg.msg;
			s_id=s_id.Left(5);
			datestr=smsg.msg+5;
			datestr=datestr.Left(12);
			s_count=smsg.msg+17;
			s_count=s_count.Left(2);
			rid=atoi(s_id);

			NPRT nprt;
			NPYB npyb;
			NPCD npcd;
			NPCS npcs;
			for(int i=0;i<arrRT.GetSize();i++)
			{
				nprt=arrRT.GetAt(i);
				if(nprt->rtid==rid)break;
			}
			for(int j=0;j<arrCD.GetSize();j++)
			{
				npcd=arrCD.GetAt(j);
				if(npcd->cdid==nprt->cdid)break;
			}
			for(j=0;j<arrCS.GetSize();j++)
			{
				npcs=arrCS.GetAt(j);
				if(npcs->csid==nprt->csid)break;
			}
			for(j=0;j<arrYB.GetSize();j++)
			{
				npyb=arrYB.GetAt(j);
				if(npyb->ybid==npcd->ybid)break;
			}

			COleDateTime dtfrom,dtto;
			CString year,month,day,hh,min;
			year=datestr.Mid(0,4);
			month=datestr.Mid(4,2);
			day=datestr.Mid(6,2);
			hh=datestr.Mid(8,2);
			min=datestr.Mid(10,2);
			dtfrom.SetDateTime(atoi(year),atoi(month),atoi(day),atoi(hh),atoi(min),0);
			COleDateTimeSpan dtspan;
			dtspan.SetDateTimeSpan(0,0,atoi(s_count),0);
			dtto=dtfrom+dtspan;
			HISTORY_DATA * data;
			int count;
			data=::ReadDataFromFile(rid,dtfrom,dtto,count);
			if(data!=NULL && count>0)
			{
				s_id.Format("%05d",count);
				s_id="";
				for(int ii=0;ii<20 && ii<count;ii++)
				{
					CString temp;
					temp.Format("%d%015.5f",(data[ii].state==101)?1:0,data[ii].value);
					s_id+=temp;
				}
				strout.Format("RRRR%04dP001%s%s%s%s%s%s%sZZZZ",count,FormatStrR(npcd->cdname,16),FormatStrR("",4),FormatStrR(npcs->csname,16),FormatStrR("",16),FormatStrR(s_id,324),FormatStrR("",60),FormatStrR("",60));
				int len=strout.GetLength();
				delete data;
			}
			else
			{
				strout.Format("RRRRRH01P900%s%s%s%s%s%s%sZZZZ",FormatStrR("",16),FormatStrR("",4),FormatStrR("",16),FormatStrR("",16),FormatStrR(s_id,324),FormatStrR("RID未找到!",60),FormatStrR("",60));
				int len=strout.GetLength();
			}
		}
		else if(strncmp(smsg.msg_type,"SH11",4)==0)
		{
			CString s_id;int rid;
			CString datestr;
			CString s_count;
			CString cdname,csname;
			cdname=smsg.msg;
			cdname=cdname.Left(40);
			csname=smsg.msg+40;
			csname=csname.Left(40);
			datestr=smsg.msg+80;
			datestr=datestr.Left(12);
			s_count=smsg.msg+92;
			s_count=s_count.Left(2);

			cdname.TrimLeft();
			cdname.TrimRight();
			csname.TrimLeft();
			csname.TrimRight();

			NPRT nprt;
			NPYB npyb;
			NPCD npcd;
			NPCS npcs;
			bool found=false;
			for(int j=0;j<arrCD.GetSize();j++)
			{
				npcd=arrCD.GetAt(j);
				CString cd;
				cd=npcd->cdname;
				cd.TrimLeft();
				cd.TrimRight();
				if(cd==cdname)break;
			}
			for(j=0;j<arrCS.GetSize();j++)
			{
				npcs=arrCS.GetAt(j);
				CString cs;
				cs=npcs->csname;
				cs.TrimLeft();
				cs.TrimRight();
				if(cs==csname)break;
			}
			for(int i=0;i<arrRT.GetSize();i++)
			{
				nprt=arrRT.GetAt(i);
				if(nprt->cdid == npcd->cdid && nprt->csid == npcs->csid )
				{
					found=true;
					break;
				}
			}
			CString cd;
			cd=npcd->cdname;
			cd.TrimLeft();
			cd.TrimRight();
			CString cs;
			cs=npcs->csname;
			cs.TrimLeft();
			cs.TrimRight();
			if(cs==csname && cd==cdname)found=true;
			else found=false;

			for(j=0;j<arrYB.GetSize();j++)
			{
				npyb=arrYB.GetAt(j);
				if(npyb->ybid==npcd->ybid)break;
			}

			rid=nprt->rtid;
			COleDateTime dtfrom,dtto;
			CString year,month,day,hh,min;
			year=datestr.Mid(0,4);
			month=datestr.Mid(4,2);
			day=datestr.Mid(6,2);
			hh=datestr.Mid(8,2);
			min=datestr.Mid(10,2);
			dtfrom.SetDateTime(atoi(year),atoi(month),atoi(day),atoi(hh),atoi(min),0);
			COleDateTimeSpan dtspan;
			dtspan.SetDateTimeSpan(0,0,atoi(s_count),0);
			dtto=dtfrom+dtspan;
			HISTORY_DATA * data;
			int count;
			data=::ReadDataFromFile(rid,dtfrom,dtto,count);
			if(data!=NULL && count>0 && found)
			{
				s_id="";//.Format("%05d",count);
				for(int ii=0;ii<20 && ii<count;ii++)
				{
					CString temp;
					temp.Format("%d%015.5f",(data[ii].state==101)?1:0,data[ii].value);
					s_id+=temp;
				}
				strout.Format("RRRR%04dP001%s%s%s%s%s%s%sZZZZ",count,FormatStrR(npcd->cdname,16),FormatStrR("",4),FormatStrR(npcs->csname,16),FormatStrR("",16),FormatStrR(s_id,324),FormatStrR("",60),FormatStrR("",60));
//				strout.Format("RRRRRH01P001%s%s%s%s%s%s%sZZZZ",FormatStrR(npcd->cdname,16),FormatStrR("",4),FormatStrR(npcs->csname,16),FormatStrR("",16),FormatStrR(s_id,324),FormatStrR("",60),FormatStrR("",60));
				int len=strout.GetLength();
				delete data;
			}
			else
			{
				strout.Format("RRRRRH01P900%s%s%s%s%s%s%sZZZZ",FormatStrR("",16),FormatStrR("",4),FormatStrR("",16),FormatStrR("",16),FormatStrR(s_id,324),FormatStrR("RID未找到!",60),FormatStrR("",60));
				int len=strout.GetLength();
			}
		}
		//测点配置
		else if(strncmp(smsg.msg_type,"",4)==0)
		{
		}
		//参数配置
		else if(strncmp(smsg.msg_type,"",4)==0)
		{
		}
		//实控配置
		else if(strncmp(smsg.msg_type,"",4)==0)
		{
		}
//		temp;
//		strout;

		CString ret;
		ret=strout;

		if(!haserror)
		{
			memcpy(&rmsg,(char *)ret.GetBuffer(512),512);
		}

		Send(&rmsg,512);

		NPRECHIEVEMSG nprmsg;
		nprmsg=&receivebuf[getreceivebuf()];
		memcpy(nprmsg,&rmsg,512);
//?		::PostMessage(::AfxGetApp()->m_pMainWnd->m_hWnd,CLT_TALKING,(WPARAM)nprmsg,(LPARAM)this);
//		WriteLog((char *)&smsg,(char *)&rmsg);

		::PostMessage(::AfxGetApp()->m_pMainWnd->m_hWnd,CLT_CLOSE,(WPARAM)this,0);

//对帐文件下载

		CString msg_yydm,msg_return,msg_type,msg_real,msg_dzr,msg_dzjg;
		//消息类型码
		msg_type=rmsg.msg_type;
		msg_type=msg_type.Left(4);
		//消息返回码
		msg_return=rmsg.msg_return;
		msg_return=msg_return.Left(4);
		//医疗机构代码
		msg_yydm=rmsg.yljgdm;
		msg_yydm=msg_yydm.Left(16);
		msg_yydm.TrimLeft();
		msg_yydm.TrimRight();
		
		msg_real=rmsg.msg;
		msg_real=msg_real.Left(324);

		msg_dzr=msg_real.Mid(0,8);
		msg_dzjg=msg_real.Mid(8,1);

		if(msg_type=="RE01" && msg_return=="P001")
		{
		}
	}

	CSocket::OnReceive(nErrorCode);
}

⌨️ 快捷键说明

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