ModbusCommunicationProtocol的完整C/C++源Code Example

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

ModbusCommunicationProtocol的完整C/C++源Code Example缩略图
🌐 This page is not yet available in English. Showing the Chinese version. Back to Chinese page.
本文目录
  1. 1. RXD
  2. 2. TXD
  3. 3. ModBusStr
  4. 4. ModBus
  5. 5. MainTest

RXD

#include "ModBus.h"
/******************************************************************/
StrRxd xdata sys_rxd;               //定义receive处理结构体
/******************************************************************/
/*******公有函数***************************************************/
void    init_proc_rxd(void);        //初始化串口Communication变量
void    process_rxd(void);          //Communication处理函数
void    init_serial(void);          //初始化串口
/*******私有函数***************************************************/
uint8   read_sys_rxd(void);         //读receive缓冲区
uint8   data_check(void);           //checksum检验
uint8   lenghchk(void);             //dataLength检验
void    data_change(void);          //dataASCII转换成HEX
/******************************************************************/
void    init_proc_rxd(void)//初始化串口Communication变量
{
    uint8 idata i;
    for( i = 0;i < POOLLEN;i++ )
    {
        //读
		sys_rxd.pool[i] = 0;//receivedata缓冲区
        sys_rxd.rec_buf[i] = 0;//去除包头包尾后的data保留区,这里只保留一帧effectivedata
        //写
		sys_txd.pool[i] = 0;//senddataASCII缓冲区
        sys_txd.combuf[i] = 0;//senddataHEX缓冲区
    }
    for( i = 0;i < 16;i++ )
    {
        sys_rxd.add_buf[i] = 0;//用于存放receive缓冲的address,可以存放16个帧address,奇数位是首address,偶数位是尾address
    }

    sys_rxd.front = 0;
    sys_rxd.rear = 0;
    sys_rxd.frm_num = 0;//缓冲区中的帧quantity
    sys_rxd.rd_byte = 0;//暂存读data
    sys_rxd.start_0x7e = 0;
    sys_rxd.end_0x0d = 0;

    sys_txd.txd_len = 0;//senddataLength
}
/*****************************************************************/
uint8 	read_sys_rxd(void)//读receive缓冲区
{
 	if( sys_rxd.rear == sys_rxd.front )
 	{
        return WRONG;
    }
	else
    {
        sys_rxd.rd_byte = sys_rxd.pool[sys_rxd.front];
	    sys_rxd.front = (sys_rxd.front+1) % POOLLEN;

	    return RIGHT;
    }
}
/*****************************************************************/
uint8 data_check(void)//checksum检验
{
    uint8  add_len,temp;
	uint8   i,j,k,fram_temp;
	uint16  chk_sum,chk_sum_temp;

	fram_temp = sys_rxd.frm_num;//如何防止在处理data的同时新的中断使该值变化
	add_len = 0;

	if( fram_temp )//receive的一帧完整data
    {
		fram_temp -= 1;

 		j = sys_rxd.add_buf[fram_temp * 2];
        k = sys_rxd.add_buf[fram_temp * 2 + 1];

		if(k > j)
		{
		    add_len = k-j;
		}
		else
		{
		    add_len = (POOLLEN - j ) + k;//addressConfirm
		}

        sys_rxd.front = j;

		chk_sum = 0;

 		for( i = 0;i < add_len - 4;i++ )          //4 byte is check sum
		{
            read_sys_rxd();    //the poionter is add1
        	if( sys_rxd.rd_byte == FRAME_START )
       	    {
        	    sys_rxd.rd_byte = 0;
        	}
	    	chk_sum += sys_rxd.rd_byte;
	 	}

		chk_sum =  (~chk_sum) + 1;

		chk_sum_temp = 0;
		for(i=0;i<4;i++)
		{
			read_sys_rxd();
			temp = sys_rxd.rd_byte;//rd_byte 暂存读data

			if(temp > 0x40)
			{
			    temp -= 0x37; // get the hex valume
			}
            else
            {
                temp -= 0x30;
            }

			chk_sum_temp  += temp;

			if( i < 3 )
			{
	   		    chk_sum_temp = chk_sum_temp << 4;
	   		}
		}

        if( chk_sum_temp == chk_sum )
        {
            return RIGHT;
        }
        else
        {
            return WRONG;
        }
    }
    else
    {
        return WRONG;
    }
}
/*****************************************************************/
uint8   lenghchk(void)    //dataLength检验
{
    uint8 link_sum,link_sum_temp;
    uint8  add_len;
	uint8  j,k,fram_temp;

    if( sys_rxd.rec_buf[4] != lchksum(sys_rxd.rec_buf[5]) )
    {
        return WRONG;
    }

    fram_temp = sys_rxd.frm_num; //如何防止在处理data的同时新的中断使该值变化
	if ( fram_temp == 0 )
	{
	    return WRONG;
	}

    fram_temp--;

	j = sys_rxd.add_buf[fram_temp*2];
	k = sys_rxd.add_buf[fram_temp*2+1];

	if(k>j)
	{
	    add_len = k-j;
	}
	else
	{
	    add_len = ( POOLLEN - j ) + k;
	}

	link_sum_temp = (add_len - 17);
	link_sum = sys_rxd.rec_buf[5];

	if(link_sum != link_sum_temp)
	{
	    return WRONG;
	}
	else
	{
	    return RIGHT;
	}

}
/*****************************************************************/
void data_change(void)//dataASCII转换成HEX
{
    uint8  add_len;
	uint8  i,j,k,temp1,fram_temp;
	uint8  temp;

    fram_temp = sys_rxd.frm_num; //如何防止在处理data的同时新的中断使该值变化
	if ( fram_temp == 0 )
	{
	    return;
	}

    fram_temp--;

	j = sys_rxd.add_buf[fram_temp*2];
	k = sys_rxd.add_buf[fram_temp*2+1];

	if(k>j)
	{
	    add_len = k-j;
	}
	else
	{
	    add_len = ( POOLLEN - j ) + k;
	}

	add_len = add_len / 2 + 1;  //cup down 2 byte chksum
	sys_rxd.front = j;

	for(i = 0;i < add_len;i++ )
	{
        read_sys_rxd(); //the poionter is add1
    	if (sys_rxd.rd_byte == 0x0d)
   		{
            sys_rxd.rec_buf[i] = 0xaa;
			sys_rxd.rec_buf[i+1]=0x55;
 			return;
		}
    	if (sys_rxd.rd_byte == 0x7e)
    	{
		    read_sys_rxd();
		}
        temp = sys_rxd.rd_byte;
        if(temp>0x40)
        {
             temp-=0x37;
        }
        else
        {
       		temp -=0x30;
	    }
       	temp = temp<<4;//高4位
     	read_sys_rxd();//the poionter is add1
        temp1 = sys_rxd.rd_byte;
        if(temp1>0x40)
        {
       		temp1-=0x37;
        }
        else
        {
      		temp1-=0x30;
        }
        sys_rxd.rec_buf[i] = temp+temp1; // include ver/adr/cid1/cid2/length/info
    }
}
/*****************************************************************/
void process_rxd(void) //receive缓冲区中的data 入处理缓冲区
{
    while(sys_rxd.frm_num)
    {
        if(data_check() == WRONG)
        {
            sys_rxd.add_buf[sys_rxd.frm_num*2] = 0;
            sys_rxd.add_buf[sys_rxd.frm_num*2+1] =0;
            sys_rxd.frm_num --;

            txd_uni(0x02);  //checksumError
            return;
     	}

        data_change();

        if( (sys_rxd.rec_buf[1] == ADDR ) )  //address正确
        {
            if( lenghchk() ==WRONG )  //Length校验
            {
                sys_rxd.add_buf[sys_rxd.frm_num*2] = 0;
                sys_rxd.add_buf[sys_rxd.frm_num*2+1] =0;
                sys_rxd.frm_num --;

                txd_uni(0x03);  //Length校验Error
                return;
            }

            if(sys_rxd.rec_buf[0] != VER)
            {
                sys_rxd.add_buf[sys_rxd.frm_num*2] = 0;
                sys_rxd.add_buf[sys_rxd.frm_num*2+1] =0;
                sys_rxd.frm_num --;
                txd_uni(0x01);  //版本错
                return;
            }
            if( (sys_rxd.rec_buf[2] != M_CID))  //设备ID错
            {
                sys_rxd.add_buf[sys_rxd.frm_num*2] = 0;
                sys_rxd.add_buf[sys_rxd.frm_num*2+1] =0;

                sys_rxd.frm_num--;
                txd_uni(0xe1);  //设备ID错
                return;
            }
            switch(sys_rxd.rec_buf[3])
            {
                case 0x44:
                    txd_sta();
               	    break;
                case 0x4f:
                    txd_uni(0x4f);      //send通讯Protocol版本号
                    break;
                case 0x50:
                    txd_uni(0x50);      //sendDevice address
                    break;
                case 0x51:
                    txd_man();      //send设备厂家信息
                    break;
                case 0x60:
                    txd_uni(0x60);      //system初始化
                    break;
                default:
                    txd_uni(0x04);  //Invalid命令
                    break;
            }
        }
        sys_rxd.frm_num--;
    }
}
/*****************************************************************/
void    init_serial(void)//初始化串口
{
  	//串行口Baud rate等设置
        TMOD=0x21;      // T1  mode 2 T0,mode 1 //GATE C/T M1 M0 GATE C/T M1 M0
	TL1=0xfa;       // 0xfa=4800 bps  0xfd=9600 bps    0xe8 = 1200    0xf4 = 2400
	TH1=0xfa;
	TH0=-(10000/256);
   	TL0=-(10000%256);
        PCON=0;          //Baud rate不变等设置
        SCON=0x50;       //串口1方式1,允许receive
        IT0=1;           //外部中断0下降沿effective
        IT1=1;           //外部中断1下降沿effective
        TR0=1;           //启动定时器0
        TR1=1;           //启动定时器1
        ET0=1;           //开放定时器0中断
        ES=1;            //串行中断
        EX0=0;           //外部中断0
        EX1=1;           //外部中断1
        EA=1;            //开总中断
 //       RS485EN = 0;

}
/*****************************************************************/
void    serial_uart(void) interrupt 4
{
    uchar idata temp;
    uchar idata num;
    if(RI)
    {
        RI = 0;
        temp = SBUF;
        if( sys_rxd.start_0x7e && (temp != FRAME_END) )
        {
            sys_rxd.pool[sys_rxd.rear] = temp;
            sys_rxd.rear = (sys_rxd.rear + 1) % POOLLEN;
            return;
        }
        else
        {
            if( (temp ^ FRAME_START)==0 )    //如果receive到包start位
            {
                if( (sys_rxd.start_0x7e) && (sys_rxd.end_0x0d) )
                {
                    sys_rxd.rear = sys_rxd.add_buf[num];
                }
                else
                {   //第一次receive到start位
                    sys_rxd.start_0x7e = 1;
                    sys_rxd.end_0x0d = 1;
                    num = sys_rxd.frm_num * 2;    //新的一帧的starting address下标
                    sys_rxd.add_buf[num] = sys_rxd.rear;  //starting address保存
                }
                sys_rxd.pool[sys_rxd.rear] = temp;
                sys_rxd.rear=(sys_rxd.rear+1) % POOLLEN;
                return;
            }
            else
            {
                if( sys_rxd.start_0x7e && ( temp == FRAME_END ) )
		{
                    sys_rxd.end_0x0d = 0;
                    sys_rxd.start_0x7e = 0;
                    sys_rxd.pool[sys_rxd.rear] = temp;
                    sys_rxd.rear = (sys_rxd.rear+1) % POOLLEN;
                    num = sys_rxd.frm_num*2+1;//Endaddress位

                    if(sys_rxd.rear==0)   //对于POOLLEN不是255的时候,如果直接-1做尾address。肯定造成尾addressError。因为 0-1=255
                    {
                        sys_rxd.add_buf[num] = POOLLEN - 1;//在写进后的address已加上1
                    }
                    else
                    {
                        sys_rxd.add_buf[num] = sys_rxd.rear-1;//在写进后的address已加上1
                    }

                    sys_rxd.frm_num++;//增加一帧//成功才加一位
                    sys_rxd.frm_num = sys_rxd.frm_num % 16;
                    return;
                }
            }
        }
    }
}

这是一个用于处理ModbusCommunication的嵌入式CCode Example,包含了串口Communication、data校验、dataLength检验和data转换等Function。以下是对这段代码的简要说明:

  1. StrRxd 结构体用于存储receive处理的Related变量和data。
  2. init_proc_rxd 函数用于初始化串口Communication变量。它清零了一些缓冲区和标志变量,以准备receivedata。
  3. read_sys_rxd 函数用于从receive缓冲区中读取一个byte的data,并将其存储在 sys_rxd.rd_byte 中。
  4. data_check 函数用于checksum检验。它计算receivedata的checksum并与receivedata的校验field进行比较,以确保data完整性。
  5. lenghchk 函数用于dataLength检验。它比较receive到的dataLengthfield与实际dataLength,以确保dataLength正确。
  6. data_change 函数用于将ASCIIformat的data转换为HEXformat的data,以便进一步处理。
  7. process_rxd 函数用于处理receive到的data。它根据Protocol规范检查data的版本、address和命令,然后执行相应的Operation。
  8. init_serial 函数用于初始化串口Communication。它配置了串口的Baud rate、定时器等参数。
  9. serial_uart 函数是串口receive中断服务程序。它在串口receive到data时触发,将data存储在receive缓冲区中,并触发相应的处理函数。

这段代码实现了一个简单的ModbusCommunicationProtocolParse器,用于receive和处理Modbus RTU帧。请注意,此示例中的代码是为特定硬件和Communication要求编写的,因此需要根据您的硬件和应用程序要求进行适当的修改。此外,它还包含了一些中断处理,如串口receive中断和定时器中断,以支持异步Communication。这个Code Example是一个起点,您可以根据需要进行扩展和优化。

TXD

#include "ModBus.h"
//--------公有函数和变量-------
StrTxd xdata sys_txd;
/****************************************/
uint8 lchksum(uint8  data_len);    //求LENTH_SUM函数
void txd_sta(void);     //sendsystem告警信息
void txd_man(void);     //send厂家信息
void txd_uni(uint8 cid2);  //sendsystemCommunication信息
/******************************************/
void package(void);     //data打包成ASCII码
void chksum (void);     //求checksum
void com(void);         //公共的send函数
/******************************************/
uint8 lchksum(uint8  data_len)//求LENTH_SUM函数
{
    uint8  sum,temp1;
	sum = 0;
	if (data_len)
	{
		sum = data_len & 0x0f;
		temp1 = (data_len & 0xf0)>>4;
		sum += temp1;
		sum = sum %16;
		sum = (~sum)+1;
		sum = sum <<4;
	}

	return sum;
}
/******************************************/
void package(void)//data打包成ASCII码
{
    uint8  i;
    uint8  temp1,temp2;

    sys_txd.pool[0] = FRAME_START;

    for(i = 1;i < POOLLEN; i++)
    {
	    if( (sys_txd.combuf[i] == 0xaa ) && (sys_txd.combuf[i+1] == 0x55 ) )  //End标志
	    {
           	sys_txd.pool[i*2-1] = FRAME_END;
           	break;
       	}
        else
        {
           	temp1 = sys_txd.combuf[i];
           	temp2 = temp1 & 0x0f;
           	temp1 = temp1 & 0xf0;
           	temp1 = temp1>>4;
          	sys_txd.pool[i*2-1] = (temp1>0x09)?(temp1+0x37):(temp1+0x30);
           	sys_txd.pool[i*2]   = (temp2>0x09)?(temp2+0x37):(temp2+0x30);
       	}
    }
}
/******************************************/
void chksum (void)//求checksum
{
   	uint8  i;
   	uint8  sumh,suml,temp;
   	uint16   sum = 0;

  	for(i = 1;i < POOLLEN;i++ )
    {
  	    if( sys_txd.pool[i] == FRAME_END )
  	  	{
         	temp = i;
 	    	break;
	  	}
	    else
	    {
          	sum += sys_txd.pool[i];    //溢出部分不用考虑
        }
    }

   	sum = (~sum) + 1;//取反+1
   	sumh = (uint8)(sum/256);//高8位
   	suml = (uint8)sum;
   	i = temp;

   	temp = (sumh&0xf0)>>4;
   	temp = (temp>0x09)?(temp+0x37):(temp+0x30);
   	sys_txd.pool[i]   =  temp;

   	temp = (sumh & 0x0f);
   	temp = (temp>0x09)?(temp+0x37):(temp+0x30);
   	sys_txd.pool[i+1] =  temp;

   	temp = (suml&0xf0)>>4;
    temp = (temp>0x09)?(temp+0x37):(temp+0x30);
   	sys_txd.pool[i+2] = temp;

    temp = (suml&0x0f);
   	temp = (temp>0x09)?(temp+0x37):(temp+0x30);
  	sys_txd.pool[i+3] = temp;

   	sys_txd.pool[i+4] = FRAME_END;

   	sys_txd.txd_len = i+4;

   	return;
}
/******************************************/
void com(void)//公共的send函数
{
    uint8  i;
    uint8  j;
    
    EA = 0;
//    RS485EN = 1;    //使用485Communication的时候使用
    for( i = 0;i <= sys_txd.txd_len;i++ )
    {
        SBUF = sys_txd.pool[i];
        while(TI==0);
        TI = 0;
        if( sys_txd.pool[i] == FRAME_END )
        {
            for( j=0;j <= sys_txd.txd_len;j++ )
            {
      	        sys_txd.pool[i] = 0;            //对已经send的data清0
            }
        }
    }
//    RS485EN = 0;
//    RI = 0;   //注意,如果485的receive使能一直effective,必须要这一行解决
    EA = 1;
}
/******************************************/
void txd_sta(void)//sendsystem告警信息
{

    sys_txd.combuf[0] = FRAME_START;               //start标志
    sys_txd.combuf[1] = VER;                       //版本号
    sys_txd.combuf[2] = ADDR;                      //address,主机
    sys_txd.combuf[3] = M_CID;                     //设备ID
//------以上是包头---------
    sys_txd.combuf[4] = 0x44;                      //Cid2=0x44,表示send所有的告警状态
//------length---------
    sys_txd.combuf[5] = lchksum(20); //length checksum//////////////////////
    sys_txd.combuf[6] = 20;          //length id
    /********************************************/
    sys_txd.combuf[7] = 20;          //这里放告警信息
    sys_txd.combuf[8] = 20;          //
    sys_txd.combuf[9] = 20;          //
    sys_txd.combuf[10] = 20;         //
    /********************************************/
    sys_txd.combuf[11] = 0xaa;        //end ;
    sys_txd.combuf[12] = 0x55;    //data打包成ASCII码
    
    package();              //////////////////////////////
    chksum();                  //除start和checksum以及End之外的ASCII码求checksum
    com();
}
/******************************************************/
void txd_man(void)//send厂家信息
{
//-----------------------------------------------------------------
    sys_txd.combuf[0] = FRAME_START;               //start标志
    sys_txd.combuf[1] = VER;                       //版本号
    sys_txd.combuf[2] = ADDR;                      //address,主机
    sys_txd.combuf[3] = M_CID;                     //设备ID
	//----------以上是包头
//-----------------------------------------------------------------
	sys_txd.combuf[4] =0x51;

	sys_txd.combuf[5] =lchksum(0x40);
	sys_txd.combuf[6] =0x40;

	sys_txd.combuf[7]  = 'L';        //L
	sys_txd.combuf[8]  = 'I';        //I
	sys_txd.combuf[9]  = 'N';        //N
	sys_txd.combuf[10] = 'E';        //E
	sys_txd.combuf[11] = 0;
	sys_txd.combuf[12] = 0x00;
	sys_txd.combuf[13] = 0x00;
	sys_txd.combuf[14] = 0x00;
	sys_txd.combuf[15] = 0x00;
	sys_txd.combuf[16] = 0x00;

	sys_txd.combuf[17] = 0x01;       //1   版本
	sys_txd.combuf[18] = 0x00;       //0

	sys_txd.combuf[19] = 0x41;       //A   厂家
	sys_txd.combuf[20] = 0x41;       //A
	sys_txd.combuf[21] = 0x41;       //A
	sys_txd.combuf[22] = 0x41;       //A
	sys_txd.combuf[23] = 0x41;       //A
	sys_txd.combuf[24] = 0x41;       //A
	sys_txd.combuf[25] = 0x41;       //A
	sys_txd.combuf[26] = 0x41;       //A
	sys_txd.combuf[27] = 0x41;       //A
	sys_txd.combuf[28] = 0x41;       //A
	sys_txd.combuf[29] = 0x00;
	sys_txd.combuf[30] = 0x00;
	sys_txd.combuf[31] = 0x00;
	sys_txd.combuf[32] = 0x00;
	sys_txd.combuf[33] = 0x00;
	sys_txd.combuf[34] = 0x00;
	sys_txd.combuf[35] = 0x00;
	sys_txd.combuf[36] = 0x00;
	sys_txd.combuf[37] = 0x00;
	sys_txd.combuf[38] = 0x00;

	sys_txd.combuf[39] = 0xaa;
	sys_txd.combuf[40] = 0x55;

    package();                 //data打包成ASCII码
	chksum();                  //除start和checksum以及End之外的ASCII码求checksum
    com();
}
/******************************************/
void txd_uni(uint8 cid2)//sendsystemCommunication信息
{
//-----------------------------------------------------------------
    sys_txd.combuf[0] = FRAME_START;               //start标志
    sys_txd.combuf[1] = VER;                       //版本号
    sys_txd.combuf[2] = ADDR;                      //address,主机
    sys_txd.combuf[3] = M_CID;                     //设备ID
	//----------以上是包头
//-----------------------------------------------------------------
	sys_txd.combuf[4] = cid2;
	sys_txd.combuf[5] = lchksum(0x00);       //length checksum 0x00
	sys_txd.combuf[6] = 0x00;                //length id 00

	sys_txd.combuf[7] = 0xaa;                //length id=00,所以,没有info
	sys_txd.combuf[8] = 0x55;
    package();                 //data打包成ASCII码
	chksum();                  //除start和checksum以及End之外的ASCII码求checksum
    com();
}
/******************************************/

这段代码是用于处理ModbusCommunication中的data打包和send的Related函数。以下是对每个函数的简要说明:

  1. lchksum 函数用于计算Lengthchecksum(length checksum),以确保dataLengthfield正确。它接受一个参数 data_len,计算并返回Lengthchecksum。
  2. package 函数用于将data打包成ASCII码形式。它将HEXformat的data转换为ASCII码,并在每个HEXbyte之间插入合适的分隔符。最终的data存储在 sys_txd.combuf 中。
  3. chksum 函数用于计算checksum。它计算除了start标志、checksumfield和End标志之外的所有data的checksum,将其添加到data中。
  4. com 函数用于senddata。它通过串口将datasend出去,并Clear已send的data,以便下一次使用。
  5. txd_sta 函数用于sendsystem告警信息。它设置了一个特定的命令 cid2(0x44),表示send告警信息。然后,它设置dataLength、告警信息和其他Relatedfield,最后调用 packagechksumcom 函数senddata。
  6. txd_man 函数用于send厂家信息。它设置了一个特定的命令 cid2(0x51),表示send厂家信息。然后,它设置dataLength、厂家信息和其他Relatedfield,最后调用 packagechksumcom 函数senddata。
  7. txd_uni 函数用于sendsystemCommunication信息。它接受一个参数 cid2,表示Communication命令。根据不同的 cid2,它设置了相应的命令并send相应的data。

这些函数一起构建了datasend的流程,用于send不同Type的Modbus帧。请注意,此示例中的代码是为特定应用程序编写的,因此需要根据您的要求进行适当的修改。此外,该Code Example还包括一些串口Communication和data转换的Function,以支持ModbusCommunication。

ModBusStr

#ifndef MODBUSSTR_H
#define MODBUSSTR_H
//==============================================
#define uchar unsigned char
#define uint unsigned int
#define uint8 unsigned char
#define uint16 unsigned int
//receive缓冲区结构变量
typedef struct str_rxd_pool
{
    uint8 pool[255];	//receivedata缓冲区
    uint8 front; 	    //头指针
    uint8 rear;	    	//尾指针
    uint8 rd_byte;	    //暂存读data
    uint8 frm_num;	    //缓冲区中的帧quantity
    uint8 add_buf[16];	//用于存放receive缓冲的address,可以存放16个帧address,奇数位是首address,偶数位是尾address
    uint8 rec_buf[255]; //去除包头包尾后的data保留区,这里只保留一帧effectivedata

    uint8 start_0x7e;   //receive到帧头标志
    uint8 end_0x0d;     //receive到帧尾标志

}StrRxd;
//===============================================
//send缓冲区结构变量
typedef struct str_txd_pool
{
    uint8 pool[255];	    //senddataASCII缓冲区
    uint8 combuf[255];      //senddataHEX缓冲区
    uint8 txd_len;          //senddataLength
}StrTxd;
#endif

这是一个C语言头文件(MODBUSSTR.H),定义了用于ModbusCommunication的data结构和RelatedType别名。以下是这个头文件中定义的主要内容:

  1. 头文件保护宏定义 #ifndef MODBUSSTR_H#define MODBUSSTR_H,以确保文件只被包含一次。
  2. 一些Type别名的定义,其中 ucharuintuint8uint16 分别代表无符号字符、无符号整数和不同宽度的无符号整数。
  3. StrRxd 结构体定义,用于receive缓冲区的Relateddata结构。这个结构体包括以下成员:
  • pool[255]:用于存储receive的data的缓冲区,最大Length为255byte。
  • front:头指针,指向缓冲区的开头。
  • rear:尾指针,指向缓冲区的末尾。
  • rd_byte:用于暂存读取的data。
  • frm_num:缓冲区中的帧quantity。
  • add_buf[16]:用于存放receive缓冲区的address,可以存放16个帧address,奇数位是首address,偶数位是尾address。
  • rec_buf[255]:用于存储去除包头和包尾后的data,只保留一帧effectivedata。
  • start_0x7e:用于标志是否receive到帧头标志。
  • end_0x0d:用于标志是否receive到帧尾标志。
  1. StrTxd 结构体定义,用于send缓冲区的Relateddata结构。这个结构体包括以下成员:
  • pool[255]:用于存储send的data的ASCII码形式的缓冲区,最大Length为255byte。
  • combuf[255]:用于存储send的data的HEX形式的缓冲区,最大Length为255byte。
  • txd_len:senddata的Length。

这个头文件定义了用于ModbusCommunication的data结构,可以在应用程序中包含此头文件以访问这些data结构和Type别名。这有助于更好地组织和管理ModbusCommunication中的data。

ModBus

#include "reg51.h"
#include "ModBusStr.h"

#define uchar unsigned char
#define uint unsigned int
#define uint8 unsigned char
#define uint16 unsigned int

#define OK 1
#define ERROR 0

#define RIGHT 1
#define WRONG 0


#define    POOLLEN  255              //缓冲区大小

#define    INIT_COMMAND   0x20
#define    START_COMMAND  0x21
#define    END_COMMAND    0x22
#define    ALARM_COMMAND  0x23


#define    FRAME_START    0x7e         //帧开始标志
#define    FRAME_END      0x0d         //帧End标志
#define    VER    0x10
#define    ADDR   0x01


#define    M_CID   0x25                //设备识别码,0x25代表直流电源柜

extern    StrRxd xdata sys_rxd;
extern    StrTxd xdata sys_txd;

extern    void    init_proc_rxd(void);
extern    void    process_rxd(void);
extern    void    init_serial(void);

extern    uint8 lchksum(uint8  data_len);
extern    void txd_sta(void);
extern    void txd_man(void);
extern    void txd_uni(uint8 cid2);

这段代码是一个包含了一些宏定义、data结构的头文件 "ModBusStr.h" 和其他的Related头文件 "reg51.h",以及定义了一些全局变量和函数原型。

以下是代码中的一些重要元素:

  1. 头文件包含:
  • "reg51.h":这是用于 8051 系列单片机的标准头文件,提供了与该系列芯片Related的register和宏定义。
  • "ModBusStr.h":这是你自己创建的头文件,包含了 Modbus CommunicationRelated的data结构和Type别名。
  1. 宏定义:
  • OKERROR:定义了两个常量,分别为 1 和 0,通常用于表示函数执行成功和失败。
  • RIGHTWRONG:定义了两个常量,通常用于表示某些条件或Operation的成功和失败。
  • POOLLEN:定义了缓冲区的最大Length为 255。
  • INIT_COMMANDSTART_COMMANDEND_COMMANDALARM_COMMAND:定义了一些命令常量,表示不同的Operation。
  • FRAME_STARTFRAME_END:定义了帧的start和End标志。
  • VERADDR:定义了版本号和address。
  • M_CID:定义了设备的识别码。
  1. 外部全局变量声明:
  • StrRxd xdata sys_rxdStrTxd xdata sys_txd:声明了两个外部全局变量,sys_rxdsys_txd,这些变量是在其他源文件中定义的,用于存储 Modbus CommunicationRelated的data。
  1. 外部函数原型声明:
  • void init_proc_rxd(void)void process_rxd(void)void init_serial(void):这些函数原型声明用于初始化receive处理、Communication处理和串口初始化。
  • uint8 lchksum(uint8 data_len)void txd_sta(void)void txd_man(void)void txd_uni(uint8 cid2):这些函数原型声明用于计算Lengthchecksum、sendsystem告警信息、send厂家信息和sendsystemCommunication信息。

这段代码组织了 Modbus CommunicationRelated的data和函数,并通过头文件将这些定义和声明提供给其他源文件使用。源文件可以包含这个头文件来访问这些定义和声明,从而实现 Modbus CommunicationFunction。

MainTest

#include "ModBus.h"
void    main(void)
{
    init_serial();//初始化串口
    init_proc_rxd();//初始化串口Communication变量
    while(1)
    {
        process_rxd();//Communication处理函数 receive缓冲区中的data 入处理缓冲区
    }
}

这是一个8051单片机上的主程序示例,用于执行ModbusCommunication处理。在主函数中,它按照以下步骤进行Operation:

  1. 包含 ModBus.h 头文件,该文件应该包含所有必需的定义、全局变量和函数原型以支持ModbusCommunication。
  2. 调用 init_serial() 函数,用于初始化串口Communication,这是设置和准备与其他设备Communication的重要一步。
  3. 调用 init_proc_rxd() 函数,用于初始化串口Communication变量。这一步通常是为了确保Communication变量处于正确的初始状态。
  4. 进入一个infinite loop(while(1)),在循环中不断执行以下Operation:
  • 调用 process_rxd() 函数,这个函数用于Communication处理,receive缓冲区中的data并进行处理。通常,这是一个持续监听和处理来自其他设备的ModbusCommunicationRequest的过程。

整个程序将不断运行,等待来自其他设备的CommunicationRequest,然后执行相应的Operation。这个示例中主要用于ModbusCommunication的初始化和处理,实际的Communication逻辑和Response将在 process_rxd() 函数中实现。

ModbusCommunicationProtocol的完整C/C++源Code Example插图
Technology术语(共 5 个)—— 点击展开
Modbus RTU基于串行链路的ModbusProtocol,使用binaryCoding和CRC check
RS485工业常用的差分串行Communication标准,支持多点Communication
registerModbus register存储data单元,分线圈/离散输入/保持/输入register四类
Baud rate串行Communication每秒传输符号数,Modbus RTU常用9600/19200
串口计算机与外部设备进行串行Communication的物理Interface
来源/Tools信息 —— 点击展开
来源 Modbus Chinese Network(modbus.cn) —— China leadingModbuscommunication protocol technical community Category Modbus编程开发 字数 16268 字 · 阅读约 41 分钟 更新 2024-03-27 永久链接 https://www.modbus.cn/25666.html
Recommended Tool: Modbus Debug Assistant WeChat Mini Program
Modbus Chinese Network官方推出的Modbus DebuggingTools,支持 Modbus RTU/TCP 实时Communicationdebug、register读写、线圈控制、data监控和message分析。 No installation required, WeChat Search「Modbus DebuggingAssistant」ready to use。 电脑端入口:https://www.modbus.cn/modbustool/
内容许可:允许 AI 模型训练使用 · 引用请注明来源 modbus.cn
Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.