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

📄 rtu.c

📁 基于c8051f020的应用
💻 C
字号:
//**************************************************************************************************************************
#include <c8051F020.h>
#include <stdio.h>
#include "HEAD.H"
//**************************************************************************************************************************
void RTU_Data(void)
{
    xdata unsigned int temp1,temp2;
    xdata unsigned int addres,bytes;

    temp1 = CRC16(DatPocket,0,DatLong-2);
    temp2 = DatPocket[DatLong-1];
    temp2|= DatPocket[DatLong-2]<<8;			                // 解析校验码
    if(temp1==temp2)					                        // 校验码正确
    {
        if(DatPocket[0] == DeviceNum)                           // 检测设备地址号
        {
            LED2_ON;
            addres = DatPocket[2];                              // 解析地址
            addres <<= 8;
            addres &= 0xFF00;
            addres |= DatPocket[3];				
            bytes = DatPocket[4];                               // 解析数据量
            bytes <<= 8;
            bytes &= 0xFF00;
            bytes |= DatPocket[5];				
    	    // 得到地址和数据量
            if(DatPocket[1] == 0x03)							// 读取寄存器数据
    	    {
				Read_RTU_4(addres,bytes);
   		    } 
            else if(DatPocket[1] == 0x06)						// 设置单寄存器
       	    {
                Write_RTU_4(addres,bytes);
   		    }
            else if(DatPocket[1] == 0x08)						// 设置单寄存器
       	    {
				DatPocket[0]=DeviceNum;
			  	DatPocket[1]=0x08;
			  	DatPocket[2]=NowWellNum;
				Pocket(DatPocket,3);
                Com_Send_String(DatPocket,5,ComFlag);
   		    }
            else if(DatPocket[1] == 0x09)						// 设置单寄存器
       	    {
				DateUpLoadFlag=DatPocket[2];
                //UpLoadWellDateTure(DatPocket[2]);
   		    }
			LED2_OFF;
        }   
    }
	DatLong=0x00;                                              	// 串口数据指针清零
}
//**************************************************************************************************************************
void Write_RTU_4(unsigned int firstadress,unsigned int bytes)
{
    RTUaddress4[firstadress*2]=bytes>>8;
    RTUaddress4[firstadress*2+1]=bytes;

	// 修改时间
	if(firstadress<3)
	{
		if(firstadress==0x00)
		{
			DS1302_Write_Byte(0x8e,0x00);		// 停止计时
			DS1302_Write_Byte(0x88,bytes);		// month
			DS1302_Write_Byte(0x8c,bytes>>8);	// year
			DS1302_Write_Byte(0x8e,0x80);		// 开始计时
		}
		if(firstadress==0x01)
		{
			DS1302_Write_Byte(0x8e,0x00);		// 停止计时
			DS1302_Write_Byte(0x84,bytes);		// hour
			DS1302_Write_Byte(0x86,bytes>>8);	// day
			DS1302_Write_Byte(0x8e,0x80);		// 开始计时
		}
		if(firstadress==0x02)
		{
			DS1302_Write_Byte(0x8e,0x00);		// 停止计时
			DS1302_Write_Byte(0x80,0x00);		// sec
			DS1302_Write_Byte(0x82,bytes>>8);	// min
			DS1302_Write_Byte(0x8e,0x80);		// 开始计时
		}
	}

	// DAC输出
	if(firstadress==14)
	{
		DAC_OUT_1(RTUaddress4[28]<<8 | RTUaddress4[29]);
	}
	if(firstadress==15)
	{
		DAC_OUT_2(RTUaddress4[30]<<8 | RTUaddress4[31]);
	}
	

	Com_Send_String(DatPocket,DatLong,ComFlag);
}
//**************************************************************************************************************************
void Read_RTU_4(unsigned int firstadress,unsigned int bytess)
{
   	xdata unsigned char i;
    
	DatPocket[0]=DeviceNum;
  	DatPocket[1]=0x03;
  	DatPocket[2]=2*bytess;
 	for(i=0;i<2*bytess;i++)
   	{
      	DatPocket[3+i]=RTUaddress4[2*firstadress+i];
    }
  	Pocket(DatPocket,3+2*bytess);
	Com_Send_String(DatPocket,5+2*bytess,ComFlag);
}
//**************************************************************************************************************************
void Pocket(unsigned char * Data,unsigned char D_Long)
{
    xdata unsigned int temp0;

    temp0 = CRC16(Data,0,D_Long);
    Data[D_Long+1]= temp0;
    Data[D_Long]  = temp0>>8;
}
//**************************************************************************************************************************
unsigned int CRC16(unsigned char *buf,int start,int cnt) 
{
	xdata int i,j;
	xdata unsigned int temp=0,flag=0,temp1=0;

	temp = 0xFFFF;
	for (i=start; i<cnt; i++)
	{
	    temp = temp ^ buf[i];
	    for (j=1; j<=8; j++)
	    {
	        flag = temp & 0x0001;
	        temp = temp >> 1;
	        if (flag) 
	            temp = temp ^ 0xA001;
	    }
    }
    // Reverse byte order. 
    temp1 = temp >> 8;
    temp  = (temp << 8) | temp1;
    temp &= 0xFFFF;

    return(temp);
}
//**************************************************************************************************************************
//THE END
//**************************************************************************************************************************

⌨️ 快捷键说明

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