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

📄 ican.c

📁 可自行修改移植的CAN应用层协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
				 	ican_cet=0;
					ican_del_cnnet_sla_respond(SrcMACID);
				}
				else
				{
					ican_respond_err(SrcMACID,SourceID,cnet_unexist);				
				}
			}
			else
			{
				ican_respond_err(SrcMACID,SourceID,funcID_parameter_unsupport);	
			}
	
	break;
	case reset_sla://复位从站
	if(ican_cet==1)
	{
		ican_respond_err(SrcMACID,SourceID,cmd_unsupport);		
	}
	else
	{
		ican_respond_err(SrcMACID,SourceID,cnet_unexist);				
	}
	break;
	
	case check_ID://检测ID
	ican_check_slaID_respond(SrcMACID);
	break;
//	case err_cnet://错误连接
//	break;
	default:if(ACK==0)//当请求回应,但没有此功能码时
			{
				ican_respond_err(SrcMACID,SourceID,1);	//返回无此功能码ErrID:0x01
			}
		
}

else
if (ACK==1)//响应帧
{
switch(FunCID)
{
	case write_port:
 OSFlagPost(ican_pevent,ican_write_OK,OS_FLAG_SET,&err);
	break;
	
	case read_port:
		if(SourceID<=0x1f)
		{
			if(SourceID+d_long-1<=iCANdeviceList[SrcMACID].DILen)//数据长度没有超出
			{
				array_copy(p,&iCANdeviceList[SrcMACID].P_DILen[SourceID],d_long);
			}
			
		}
		else
		if(SourceID<=0x3f)
		{
			if(SourceID+d_long-1-0x20<=iCANdeviceList[SrcMACID].DOLen)//数据长度没有超出
			{
				array_copy(p,&iCANdeviceList[SrcMACID].P_DOLen[SourceID-0x20],d_long);
			}
		
				
		}
		else
		if(SourceID<=0x5f)
		{
				if(SourceID+d_long-1-0x40<=iCANdeviceList[SrcMACID].AILen)//数据长度没有超出
			{
				array_copy(p,&iCANdeviceList[SrcMACID].P_AILen[SourceID-0x40],d_long);
			
			}
			
		}
		else
		if(SourceID<=0x7f)
		{
			if(SourceID+d_long-1-0x60<=iCANdeviceList[SrcMACID].AOLen)//数据长度没有超出
			{
				array_copy(p,&iCANdeviceList[SrcMACID].P_AOLen[SourceID-0x60],d_long);
			
			}

			
		}
		OSFlagPost(ican_pevent,ican_read_OK,OS_FLAG_SET,&err);//发送信号量			
	break;

	case slv_to_mas_trigger	:
			if(SourceID<=0x1f)
		{
			if(SourceID+d_long-1<=iCANdeviceList[SrcMACID].DILen)//数据长度没有超出
			{
				array_copy(p,&iCANdeviceList[SrcMACID].P_DILen[SourceID],d_long);
			}
			
		}
		else
		if(SourceID<=0x3f)
		{
			if(SourceID+d_long-1-0x20<=iCANdeviceList[SrcMACID].DOLen)//数据长度没有超出
			{
				array_copy(p,&iCANdeviceList[SrcMACID].P_DOLen[SourceID-0x20],d_long);
			}
		
				
		}
		else
		if(SourceID<=0x5f)
		{
				if(SourceID+d_long-1-0x40<=iCANdeviceList[SrcMACID].AILen)//数据长度没有超出
			{
				array_copy(p,&iCANdeviceList[SrcMACID].P_AILen[SourceID-0x40],d_long);
			
			}
			
		}
		else
		if(SourceID<=0x7f)
		{
			if(SourceID+d_long-1-0x60<=iCANdeviceList[SrcMACID].AOLen)//数据长度没有超出
			{
				array_copy(p,&iCANdeviceList[SrcMACID].P_AOLen[SourceID-0x60],d_long);
			
			}
			
		}
	break;
	
	
	case creat_cnet:
	/*****************释放空间内存(为了安全)**************/
	if(iCANdeviceList[SrcMACID].sla_status==0)//当前为第一次连接
	{
	#if ican_DRAM_Enalbe
	
		ican_free (iCANdeviceList[SrcMACID].P_DILen);
		ican_free (iCANdeviceList[SrcMACID].P_DOLen);
		ican_free (iCANdeviceList[SrcMACID].P_AILen);
		ican_free (iCANdeviceList[SrcMACID].P_AOLen);
		iCANdeviceList[SrcMACID].DILen=*p;
		p++;
		iCANdeviceList[SrcMACID].DOLen=*p;
		p++;
		iCANdeviceList[SrcMACID].AILen=*p;
		p++;
		iCANdeviceList[SrcMACID].AOLen=*p;
		/********************重新申请内存空间*****************/
		
		iCANdeviceList[SrcMACID].P_DILen=ican_malloc(iCANdeviceList[SrcMACID].DILen);
		iCANdeviceList[SrcMACID].P_DOLen=ican_malloc(iCANdeviceList[SrcMACID].DOLen);
		iCANdeviceList[SrcMACID].P_AILen=ican_malloc(iCANdeviceList[SrcMACID].AILen);
		iCANdeviceList[SrcMACID].P_AOLen=ican_malloc(iCANdeviceList[SrcMACID].AOLen);
	#endif
		iCANdeviceList[SrcMACID].sla_status=1;
	}
	OSFlagPost(ican_pevent,ican_crt_cnet_ok,OS_FLAG_SET,&err);//发送信号量		
	break;

	case del_cnet:
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	iCANdeviceList[SrcMACID].sla_status=0;
	
	#if ican_DRAM_Enalbe
	ican_free (iCANdeviceList[SrcMACID].P_DILen);
	ican_free (iCANdeviceList[SrcMACID].P_DOLen);
	ican_free (iCANdeviceList[SrcMACID].P_AILen);
	ican_free (iCANdeviceList[SrcMACID].P_AOLen);
	iCANdeviceList[SrcMACID].DILen=0;
	iCANdeviceList[SrcMACID].DOLen=0;
	iCANdeviceList[SrcMACID].AILen=0;
	iCANdeviceList[SrcMACID].AOLen=0;
	#endif
	OSFlagPost(ican_pevent,ican_del_cnet_ok,OS_FLAG_SET,&err);//发送信号量	
	break;
	//case reset_sla:
	//break;
	
	case check_ID:
	ican_cet=2;//标志id以存在 不能正常工作
	OSFlagPost(ican_pevent,ican_id_exiet,OS_FLAG_SET,&err);//发送信号量	
	break;
	
	case err_cnet:
	switch(*p)
		{
			case funcID_unexist:
			OSFlagPost(ican_pevent,ican_err_funID_unfound,OS_FLAG_SET,&err);//发送信号量	
			break;
			
			case res_unfound:
			OSFlagPost(ican_pevent,ican_err_sourID_unfound,OS_FLAG_SET,&err);//发送信号量	
			break;
			
			case cmd_unsupport:
			OSFlagPost(ican_pevent,ican_err_cmd_unsupply,OS_FLAG_SET,&err);//发送信号量	
			break;
			
			case funcID_parameter_unsupport:
			OSFlagPost(ican_pevent,ican_err_funID_err,OS_FLAG_SET,&err);//发送信号量	
			break;
			
			case cnet_unexist:
			ican_create_cnnet_sla (SrcMACID);//重新建立连接
			OSFlagPost(ican_pevent,ican_err_cent_unfound,OS_FLAG_SET,&err);//发送信号量,通知等待建立连接,重发数据	
			break;
			
			case operate_impact:
			OSFlagPost(ican_pevent,ican_err_operate_impact,OS_FLAG_SET,&err);//发送信号量	
			break;
			
			case flag_err:
			OSFlagPost(ican_pevent,ican_err_flag_err,OS_FLAG_SET,&err);//发送信号量	
			break;			
		}
	break;

			
}
}
}

#define wait_time 5
/***********************************主站轮训函数*******************************
本函数为主站功能函数
作者:张森
日期:2008年4月9日
********************************************************************************/
void master_wheel_cnet()
{INT8U i,err,cnt_f=0;
	ALL_OVER=1;
	for(i=0;i<ICAN_SLV_NUM;i++)
	{  
	
	if(iCANdeviceList[i].sla_status!=3)//是否忽略此站
		{//cnt_f=1;
			if(iCANdeviceList[i].sla_status==0)//这个站是首次连接,需要建立连接获取数据长度,申请数据空间
			{			
goto_create_cnet:
				OSFlagAccept(ican_pevent,ican_crt_cnet_ok|ican_err_cmd_unsupply|ican_err_funID_err,OS_FLAG_WAIT_SET_ANY|OS_FLAG_CONSUME,&err);//清除标识,保证安全,保证当前使用标识为空
				ican_create_cnnet_sla(i) ;//发送建立连接命令。
				OSFlagPend(ican_pevent,ican_crt_cnet_ok|ican_err_cmd_unsupply|ican_err_funID_err,OS_FLAG_WAIT_SET_ANY,wait_time,&err);//等待命令成功或从站已经连接或参数错误,不复位标志符
				if(err==OS_NO_ERR)//有响应
				{
					OSFlagAccept(ican_pevent,ican_crt_cnet_ok,OS_FLAG_WAIT_SET_ALL|OS_FLAG_CONSUME,&err);//查看是否连接成功,并复位标志符
					if(err==OS_NO_ERR)//成功建立连接
					{
						
					}
					else
					{
						OSFlagAccept(ican_pevent,ican_err_cmd_unsupply,OS_FLAG_WAIT_SET_ALL|OS_FLAG_CONSUME,&err);
						if(err==OS_NO_ERR)//从站已经建立了连接
						{
							ican_del_cnnet_sla(i);//删除连接
							OSFlagPend(ican_pevent,ican_del_cnet_ok|ican_err_cent_unfound|ican_err_funID_err,OS_FLAG_WAIT_SET_ANY|OS_FLAG_CONSUME,wait_time,&err);//等待删除成功命令			
							if(err==OS_NO_ERR)
							{   ALL_OVER&=0;
								if(iCANdeviceList[i].sla_timeover_cont>0)//成功发送一次
								{
									iCANdeviceList[i].sla_timeover_cont=0;	//超时--
									
								}	
								goto goto_create_cnet;	
							}
							else
							if(err==OS_TIMEOUT)//超时一次
							{	ALL_OVER&=1;
								if(iCANdeviceList[i].sla_timeover_cont<255)
								{
									iCANdeviceList[i].sla_timeover_cont++;	//超时++
									
								}	
							}
						}
						else
						{
							OSFlagAccept(ican_pevent,ican_err_funID_err,OS_FLAG_WAIT_SET_ALL|OS_FLAG_CONSUME,&err);	//得到格式不对的回应,清楚标志位,(为了安全没有别的作用)
						}
					}
					ALL_OVER&=0;
					if(iCANdeviceList[i].sla_timeover_cont>0)//成功发送一次,超时计数器不为0
					{
						iCANdeviceList[i].sla_timeover_cont=0;	//超时--
						
					}	
				}
				else
				if(err==OS_TIMEOUT)//从站超时
				{ALL_OVER&=1;
					if(iCANdeviceList[i].sla_timeover_cont<255)//没有超出上限
					{
						iCANdeviceList[i].sla_timeover_cont++;//超时++
						
					}
				}
				
				
			}
			if(iCANdeviceList[i].sla_status==1)//此战为正常连接^_^,开始交换数据信息
			{	if(iCANdeviceList[i].DOLen>0)
				{
					/****************************写ok***********没有建立连接********功能参数非法*********资源不存在**************命令不支持*************操作无影响*********/		
					OSFlagAccept(ican_pevent,ican_write_OK|ican_err_cent_unfound|ican_err_funID_err|ican_err_sourID_unfound|ican_err_cmd_unsupply|ican_err_operate_impact,OS_FLAG_WAIT_SET_ANY|OS_FLAG_CONSUME,&err);//清除标识,保证安全,保证当前使用标识为空
					ican_write_sla(i,0x20,iCANdeviceList[i].P_DOLen,iCANdeviceList[i].DOLen);
					OSFlagPend(ican_pevent,ican_write_OK|ican_err_cent_unfound|ican_err_funID_err|ican_err_sourID_unfound|ican_err_cmd_unsupply|ican_err_operate_impact,OS_FLAG_WAIT_SET_ANY,wait_time,&err);
					if(err==OS_NO_ERR)//成功写入数据
					{	ALL_OVER&=0;if(iCANdeviceList[i].sla_timeover_cont>0)//成功发送一次
								{
									iCANdeviceList[i].sla_timeover_cont=0;	//超时--
									
								}	
						OSFlagAccept(ican_pevent,ican_err_cent_unfound,OS_FLAG_WAIT_SET_ANY|OS_FLAG_CONSUME,&err);
						if(err==OS_NO_ERR)
						{
							goto goto_create_cnet;			
						}
					}
					else
					if(err==OS_TIMEOUT)//从站超时
					{ALL_OVER&=1;
						if(iCANdeviceList[i].sla_timeover_cont<255)//没有超出上限
						{
							iCANdeviceList[i].sla_timeover_cont++;//超时++
							
						}
					}
					
				}
				if(iCANdeviceList[i].AOLen>0)
				{
					/****************************写ok***********没有建立连接********功能参数非法*********资源不存在**************命令不支持*************操作无影响*********/		
					OSFlagAccept(ican_pevent,ican_write_OK|ican_err_cent_unfound|ican_err_funID_err|ican_err_sourID_unfound|ican_err_cmd_unsupply|ican_err_operate_impact,OS_FLAG_WAIT_SET_ANY|OS_FLAG_CONSUME,&err);//清除标识,保证安全,保证当前使用标识为空
					ican_write_sla(i,0x60,iCANdeviceList[i].P_AOLen,iCANdeviceList[i].AOLen);
					OSFlagPend(ican_pevent,ican_write_OK|ican_err_cent_unfound|ican_err_funID_err|ican_err_sourID_unfound|ican_err_cmd_unsupply|ican_err_operate_impact,OS_FLAG_WAIT_SET_ANY,wait_time,&err);
					if(err==OS_NO_ERR)//成功写入数据
					{	if(iCANdeviceList[i].sla_timeover_cont>0)//成功发送一次
								{
									iCANdeviceList[i].sla_timeover_cont=0;	//超时--
									
								}	ALL_OVER&=0;
						OSFlagAccept(ican_pevent,ican_err_cent_unfound,OS_FLAG_WAIT_SET_ANY|OS_FLAG_CONSUME,&err);
						if(err==OS_NO_ERR)
						{
							goto goto_create_cnet;			
						}
					}
					else
					if(err==OS_TIMEOUT)//从站超时
					{ALL_OVER&=1;
						if(iCANdeviceList[i].sla_timeover_cont<255)//没有超出上限
						{
							iCANdeviceList[i].sla_timeover_cont++;//超时++
							
						}
					}
					
				}
				
				
				if(iCANdeviceList[i].DILen>0)
				{
					/**************************** 读ok***********没有建立连接********功能参数非法*********资源不存在**************命令不支持*************操作无影响*********/		
					OSFlagAccept(ican_pevent,ican_read_OK|ican_err_cent_unfound|ican_err_funID_err|ican_err_sourID_unfound,OS_FLAG_WAIT_SET_ANY|OS_FLAG_CONSUME,&err);//清除标识,保证安全,保证当前使用标识为空
					ican_read_sla(i,0x00,0,iCANdeviceList[i].DILen);
					OSFlagPend(ican_pevent,ican_read_OK|ican_err_cent_unfound|ican_err_funID_err|ican_err_sourID_unfound,OS_FLAG_WAIT_SET_ANY,wait_time,&err);
					if(err==OS_NO_ERR)//有响应
					{	ALL_OVER&=0;if(iCANdeviceList[i].sla_timeover_cont>0)//成功发送一次
								{
									iCANdeviceList[i].sla_timeover_cont=0;	//超时--
									
								}	
						OSFlagAccept(ican_pevent,ican_err_cent_unfound,OS_FLAG_WAIT_SET_ANY|OS_FLAG_CONSUME,&err);
						if(err==OS_NO_ERR)
						{
							goto goto_create_cnet;			
						}
					}
					else
					if(err==OS_TIMEOUT)//从站超时
					{ALL_OVER&=1;
						if(iCANdeviceList[i].sla_timeover_cont<255)//没有超出上限
						{
							iCANdeviceList[i].sla_timeover_cont++;//超时++
							
						}
					}
					
				}
				
				
				if(iCANdeviceList[i].AILen>0)
				{
					/**************************** 读ok***********没有建立连接********功能参数非法*********资源不存在**************命令不支持*************操作无影响*********/		
					OSFlagAccept(ican_pevent,ican_read_OK|ican_err_cent_unfound|ican_err_funID_err|ican_err_sourID_unfound,OS_FLAG_WAIT_SET_ANY|OS_FLAG_CONSUME,&err);//清除标识,保证安全,保证当前使用标识为空
					ican_read_sla(i,0x40,0,iCANdeviceList[i].AILen);
					OSFlagPend(ican_pevent,ican_read_OK|ican_err_cent_unfound|ican_err_funID_err|ican_err_sourID_unfound,OS_FLAG_WAIT_SET_ANY,wait_time,&err);
					if(err==OS_NO_ERR)//有响应
					{ALL_OVER&=0;	
					if(iCANdeviceList[i].sla_timeover_cont>0)//成功发送一次
								{
									iCANdeviceList[i].sla_timeover_cont=0;	//超时--
									
								}	
						OSFlagAccept(ican_pevent,ican_err_cent_unfound,OS_FLAG_WAIT_SET_ANY|OS_FLAG_CONSUME,&err);
						if(err==OS_NO_ERR)
						{
							goto goto_create_cnet;			
						}
					}
					else
					if(err==OS_TIMEOUT)//从站超时
					{ALL_OVER&=1;
						if(iCANdeviceList[i].sla_timeover_cont<255)//没有超出上限
						{
							iCANdeviceList[i].sla_timeover_cont++;//超时++
							
						}
					}
					
				}
				
				
			
			}
		
		}
	
       

       OSTimeDly(1);    	
	}
	if(ALL_OVER)
	{
		M(30,0)=1;
	}
	else
	{
		M(30,0)=0;
	}
	
	for(i=1;i<ICAN_SLV_NUM;i++)
	{
		VB(79+i)=0;		
		if(iCANdeviceList[i].sla_status!=3)//是否忽略此站
		{
			if(iCANdeviceList[i].sla_timeover_cont>200)
			{
				VB(79+i)=1;	
			}
			else
			{
				VB(79+i)=0;	
			}
			
		}
		
		
	}
	
}

⌨️ 快捷键说明

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