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

📄 comm.c

📁 该程序完成了c8051外围电路的驱动和串口协议的编写。希望能给大家学习51单片机带来帮助
💻 C
📖 第 1 页 / 共 2 页
字号:
//*******************************************************************************
//Copyright (c) 2007,派利斯电子(北京)有限公司 硬件部
//文件名称: comm.c
//摘    要: 本文件主要包含通信解析部分的程序
//当前版本: D-1 
//作    者:  佟文杰
//完成日期: 4/16/2007
//修 改 人:
//修改原因:
//修改日期:
//*******************************************************************************
#include <string.h>
#include "comm.h"
#include "framemodule.h"

extern xdata union para1 All;

unsigned char  xdata  *pInput;
xdata struct System *pOutput;
unsigned char  xdata  uchSendBuf[100];
unsigned char xdata uchAlarmStatusJudge[2];
unsigned char xdata uchAlarmStatusJudgeBAK[2];
unsigned char xdata uchModuleConfigStatus;
unsigned char xdata uchCPUAddressStatus;

unsigned int xdata uiForeChannelType; //20080529

//--------------------------------------------------------------------
//函数名称:void CanMessageProcess(void)
//功    能:处理CAN命令包
//输入参数:无
//返    回:无
//完成日期:4/16/2007
//--------------------------------------------------------------------
void CanMessageProcess(void)
{
	unsigned int idata i,uiOffset,uiLength;
	unsigned char  xdata  *p;
	static unsigned char xdata uchCalibrationStatus = 2;  //0:标定零点 1:标定满度  2:初始值无意义

	FeedDog();												  
	pOutput = (struct System  *)&All;					  
	i = BagNum();
	while(i--)
	{
		pInput = GetAMessage();
		if(pInput == NULL)
		{
			return;
		}
		switch(pInput[3])
		{
			case READ_MODULE_PARAMETERS:			  //0x01
				if (((uchCPUAddressStatus == 0) && (pInput[4] == 1 || pInput[4] == 2)) 
						|| ((uchCPUAddressStatus == 1) && (pInput[4] == 3 || pInput[4] == 4)))
				{
					uchSendBuf[0] = 0;
					uiOffset = *((unsigned int *)&pInput[5]);
					uiLength = *((unsigned int *)&pInput[7]);
					uiLength = uiLength*2;
					uchSendBuf[1] = (uiLength+6)/256;	
					uchSendBuf[2] = (uiLength+6)%256;
					uchSendBuf[3] = 0x02;
					uchSendBuf[4] = pInput[4];
					*((unsigned int *)&uchSendBuf[5]) = uiOffset;
					*((unsigned int *)&uchSendBuf[7]) = uiLength/2;	
	
					if ((uiOffset >= 40200) && (uiOffset <= 40237))
					{
						uiOffset = AddrMap(uiOffset);					   
						if(pInput[4] == 1 || pInput[4] == 2)
						{
							p = (unsigned char  xdata *)&pOutput->Ch[0x1&(pInput[4] - 1)]+uiOffset*2;
							memcpy((unsigned char*)&uchSendBuf[9],p,uiLength);
						}
		
						if(pInput[4] == 3 || pInput[4] == 4)
						{
							p = (unsigned char  xdata *)&pOutput->Ch[0x1&(pInput[4] - 3)]+uiOffset*2;
							memcpy((unsigned char*)&uchSendBuf[9],p,uiLength);
						}
					} 
					else if (uiOffset == 40251)
					{
						if (pInput[4] == 1 || pInput[4] == 2)
						{
							if (uiLength == 2)
							{
								*(unsigned int *)&uchSendBuf[9] = *(unsigned int xdata *)&pOutput->uiTripValue[pInput[4] - 1];
							}
							else if (uiLength == 4)
							{
								*(unsigned int *)&uchSendBuf[9] = *(unsigned int xdata *)&pOutput->uiTripValue[pInput[4] - 1];
								*(unsigned int *)&uchSendBuf[11] = *(unsigned int xdata *)&pOutput->uiBypassFlag[pInput[4] - 1];
							}
						}
						else if (pInput[4] == 3 || pInput[4] == 4)
						{
							if (uiLength == 2)
							{
								*(unsigned int *)&uchSendBuf[9] = *(unsigned int xdata *)&pOutput->uiTripValue[pInput[4] - 3];
							}
							else if (uiLength == 4)
							{
								*(unsigned int *)&uchSendBuf[9] = *(unsigned int xdata *)&pOutput->uiTripValue[pInput[4] - 3];
								*(unsigned int *)&uchSendBuf[11] = *(unsigned int xdata *)&pOutput->uiBypassFlag[pInput[4] - 3];
							}
						}
					}
					else if (uiOffset == 40252)
					{
						if (pInput[4] == 1 || pInput[4] == 2)
						{
							*(unsigned int *)&uchSendBuf[9] = *(unsigned int xdata *)&pOutput->uiBypassFlag[pInput[4] - 1];
						}
						else if (pInput[4] == 3 || pInput[4] == 4)
						{
							*(unsigned int *)&uchSendBuf[9] = *(unsigned int xdata *)&pOutput->uiBypassFlag[pInput[4] - 3];
						}	
					}
					SendPackage(0,1,uchSendBuf);
				}
				break;

			case READ_MODULE_INFO:				   //0x03	   读模块相关信息
				if (uchCPUAddressStatus == 0)
				{
					uchSendBuf[0] = 0;
					uiOffset = *((unsigned int *)&pInput[4]);
					uiLength = *((unsigned int *)&pInput[6]);
					uiLength = uiLength*2;
					uchSendBuf[1] = (uiLength+5)/256;
					uchSendBuf[2] = (uiLength+5)%256;
					uchSendBuf[3] = 0x04;

					*((unsigned int *)&uchSendBuf[4]) = *((unsigned int *)&pInput[4]);
					*((unsigned int *)&uchSendBuf[6]) = *((unsigned int *)&pInput[6]);
					p = (unsigned char  xdata *)&pOutput->Info+(uiOffset-40191)*2;
					memcpy((unsigned char*)&uchSendBuf[8],p,uiLength);
					(unsigned int)uchSendBuf[8] = 403;          
					SendPackage(3,1,uchSendBuf);
				}
				break;

			case READ_CURRENT_STATUS:				//0x05

				break;

			case SET_FULL_SCALE:					  //0x0b
				pOutput->iDataSpan = *((int*)&pInput[4]);				
				WriteSystemPara(0,(unsigned char xdata *)pOutput, PARALENGTH*2); // flash module
				SetFullScale(*((int*)&pInput[4]));
				break;

			case READ_FULL_SCALE:					 //0x0c
				uchSendBuf[0] = 0;
				uchSendBuf[1] = 0;
				uchSendBuf[2] = 3;
				uchSendBuf[3] = 0xD;
				uchSendBuf[4] = pOutput->iDataSpan / 256;
				uchSendBuf[5] = pOutput->iDataSpan % 256;	 		 			  
				SendPackage(3,1,uchSendBuf);
				break;

			case SET_MODULE_STATUS:						 //0xa5				 						
				SetModuleStatusMain(*(unsigned char *)&pInput[4]);//设置状态到主模块		  
				ClearSelfFlag();	
				break;

			case CONFIG_CHANNEL_PARA:		//0x55			
				if (((uchCPUAddressStatus == 0) && (pInput[4] == 1 || pInput[4] == 2)) 
						|| ((uchCPUAddressStatus == 1) && (pInput[4] == 3 || pInput[4] == 4)))
				{
					if (uchModuleConfigStatus == 0x01)		 //只有在Config状态下才能配置通道参数
					{				
						uiOffset = *((unsigned int *)&pInput[5]);
						uiLength = *((unsigned int *)&pInput[7]);
						uiLength = uiLength*2;												
						uiOffset = AddrMap(uiOffset);

						uiForeChannelType = *((unsigned int *)&pInput[59]);	   //20080529
						JudgeChannelTypePara();									   //20080529
						*((unsigned int *)&pInput[59]) = uiForeChannelType;	   //20080529


						if ((pInput[4] == 1) || (pInput[4] == 2))
						{
							p = (unsigned char  xdata *)&pOutput->Ch[pInput[4] - 1] + uiOffset*2;
						}
						else if ((pInput[4] == 3) || (pInput[4] == 4))
						{
							p = (unsigned char  xdata *)&pOutput->Ch[pInput[4] - 3] + uiOffset*2;	
						}
						memcpy(p,(unsigned char*)&pInput[9],uiLength);												
						WriteSystemPara(0,(unsigned char xdata *)pOutput, PARALENGTH*2);	   // flash module
						SetModuleChannelParaStatusMain(1);	   //通知框架参数有变化,需要判断处理及初始化变量
						SetModuleNewConfigStatusMain(1);	 //将模块被重新配置过的状态标志位送到框架中
						InitFramemoduleParameters();		 //最新添加
						InitAlarmModuleSoftPara();			//配置参数重新初始化报警参数
						SetAfreshConfigFlagAlarm(pInput[4],1);
					}	//需要重新初始化AD
				}
				break;

			case CONFIG_FACTORY_INFO:					  //0x57
				if (uchModuleConfigStatus == 0x01)		//Config状态才能进行厂家信息配置
				{
					uiOffset = *((unsigned int *)&pInput[4]);
					uiLength = *((unsigned int *)&pInput[6]);
					if (*((unsigned int *)&pInput[16]) != MODULE20)
					{
						*((unsigned int *)&pInput[16]) = MODULE20;
					}
					p = (unsigned char  xdata *)&pOutput->Info + (uiOffset-40191)*2;
					memcpy(p,(unsigned char*)&pInput[8],uiLength*2);
					WriteSystemPara(0,(unsigned char xdata *)pOutput, PARALENGTH*2);	// flash module
					ReportModuleType(MODULE20, ReadCANAddress());  //读完厂家信息后再一次上报模块信息
				}
				else
				{
					;
				}
				break;

			case CALIBRATION:					//0x40	
				if (((uchCPUAddressStatus == 0) && (pInput[4] == 1 || pInput[4] == 2)) 
						|| ((uchCPUAddressStatus == 1) && (pInput[4] == 3 || pInput[4] == 4)))
				{
					if ((pInput[4] == 1) || (pInput[4] == 2))
					{
						SetCalibrationStatusMain(*(unsigned char *)&pInput[4],*((unsigned int*)&pInput[5]));
					}	
					else if ((pInput[4] == 3) || (pInput[4] == 4))
					{
						SetCalibrationStatusMain(*(unsigned char *)&pInput[4] - 2,*((unsigned int*)&pInput[5]));
					}
					if (*((unsigned int*)&pInput[5]) == 8)
					{
						uchCalibrationStatus = 0;				
					}
					else if (*((unsigned int*)&pInput[5]) == 9)
					{
						uchCalibrationStatus = 1;				
					}		
				}					  
				break;

			case CALCULATE:	                              //0x41   标定值
				if (((uchCPUAddressStatus == 0) && (pInput[4] == 1 || pInput[4] == 2)) 
						|| ((uchCPUAddressStatus == 1) && (pInput[4] == 3 || pInput[4] == 4)))
				{
					if ((pInput[4] == 1) || (pInput[4] == 2))
					{
						if (uchCalibrationStatus == 0)										 
						{
							SetCalibCurrentZero(*(unsigned char *)&pInput[4], *(unsigned int *)&pInput[5]);
							CalculateCalibrateValue(*(unsigned char *)&pInput[4], 0);
							uchCalibrationStatus = 2;
						}
						else if(uchCalibrationStatus == 1)
						{
							SetCalibCurrentFull(*(unsigned char *)&pInput[4], *(unsigned int *)&pInput[5]);
							CalculateCalibrateValue(*(unsigned char *)&pInput[4], 1);
							uchCalibrationStatus = 2;
						}
					}
					else if ((pInput[4] == 3) || (pInput[4] == 4))
					{
						if (uchCalibrationStatus == 0)										 
						{
							SetCalibCurrentZero(*(unsigned char *)&pInput[4] - 2, *(unsigned int *)&pInput[5]);
							CalculateCalibrateValue(*(unsigned char *)&pInput[4] - 2, 0);
							uchCalibrationStatus = 2;
						}
						else if(uchCalibrationStatus == 1)
						{
							SetCalibCurrentFull(*(unsigned char *)&pInput[4] - 2, *(unsigned int *)&pInput[5]);
							CalculateCalibrateValue(*(unsigned char *)&pInput[4] - 2, 1);
							uchCalibrationStatus = 2;
						}
					}
				}
				break;
			case ALARMRESET:			   //0x12报警复位				
				SetResetStatusALM(1);		   //送报警模块
				break;

			case BYPASSCOMMAND:			 //0x13	 Bypass
				if (((uchCPUAddressStatus == 0) && (pInput[4] == 1 || pInput[4] == 2)) 
						|| ((uchCPUAddressStatus == 1) && (pInput[4] == 3 || pInput[4] == 4)) || (pInput[4] == 0xff))
				{
					if ((pInput[4] == 1) || (pInput[4] == 2))
					{
						*(unsigned int xdata *)&pOutput->uiBypassFlag[pInput[4] - 1] = *(unsigned int *)&pInput[5];
					 	WriteSystemPara(0,(unsigned char xdata *)pOutput, PARALENGTH*2);
					}
					else if ((pInput[4] == 3) || (pInput[4] == 4))
					{
						*(unsigned int xdata *)&pOutput->uiBypassFlag[pInput[4] - 3] = *(unsigned int *)&pInput[5];
					 	WriteSystemPara(0,(unsigned char xdata *)pOutput, PARALENGTH*2);
					}
					else if (pInput[4] == 0xff)
					{
						*(unsigned int xdata *)&pOutput->uiBypassFlag[0] = *(unsigned int *)&pInput[5];
						*(unsigned int xdata *)&pOutput->uiBypassFlag[1] = *(unsigned int *)&pInput[5];
						WriteSystemPara(0,(unsigned char xdata *)pOutput, PARALENGTH*2);			
					}
				}
				break;

			case TRIPLECOMMAND:			 //0x14   倍增
				if (((uchCPUAddressStatus == 0) && (pInput[4] == 1 || pInput[4] == 2)) 
						|| ((uchCPUAddressStatus == 1) && (pInput[4] == 3 || pInput[4] == 4)) || (pInput[4] == 0xff))
				{
					if ((pInput[4] == 1) || (pInput[4] == 2))

⌨️ 快捷键说明

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