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

📄 connect.c

📁 泵站系统屏与屏之间的通信
💻 C
📖 第 1 页 / 共 4 页
字号:
										}									}									/*--------------------是否为校时命令----------------------*/									if((B_ClientSocket[i].lport == OPERATOR_PORT) && (RecvBuff[6] == IEC104_TI_C_CS_NA_1))//操作员站来的校时命令									{										timeBAK.mSecond	= RecvBuff[IEC104_OFFSET_CONTEXT+1] * 0x100+RecvBuff[IEC104_OFFSET_CONTEXT] ;			//change local time																timeBAK.second		= timeBAK.mSecond/1000;												timeBAK.minute		= RecvBuff[IEC104_OFFSET_CONTEXT+2] & 0x3f;										timeBAK.hour		= RecvBuff[IEC104_OFFSET_CONTEXT+3] & 0x1f;										timeBAK.day			= RecvBuff[IEC104_OFFSET_CONTEXT+4] & 0x1f;										timeBAK.month		= RecvBuff[IEC104_OFFSET_CONTEXT+5] & 0x0f;										timeBAK.year		= RecvBuff[IEC104_OFFSET_CONTEXT+6] & 0x7f;										if(memcmp(OpeTimeBuff,RecvBuff+15,7)!=0)										{											memcpy(OpeTimeBuff,RecvBuff+15,7);											SetTime(timeBAK);										}										CONN_PackCount(1,i,RecvBuff);										RecvBuff[IEC104_OFFSET_COT] = IEC104_COT_M_acton;											B_ClientSocket[i].Conn_SendCount += 2;										CONN_SendBuff(B_ClientSocket[i].Socket_fd, RecvBuff, MIN_IEC104_FRAMELEN+9);									}									else if ((B_ClientSocket[i].lport == ENGINEER_PORT) && (RecvBuff[6] == IEC103_TI_M_SYN_TA_3))//工程师站来的校时命令									{										timeBAK.mSecond	= RecvBuff[TCP_OFFSET_CONTEXT+1] * 0x100 + RecvBuff[TCP_OFFSET_CONTEXT] ;				//change local time				 										timeBAK.second		= timeBAK.mSecond/1000;												timeBAK.minute		= RecvBuff[TCP_OFFSET_CONTEXT+2] & 0x3f;										timeBAK.hour		= RecvBuff[TCP_OFFSET_CONTEXT+3] & 0x1f;										timeBAK.day			= RecvBuff[TCP_OFFSET_CONTEXT+4] & 0x1f;										timeBAK.month		= RecvBuff[TCP_OFFSET_CONTEXT+5] & 0x0f;										timeBAK.year		= RecvBuff[TCP_OFFSET_CONTEXT+6] & 0x7f;										if(memcmp(EngTimeBuff,RecvBuff+15,7)!=0)										{											memcpy(EngTimeBuff,RecvBuff+15,7);											SetTime(timeBAK);										}										CONN_PackCount(1,i,RecvBuff);										B_ClientSocket[i].Conn_SendCount += 2;										CONN_SendBuff(B_ClientSocket[i].Socket_fd, RecvBuff, MIN_TCP_FRAMELEN+9);									}									else									{										SendBuff[0] = START_CODE2;										SendBuff[1] = 0x04;										SendBuff[2] = 0x01;										SendBuff[3] = 0x00;										SendBuff[4] = LOBYTE(B_ClientSocket[i].Conn_RecvCount);										SendBuff[5] = HIBYTE(B_ClientSocket[i].Conn_RecvCount);										CONN_SendBuff(B_ClientSocket[i].Socket_fd, SendBuff, 6);									}								}							}						}					}					else if (PreRead==0)  //客户端断开了连接					{						if((B_ClientSocket[i].lport==OPERATOR_PORT)||(B_ClientSocket[i].lport==ENGINEER_PORT))						{							B_Flag--;						}						FD_CLR(B_ClientSocket[i].Socket_fd,&B_Sockset);						close(B_ClientSocket[i].Socket_fd);						Conn_Clear(1,i);					}					else					{						getsockopt(B_ClientSocket[i].Socket_fd, SOL_SOCKET, SO_ERROR, &optval, &optlen);					}				}			}		}		add_socket(&B_Sockset, &B_ListenSocket[0], &B_ClientSocket[0]);		//////////////////////////////////////////处理B网的连接,数据接收结束///////////////////////////////////////////////////	}}void ProcessParameters(int NetGroup, int iConnect, unsigned char *pData){	unsigned int i,m,nLen;	switch(*(pData+2))	{	case 0x77:		break;	case 0x55://write		SetParameters(pData,*(pData+3));		if (NetGroup==0)			CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd, pData, 4);		if (NetGroup==1)			CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd, pData, 4);		break;		case 0xAA://Read		GetParameters(NetGroup,iConnect,pData+3);		break;	case 0x66://Set YX and YC TransTable		if(*(pData+3)==0x03)		{			if (NetGroup==0)				CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd, pData, 4);			if (NetGroup==1)				CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd, pData, 4);		}		else		{			UpDateTransTable(pData,*(pData+3));		}		break;		case 0xFF:		FlashCopyIn(0,MaxAccess,MyFlashBuff);		if(NetGroup==0)			CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd, pData, 4);		if(NetGroup==1)			CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd, pData, 4);		for(i=0;i<IEC_Client_Num;i++)		{			if(A_ClientSocket[i].inUse)				close(A_ClientSocket[i].Socket_fd);			if(B_ClientSocket[i].inUse)				close(B_ClientSocket[i].Socket_fd);		}		for(i=0; i<MBTCP_Client_Num; i++)		{			if(MBTCP_A_ClientSocket[i].inUse)				close(MBTCP_A_ClientSocket[i].Socket_fd);			if(MBTCP_B_ClientSocket[i].inUse)				close(MBTCP_B_ClientSocket[i].Socket_fd);		}		system("reset");		break;		default :				break;	}}void GetParameters(int NetGroup, int iConnect,unsigned char *nType){	int i,m,nLen;	unsigned char SendBuff[32];	switch(*nType)	{	case 0xf0:		Get_COM_Parameter(NetGroup, iConnect);		break;	case 0x0f:		Get_Dev_Parameter(NetGroup, iConnect,*(nType+1)-0xf0);		break;	case 0xFF:		SendBuff[0]=0x68;		SendBuff[1]=4;		SendBuff[2]=0xaa;		SendBuff[3]=0xFF;		SendBuff[4]=(unsigned char)(MyFlashBuff[ComputerAddr]/256);		SendBuff[5]=(unsigned char)(MyFlashBuff[ComputerAddr]%256);		if (NetGroup==0)			CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd, SendBuff, SendBuff[1]+2);		if (NetGroup==1)			CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd, SendBuff, SendBuff[1]+2);		break;	default :		break;	}}void Get_COM_Parameter(int NetGroup, int iConnect){	int i,m,nLen;	unsigned short readbuff[128];	unsigned char SendBuff[256],TempBuff[256];	for(m=0;m<40;m++)		readbuff[m]=MyFlashBuff[m];	for(m=0;m<80;m++)	{		if(m%2==0)			TempBuff[m]=(unsigned char)(readbuff[m/2]/256);		else			TempBuff[m]=(unsigned char)(readbuff[(m-1)/2]%256);	}	SendBuff[0]=0x68;	SendBuff[1]=10*8+2;	SendBuff[2]=0xaa;	SendBuff[3]=0xf0;	for(i=0;i<80;i++)	{		SendBuff[i+4] = TempBuff[i];	}	if (NetGroup==0)		CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd, SendBuff, SendBuff[1]+2);	if (NetGroup==1)		CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd, SendBuff, SendBuff[1]+2);}void Get_Dev_Parameter(int NetGroup, int iConnect,unsigned char iPort){	int i,m,nLen;	int DevNum;	unsigned short readbuff[128];	unsigned char SendBuff[256],TempBuff[256];		DevNum=(unsigned char)(MyFlashBuff[iPort*100]%256);	for(m=0;m<DevNum*5;m++)		readbuff[m]=MyFlashBuff[iPort*100+m+1];	for(m=0;m<DevNum*10;m++)	{		if(m%2==0)			TempBuff[m]=(unsigned char)(readbuff[m/2]/256);		else			TempBuff[m]=(unsigned char)(readbuff[(m-1)/2]%256);	}	SendBuff[0]=0x68;	SendBuff[1]=DevNum*10+4;	SendBuff[2]=0xaa;	SendBuff[3]=0x0f;	SendBuff[4]=0xf0+iPort;	SendBuff[5]=DevNum;	for(i=0;i<DevNum*10;i++)	{		SendBuff[i+6] = TempBuff[i];	}	if (NetGroup==0)		CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd, SendBuff, SendBuff[1]+2);	if (NetGroup==1)		CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd, SendBuff, SendBuff[1]+2);}void UpDateTransTable(unsigned char *pData,unsigned char nType){	int i,iPort,nMeter;	int Total_YX,Total_YC;	int Count = (*(pData+1)-4)/4;	int offset = *(pData+4)*256+*(pData+5);	switch(nType)	{	case 0x01://YC		Total_YC = offset + Count;		if(Total_YC < MaxTransYc)		{				MyFlashBuff[YcTableAddr] = offset + Count;  			for(i=0;i<Count;i++)			{				MyFlashBuff[YcTableAddr+2*offset+2*i+1] = *(pData+6+4*i)*256+*(pData+7+4*i);				MyFlashBuff[YcTableAddr+2*offset+2*i+2] = *(pData+8+4*i)*256+*(pData+9+4*i);			}		}		break;	case 0x02://YX		Total_YX = offset + Count;		if(Total_YX < MaxTransYx)		{				MyFlashBuff[YxTableAddr] = offset + Count; 			for(i=0;i<Count;i++)			{				MyFlashBuff[YxTableAddr+2*offset+2*i+1] = *(pData+6+4*i)*256+*(pData+7+4*i);				MyFlashBuff[YxTableAddr+2*offset+2*i+2] = *(pData+8+4*i)*256+*(pData+9+4*i);			}		}		break;	case 0x05://设置漳州电度表地址		iPort = *(pData+4);		if(iPort>8) iPort = 8;		nMeter = *(pData+5);		if(nMeter>16) nMeter = 16;		MyFlashBuff[DianDuAddr + 50*(iPort-1)] = iPort*256 +  nMeter;		for(i=0;i<nMeter;i++)		{			MyFlashBuff[DianDuAddr + 50*(iPort-1) + 1 + 3*i] = *(pData+7+6*i)*256+*(pData+6+6*i);			MyFlashBuff[DianDuAddr + 50*(iPort-1) + 2 + 3*i] = *(pData+9+6*i)*256+*(pData+8+6*i);			MyFlashBuff[DianDuAddr + 50*(iPort-1) + 3 + 3*i] = *(pData+11+6*i)*256+*(pData+10+6*i);		}		break;	case 0x06:		for(i=0; i<7; i++)		{			MyFlashBuff[ModbusTCP+i] = pData[4+2*i]*256 +  pData[5+2*i];		}		break;	}}void SetParameters(unsigned char *pData,unsigned char nType){	switch(nType)	{	case 0xF0:		Set_COM_Parameter(pData+4,*(pData+1)-2);		break;	case 0x0F:		Set_Dev_Parameter(pData+4,*(pData+1)-2);		break;	case 0xFF:		MyFlashBuff[ComputerAddr] = pData[4]*256+pData[5];//设置操作员站和工程师站个数		break;	}}void Set_COM_Parameter(unsigned char *pData,unsigned char nLen){	int m,nWrite;	if((nLen%2)==0)		nWrite=nLen/2;	else		nWrite=nLen/2+1;	for(m=0;m<nWrite;m++)	{		MyFlashBuff[m] = pData[2*m]*256+pData[2*m+1];	}}void Set_Dev_Parameter(unsigned char *pData,unsigned char nLen){	int offset;	int m,nWrite;	if((nLen%2)==0)		nWrite=nLen/2;	else		nWrite=nLen/2+1;	offset=(*pData-0xf0)*100;	for(m=0;m<nWrite;m++)	{		MyFlashBuff[m+offset]=pData[2*m]*256+pData[2*m+1];	}}void ProcessDebugParameters(int NetGroup, int iConnect, unsigned char *pData){	unsigned int i,m,nLen;	unsigned char SendBuff[256];		switch(*(pData+2))	{	case 0x88:		switch(pData[3])		{		case 0:			if(NetGroup==0)			{				A_ClientSocket[iConnect].DebugFlag = 0;			}			if (NetGroup==1)			{				B_ClientSocket[iConnect].DebugFlag = 0;			}			break;		case 1:			if(NetGroup==0)			{				A_ClientSocket[iConnect].DebugFlag = 1;			}			if (NetGroup==1)			{				B_ClientSocket[iConnect].DebugFlag = 1;			}			break;		case 0xF0:			MyFlashBuff[DebugPortAddr] = pData[4]*256+pData[5];			if (NetGroup==0)				CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd, pData, pData[1]+2);			if (NetGroup==1)				CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd, pData, pData[1]+2);			break;		case 0x0F:			//68  02 88 0F			SendBuff[0] = 0x68;			SendBuff[1] = 4;			SendBuff[2] = 0x88;			SendBuff[3] = 0x0F;			SendBuff[4] = HIBYTE(MyFlashBuff[DebugPortAddr]);			SendBuff[5] = LOBYTE(MyFlashBuff[DebugPortAddr]);			if (NetGroup==0)				CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd, SendBuff, SendBuff[1]+2);			if (NetGroup==1)				CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd, SendBuff, SendBuff[1]+2);			break;		default :			break;		}		break;	case 0xFF:		FlashCopyIn(0,MaxAccess,MyFlashBuff);		if(NetGroup==0)		{			CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd, pData, 4);			for(i=0;i<MAX_CLIENT;i++)			{				if(A_ClientSocket[i].inUse)				{					close(A_ClientSocket[i].Socket_fd);					Conn_Clear(0,i);				}			}		}		if(NetGroup==1)		{			CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd, pData, 4);			for(i=0;i<MAX_CLIENT;i++)			{				if(B_ClientSocket[i].inUse)				{					close(B_ClientSocket[i].Socket_fd);					Conn_Clear(1,i);				}			}		}			system("reset");		break;		default :		break;	}}void PrintDebugInfo(int iPort,unsigned char MsgType,unsigned char *pData,int nLen){	int i;	unsigned char SendBuff[512];	if(iPort != CurDebugPort) return;	if(nLen>253) nLen=253;	SendBuff[0] = 0x68;	SendBuff[1] = nLen + 2;	SendBuff[2] = MsgType;	SendBuff[3] = iPort;	memcpy(SendBuff+4,pData,nLen);	for(i=0;i<MAX_CLIENT;i++)	{		if((A_ClientSocket[i].lport==DEBUG_PORT)&&(A_ClientSocket[i].inUse)&&(A_ClientSocket[i].DebugFlag))		{			CONN_SendBuff(A_ClientSocket[i].Socket_fd,SendBuff,SendBuff[1]+2);		}		if((B_ClientSocket[i].lport==DEBUG_PORT)&&(B_ClientSocket[i].inUse)&&(B_ClientSocket[i].DebugFlag))		{			CONN_SendBuff(B_ClientSocket[i].Socket_fd,SendBuff,SendBuff[1]+2);		}	}}

⌨️ 快捷键说明

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