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

📄 rxr_cs.c

📁 714771878051_MODBUS_RTU.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
							             send_Len=(sendbuf[2]+3);        //发送数据长度
							             group_out_ad();
							             break;
							             }
						           case 3:
						                {
							             command_03();
							             sendbuf[2]=(uchar)(sl_n*2);     //返回字节数
							             AI=ad1;
                                         sendbuf[3]=(uchar)(AI>>8);      //高
							             sendbuf[4]=(uchar)(ad1);        //低
							             AI=ad2;
                                         sendbuf[5]=(uchar)(AI>>8);      //高
							             sendbuf[6]=(uchar)(ad2);        //低
							             AI=ad3;
                                         sendbuf[7]=(uchar)(AI>>8);      //高
							             sendbuf[8]=(uchar)(ad3);       //低
							             send_Len=(sendbuf[2]+3);        //发送数据长度
							             group_out_ad();
							             break;
							            }
						           case 4:
						                {
							             command_03();
							             sendbuf[2]=(uchar)(sl_n*2);     //返回字节数
							             AI=ad1;
                                         sendbuf[3]=(uchar)(AI>>8);      //高
							             sendbuf[4]=(uchar)(ad1);        //低
							             AI=ad2;
                                         sendbuf[5]=(uchar)(AI>>8);      //高
							             sendbuf[6]=(uchar)(ad2);        //低
							             AI=ad3;
                                         sendbuf[7]=(uchar)(AI>>8);      //高
							             sendbuf[8]=(uchar)(ad3);       //低
							             AI=ad4;
                                         sendbuf[9]=(uchar)(AI>>8);     //高
							             sendbuf[10]=(uchar)(ad4);       //低
							             send_Len=(sendbuf[2]+3);        //发送数据长度
							             group_out_ad();
							             break;
							            }
						           default :
                                        {
						                sendbuf[0]=addr;             //取地址
	                                    sendbuf[1]=0xff;             //命令号
							            send_Len=2;                  //发送数据长度
							            group_out_ad();
						                break;
                                        }
						          }
                             break;
                            }
                        case 1:
                             {
                             switch (sl_n)
				                   {
						           case 1:
						                {
							            command_03();
							            sendbuf[2]=(uchar)(sl_n*2);     //返回字节数
							            AI=ad2;
                                        sendbuf[3]=(uchar)(AI>>8);      //高
							            sendbuf[4]=(uchar)(ad1);        //低
							            send_Len=(sendbuf[2]+3);        //发送数据长度
							            group_out_ad();
							            break;
							            }
						           case 2:
						                {
							            command_03();
							            sendbuf[2]=(uchar)(sl_n*2);     //返回字节数
						 	            AI=ad2;
                                        sendbuf[3]=(uchar)(AI>>8);      //高
							            sendbuf[4]=(uchar)(ad1);        //低
							            AI=ad3;
                                        sendbuf[5]=(uchar)(AI>>8);      //高
							            sendbuf[6]=(uchar)(ad2);        //低
							            send_Len=(sendbuf[2]+3);        //发送数据长度
							            group_out_ad();
							            break;
							            }
						           case 3:
						                {
							            command_03();
							            sendbuf[2]=(uchar)(sl_n*2);     //返回字节数
							            AI=ad2;
                                        sendbuf[3]=(uchar)(AI>>8);      //高
							            sendbuf[4]=(uchar)(ad1);        //低
							            AI=ad3;
                                        sendbuf[5]=(uchar)(AI>>8);      //高
							            sendbuf[6]=(uchar)(ad2);        //低
							            AI=ad4;
                                        sendbuf[7]=(uchar)(AI>>8);      //高
							            sendbuf[8]=(uchar)(ad3);       //低
							            send_Len=(sendbuf[2]+3);        //发送数据长度
							            group_out_ad();
							            break;
							            }
						           default :
                                        {
						                sendbuf[0]=addr;             //取地址
	                                    sendbuf[1]=0xff;             //命令号
							            send_Len=2;                  //发送数据长度
							            group_out_ad();
						                break;
                                        }
						           }
                             break;
                             }
                       case 2:
                            {
                            switch (sl_n)
				                   {
						           case 1:
						                 {
							             command_03();
							             sendbuf[2]=(uchar)(sl_n*2);     //返回字节数
							             AI=ad3;
                                         sendbuf[3]=(uchar)(AI>>8);      //高
							             sendbuf[4]=(uchar)(ad1);        //低
							             send_Len=(sendbuf[2]+3);        //发送数据长度
							             group_out_ad();
							             break;
							             }
						           case 2:
						                 {
							             command_03();
							             sendbuf[2]=(uchar)(sl_n*2);     //返回字节数
						 	             AI=ad3;
                                         sendbuf[3]=(uchar)(AI>>8);      //高
							             sendbuf[4]=(uchar)(ad1);        //低
							             AI=ad4;
                                         sendbuf[5]=(uchar)(AI>>8);      //高
							             sendbuf[6]=(uchar)(ad2);        //低
							             send_Len=(sendbuf[2]+3);        //发送数据长度
							             group_out_ad();
							             break;
							             }
						           default :
                                         {
						                 sendbuf[0]=addr;             //取地址
	                                     sendbuf[1]=0xff;             //命令号
							             send_Len=2;                  //发送数据长度
							             group_out_ad();
						                 break;
                                         }
						          }
                             break;
                            }
                      case 3:
                           {
                            switch (sl_n)
				                   {
						           case 1:
						                 {
							             command_03();
							             sendbuf[2]=(uchar)(sl_n*2);     //返回字节数
							             AI=ad4;
                                         sendbuf[3]=(uchar)(AI>>8);      //高
							             sendbuf[4]=(uchar)(ad1);        //低
							             send_Len=(sendbuf[2]+3);        //发送数据长度
							             group_out_ad();
							             break;
							             }
						            default :
                                         {
						                 sendbuf[0]=addr;             //取地址
	                                     sendbuf[1]=0xff;             //命令号
							             send_Len=2;                  //发送数据长度
							             group_out_ad();
						                 break;
                                         }
						           }
                             break;
                            }
                       default :
                               {
						       sendbuf[0]=addr;             //取地址
	                           sendbuf[1]=0xff;             //命令号
							   send_Len=2;                  //发送数据长度
							   group_out_ad();
						       break;
                               }
                       }
				  break;
				  }
			 case 0x05:    //强制单线圈
			      {
				  star_fun=recebuf_com[2];star_fun=(star_fun<<8)|(recebuf_com[3]);//取线圈号
                  sl_n=recebuf_com[4];sl_n=(sl_n<<8)|(recebuf_com[5]);        //读取的数量
	              sendbuf[2]=0x00;             //线圈Hi
	              sendbuf[3]=recebuf_com[3];   //线圈Li
	              sendbuf[4]=recebuf_com[4];   //
	              sendbuf[5]=recebuf_com[5];   //
                  switch  (sl_n)
				        {
						case 0xff00:    //on
						    {
							command_05();
                            switch (star_fun)
                                  {
                                  case 0:{jd_on(0x01);break;}
                                  case 1:{jd_on(0x02);break;}
                                  case 2:{jd_on(0x04);break;}
                                  case 3:{jd_on(0x08);break;}
                                  case 4:{jd_on(0x10);break;}
                                  case 5:{jd_on(0x20);break;}
                                  default:{break;}
                                  }
							group_out_ad();
							break;
							}
                        case 0x0000:    //off
						    {
							command_05();
							switch (star_fun)
                                  {
                                  case 0:{jd_off(0x01);break;}
                                  case 1:{jd_off(0x02);break;}
                                  case 2:{jd_off(0x04);break;}
                                  case 3:{jd_off(0x08);break;}
                                  case 4:{jd_off(0x10);break;}
                                  case 5:{jd_off(0x20);break;}
                                  default:{break;}
                                  }
							group_out_ad();
							break;
							}
						default :
						    sendbuf[0]=addr;             //取地址
	                        sendbuf[1]=0xff;             //命令号
							send_Len=2;                  //发送数据长度
							group_out_ad();
						    break;
						} 
				  break;
				  }
             default:
			        {
			        sendbuf[0]=addr;             //取地址
	                sendbuf[1]=0xff;             //命令号
				    send_Len=2;                  //发送数据长度
				    group_out_ad();
                    break;
					}
			 }
	   }
     XJ=1;RUN=1;
	 command_ok=0;    //消息处理完
	 }

void command_01(void)
     {
	 sendbuf[0]=addr;             //取地址
	 sendbuf[1]=0x01;             //命令号
	 sendbuf[2]=0x01;             //返回字节数
	 send_Len=4;                  //发送数据长度
	 }

void command_02(void)
     {
	 sendbuf[0]=addr;             //取地址
	 sendbuf[1]=0x02;             //命令号
	 sendbuf[2]=0x01;             //返回字节数
	 send_Len=4;                  //发送数据长度
	 }

void command_03(void)
     {
	 sendbuf[0]=addr;             //取地址
	 sendbuf[1]=0x03;             //命令号
	 }

void command_05(void)
     {
	 sendbuf[0]=addr;             //取地址
	 sendbuf[1]=0x05;             //命令号
	 send_Len=6;                  //发送数据长度
	 }


/*=================================*/
/*=======定时器0中断处理函数=======*/
/*          中断计时1ms            */
/*=================================*/
void timer0(void) interrupt 1 using 2
     {
	 TH0  = 0xf8;
     TL0  = 0x14;
	 if(timer0_cn<2)
	   {timer0_cn++;}
	 else
	   {
	   TR0=0;
	   rece_Len=0;
	   XJ=1;
	   command_ok=1; //一个消息接收完成!等待处理 
       }  
	 }
/*===========================*/
/*======串口配置函数函数=====*/
/*  bruad:波特率            */
/*===========================*/
void Serial(void)
    {
	SCON   = 0xDC;
    TMOD   = 0x21;
    TH1    = 0xFA;
    TL1    = 0xFA;
    PCON  &= 0x7F;
    TR1    = 1;
	}


/*===========================*/
/*========奇偶校验函数=======*/
/*===========================*/
void chenck_fun(uchar x)
      {
      ACC=x;
	  if(P) {TB8=1;} //若P=1,说明有奇数个1,TB8置1
	  else  {TB8=0;} 
      }
/*=================================*/
/*=========中断接收数据函数========*/
/*=================================*/
void com(void) interrupt 4 using 3
    {
    EA=0;
	if (RI)
       {
	   RI=0;TR0=0;
	   if(timer0_cn>1) //>1.5个字符停顿,刷新不完整消息   1x2ms
		  {
		  rece_Len=0;	//command_ok=0;
		  TH0=0xf8;TL0=0x14;timer0_cn=0;TR0=1;
		  }
	   recebuf[rece_Len]=SBUF;         //接收数据
	   recebuf_com[rece_Len]=recebuf[rece_Len];
	   rece_Len++;
	   command_Len=rece_Len;  //消息长度
       ACC=recebuf[rece_Len];
       /*以下进行奇偶校验*/
	   if(P)  //P=1时,说明接收到的数据为奇数个1,接收第9位RB8为1正确,否则错误!
		 {
		 if(RB8){js_check=1;}//偶校验正确
         else{js_check=2;}//偶校验错误!
		 }
	   else  //P=0时,说明接收到的数据为偶数个1,接收第9位RB8为0正确,否则错误!
		 {
		 if(!RB8){js_check=1;}//偶校验正确
         else{js_check=2;}//偶校验错误!
		 }
       rece_Len&=0x0f;
	   timer0_cn=0;TR0=1;//接收完数据,启动接收计时,检测是否停顿超过1.5个字符时间停顿!
	   }
    EA=1;
	}


⌨️ 快捷键说明

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