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

📄 connect.c

📁 泵站系统屏与屏之间的通信
💻 C
📖 第 1 页 / 共 4 页
字号:
#include "include/mcuapi.h"#define CONN_STATE_CLOSE					0#define CONN_STATE_LISTEN					6#define CONN_STATE_CONNECTED				7#define IEC104_MIN_APDU						12	#define IEC104_MAX_APDU						253#define	FRAMEHEAD							0x68			// 104 protocol frame head#define	STARTDTACT							0x7				// 104 protocol start date transmit act #define	STOPDTACT							0x13			// 104 protocol stop  data transmit act#define	TESTFRACT							0x43			// 104 protocol test frame act#define	STARTDTCON							0x0b			// 104 protocol start date transmit confirm #define	STOPDTCON							0x23			// 104 protocol stop  data transmit confirm#define	TESTFRCON							0x83			// 104 protocol test frame confirm#define	CONFIGCMD							0xff				// 104 protocol networks config command			#define	STARTDT_ACT							0x7				// 104 protocol start data transmit act #define	STARTDT_CONFIRM						0x0b			// 104 protocol start date transmit confirm #define	STOPDT_ACT							0x13			// 104 protocol stop  data transmit act#define	STOPDT_CONFIRM						0x23			// 104 protocol stop  data transmit confirm#define	TESTFR_ACT							0x43			// 104 protocol test frame act#define	TESTFR_CONFIRM						0x83			// 104 protocol test frame confirm#define  IEC104_TIMEOUT_T0					30000#define  IEC104_TIMEOUT_T1					15000#define  IEC104_TIMEOUT_T2					8500#define  IEC104_TIMEOUT_T3					18000#define	 IEC104_ERR_RECV					0x00000001#define	 IEC104_ERR_SEND					0x00000002#define  IEC104_ERR_NEED_S					0x00000004#define	 IEC104_ERR_NODATA					0x00000008#define	 IEC104_FLAG_START					0x00000100#define  IEC104_FLAG_RETURN_S   			0x00000200#define  IEC104_FLAG_TEST					0x00000400#define  IEC104_FLAG_WAIT_S     			0x00000800#define	 CONN_ERR_SEND						0x00010000#define	 CONN_ERR_RECV						0x00020000#define	 CONN_ERR_STATE						0x00040000#define	 CONN_ERR_BUFFOUT					0x00080000#define	 OPERATOR_PORT 						2404			//normal connection port#define	 ENGINEER_PORT						2403			//Engineer connection port#define	 MCUSET_PORT 						4661			//normal connection port#define	 DEBUG_PORT							4662			//Engineer connection portint CONN_SendBuff(SOCKET Socketfd, unsigned char *sendbuff, int nLen){	int nSend = 0;	fd_set fs_write;	struct timeval tv_timeout;	int optval, optlen = sizeof(int);	pthread_mutex_t	Net_Mutex;		getsockopt(Socketfd, SOL_SOCKET, SO_ERROR, &optval, &optlen);		pthread_mutex_init(&Net_Mutex, NULL);	pthread_mutex_lock(&Net_Mutex);	if(Socketfd>0)	{		FD_ZERO(&fs_write);		FD_SET(Socketfd, &fs_write);		tv_timeout.tv_sec = 0;		tv_timeout.tv_usec = 10000;		if(select(Socketfd + 1,  NULL, &fs_write, NULL, &tv_timeout)!=0)		{						if(FD_ISSET(Socketfd, &fs_write))				nSend = send(Socketfd, sendbuff, nLen, MSG_DONTWAIT);		}	}	pthread_mutex_unlock(&Net_Mutex); 	return nSend;}void *NetSend(void * tmp)//负责向后台转发数据{	int k,nRead;	int DelayTime = 10;	unsigned long StartAddr;	unsigned char ReadBuff[256],TempBuff[8];	unsigned short ReadPointer,WritePointer;	while(1)	{		for(k=1; k<=8; k++)		{			StartAddr = k*100+12;//检测COM1-COM8的数据暂存区是否有遥信数据需要发往操作员站			while(PointerNotEq(StartAddr))			{				ReadPointer = GetCurrentPointer(StartAddr);				nRead = ReadOneMessageFromMem(k, ReadBuff, StartAddr, ReadPointer);				if(nRead>0)				{					CONN_SendToOpe(ReadBuff, nRead);					SYS_Delay(DelayTime);				}			}			StartAddr = k*100+8;//检测COM1-COM8的数据暂存区是否有遥测数据需要发往操作员站			while(PointerNotEq(StartAddr))			{				ReadPointer = GetCurrentPointer(StartAddr);				nRead = ReadOneMessageFromMem(k, ReadBuff, StartAddr, ReadPointer);				if(nRead>0)				{					CONN_SendToOpe(ReadBuff, nRead);					SYS_Delay(DelayTime);				}			}			StartAddr = k*100+16;//检测COM1-COM8的数据暂存区是否有数据需要发往工程师站			while(PointerNotEq(StartAddr))			{				ReadPointer = GetCurrentPointer(StartAddr);				nRead = ReadOneMessageFromMem(k, ReadBuff, StartAddr, ReadPointer);				if(nRead>0)				{					CONN_SendToEng(ReadBuff, nRead);					SYS_Delay(DelayTime);				}			}		}	}}int GetIP(int index,unsigned long int *pIp){	int ret;	SOCKET fd;	struct ifreq ifr;	fd = socket(AF_INET, SOCK_STREAM, 0);		if (fd >= 0) 	{		memset(&ifr, 0, sizeof(ifr));		sprintf(ifr.ifr_name,"eth%i",index);			ifr.ifr_addr.sa_family = AF_INET;			ret =ioctl(fd, SIOCGIFADDR, &ifr);		if (ret == 0)		{			*pIp =ntohl(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr);		}	    		close(fd);		}	return ret;}int CONN_COMMU_Enable(){	if((A_Flag>0)||(B_Flag>0))		return 1;	else		return 0;}int CONN_Validate(int ndx){	if ( (ndx>=0) && (ndx<MAX_CLIENT) )	{		return 1;	}	else	{		return 0;	}}int GetFreeConnect(int NetGroup){	int i;	switch(NetGroup)	{	case 0:		for (i=0 ; i < MAX_CLIENT ; i++)		{			if (A_ClientSocket[i].inUse == 0)			{				return i;			}		}		break;	case 1:		for (i=0 ; i < MAX_CLIENT ; i++)		{			if (B_ClientSocket[i].inUse == 0)			{				return i;			}		}		break;	}	return -1;}int CONN_IsEngStation(int NetGroup,int index){	int ret=0;	if (CONN_Validate(index))	{		switch(NetGroup)		{		case 0:			if (A_ClientSocket[index].lport == ENGINEER_PORT)			{				ret = 1;			}			break;		case 1:			if (B_ClientSocket[index].lport == ENGINEER_PORT)			{				ret = 1;			}			break;		}	}	return ret;}void OnCommOK(int iPort, int ucIndex){	int ret=0,uInf_Modbus = -1;	unsigned char IEC104DataBuf_M[256];	unsigned char ucSPI = 0 ,CommState = 1;	unsigned char ucAddr=SubDevice[iPort].SubStations[ucIndex].sub_address;	uInf_Modbus = FindPointInf(YX_Msg, ucAddr, 0, 1 );	if( uInf_Modbus >=0 ) 	{		RefreshDateData(YX_Msg, uInf_Modbus, &CommState );	}	if(CONN_COMMU_Enable())	{		ret = IEC_PackNode_104(IEC104DataBuf_M,ucAddr,3,ucSPI);		if(ret>MIN_IEC104_FRAME)  			WriteMsgToShareMem(iPort,ret,IEC104DataBuf_M,OperatorStation,YX_Msg);	}	}void OnCommError(int iPort, int ucIndex){	int ret=0,uInf_Modbus = -1;	unsigned char IEC104DataBuf_M[256];	unsigned char ucSPI = 1 ,CommState = 0;	unsigned char ucAddr = SubDevice[iPort].SubStations[ucIndex].sub_address;	uInf_Modbus = FindPointInf(YX_Msg, ucAddr, 0, 1 );	if( uInf_Modbus >=0 ) 	{		RefreshDateData(YX_Msg, uInf_Modbus, &CommState );	}	if(CONN_COMMU_Enable())	{		ret = IEC_PackNode_104(IEC104DataBuf_M,ucAddr,3,ucSPI);		if(ret>MIN_IEC104_FRAME)  			WriteMsgToShareMem(iPort,ret,IEC104DataBuf_M,OperatorStation,YX_Msg);	}}void CheckNetConnected(int NetGroup){	int i;	unsigned char SendBuff[8];	SendBuff[0]=0x68;	SendBuff[1]=0x04;	SendBuff[2]=0x43;	SendBuff[3]=0x00;	SendBuff[4]=0x00;	SendBuff[5]=0x00;	switch(NetGroup)	{	case 0:		for (i=0 ; i < MAX_CLIENT ; i++)		{			if ( (A_ClientSocket[i].inUse)&&(A_ClientSocket[i].NeedCheck) )			{				CONN_SendBuff(A_ClientSocket[i].Socket_fd, SendBuff, 6);				A_ClientSocket[i].NeedCheck = 0;			}		}		break;	case 1:		for (i=0 ; i < MAX_CLIENT ; i++)		{			if ( (B_ClientSocket[i].inUse)&&(B_ClientSocket[i].NeedCheck) )			{				CONN_SendBuff(B_ClientSocket[i].Socket_fd, SendBuff, 6);				B_ClientSocket[i].NeedCheck = 0;			}		}		break;	}}int CONN_Send104Data(int NetGroup,int iConnect,unsigned char *pData,int SendLen){	int i,ret=0;	switch(NetGroup)	{	case 0:		if ((A_ClientSocket[iConnect].inUse) && (A_ClientSocket[iConnect].IEC_Flag & IEC104_FLAG_START))		{						*(pData+2)=(unsigned char)(A_ClientSocket[iConnect].Conn_SendCount%256);			*(pData+3)=(unsigned char)(A_ClientSocket[iConnect].Conn_SendCount/256);			*(pData+4)=(unsigned char)(A_ClientSocket[iConnect].Conn_RecvCount%256);			*(pData+5)=(unsigned char)(A_ClientSocket[iConnect].Conn_RecvCount/256);			ret=CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd,pData,SendLen);			if(ret == SendLen) //(errno == EBADF)			{				A_ClientSocket[iConnect].Conn_SendCount	+=2;			}
			if(pData[IEC104_OFFSET_TI] == IEC104_TI_C_DC_NA_1)
			{
				if ((pData[IEC104_OFFSET_CONTEXT] & 0x80) == 0)
				{
					SYS_Delay(20);
					pData[IEC104_OFFSET_COT] =IEC104_COT_M_actterm;
					CONN_PackCount(NetGroup,iConnect,pData);
					ret=CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd,pData,SendLen);
					if(ret == SendLen) //(errno == EBADF)
					{
						A_ClientSocket[iConnect].Conn_SendCount	+=2;
					}
				}
			}		}		ret = SendLen;		break;	case 1:		if ((B_ClientSocket[iConnect].inUse) && (B_ClientSocket[iConnect].IEC_Flag & IEC104_FLAG_START))		{			*(pData+2)=(unsigned char)(B_ClientSocket[iConnect].Conn_SendCount%256);			*(pData+3)=(unsigned char)(B_ClientSocket[iConnect].Conn_SendCount/256);			*(pData+4)=(unsigned char)(B_ClientSocket[iConnect].Conn_RecvCount%256);			*(pData+5)=(unsigned char)(B_ClientSocket[iConnect].Conn_RecvCount/256);			ret=CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd,pData,SendLen);			if ( ret == SendLen ) //(errno == EBADF)			{				B_ClientSocket[iConnect].Conn_SendCount	+=2;			}
			if(pData[IEC104_OFFSET_TI] == IEC104_TI_C_DC_NA_1)
			{
				if ((pData[IEC104_OFFSET_CONTEXT] & 0x80) == 0)
				{
					SYS_Delay(20);
					pData[IEC104_OFFSET_COT] =IEC104_COT_M_actterm;
					CONN_PackCount(NetGroup,iConnect,pData);
					ret=CONN_SendBuff(B_ClientSocket[iConnect].Socket_fd,pData,SendLen);
					if(ret == SendLen) //(errno == EBADF)
					{
						B_ClientSocket[iConnect].Conn_SendCount	+=2;
					}
				}
			}		}		ret = SendLen;		break;	default :		break;	}	return ret;}void CONN_Broad104Data(unsigned char *puc104,int len){	int i;	if(A_Flag)	{		for(i=0 ; i< MAX_CLIENT ; i++)		{				if(A_ClientSocket[i].lport == OPERATOR_PORT)//把数据发送到A网的操作员站				CONN_Send104Data(0,i,puc104,len);		}	}	if(B_Flag)	{		for(i=0 ; i< MAX_CLIENT ; i++)		{			if(B_ClientSocket[i].lport == OPERATOR_PORT)	//把数据发送到B网的操作员站				CONN_Send104Data(1,i,puc104,len);		}	}}void CONN_SendSConfirm(int iConnect){	int ret;	unsigned char ucBuff[6];	if (CONN_Validate(iConnect))	{		ucBuff[0]	=0x68;//START_CODE;		ucBuff[1]	=0x04;		ucBuff[2]	=0x01;		ucBuff[3]	=0x00;		ucBuff[4]	=A_ClientSocket[iConnect].Conn_RecvCount & 0xff;		ucBuff[5]	=(A_ClientSocket[iConnect].Conn_RecvCount >> 8) & 0xff;		ret = CONN_SendBuff(A_ClientSocket[iConnect].Socket_fd, ucBuff, 6);		if (ret == 6 )		{			A_ClientSocket[iConnect].send_wait_count++;		}	}}void CONN_SendToOpe(unsigned char *puc104,int len){	int i;	if(A_Flag)	{		for(i=0 ; i< MAX_CLIENT ; i++)		{			if(A_ClientSocket[i].lport == OPERATOR_PORT)//把数据发送到A网的操作员站				CONN_Send104Data(0,i,puc104,len);		}	}	if(B_Flag)	{		for(i=0 ; i< MAX_CLIENT ; i++)		{			if(B_ClientSocket[i].lport == OPERATOR_PORT)	//把数据发送到B网的操作员站				CONN_Send104Data(1,i,puc104,len);		}	}}void CONN_SendToEng(unsigned char *pucTCP,int len){	int i;	if(A_Flag)	{		for(i=0 ; i< MAX_CLIENT ; i++)		{				if(A_ClientSocket[i].lport == ENGINEER_PORT)	//把数据发送到A网的工程师站				CONN_Send104Data(0,i,pucTCP,len);		}	}	if(B_Flag)	{

⌨️ 快捷键说明

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