modbus.c ( File view )

  • By Karma_w 2015-06-09
  • View(s):16
  • Download(s):1
  • Point(s): 1
			#include "modbus.h"
#include "timer.h"
#include "usart.h"
#include "adc.h"

u8 volatile uart_rev_flag=0;	//接收完成标志
u8 slave_id=0x01;	//从机地址
u16 uart_rev_count=0;	//ADU接收字节个数
u16 uart_send_count=0;	//ADU发送数据个数
u8 uart_send_buff[MB_SEND_BUF_MAX]={
0
};	//modbus发送数据
extern u8 uart_rev_buff[Rev_Buff_Max];     //接收缓冲
u16 MB_Reg[MB_REG_MAX];	//modbus寄存器数组,电流读取值

void MB_Init()
{

	Timerx_Init(6,7199);	//0.5ms
	TIM3->CR1&=~(0x01);

}

/*
 * info:CRC16校验
 * para:*ptr 校验数据 len 校验数据长度
*/
u16 getCRC16(volatile u8 *ptr,u8 len)
{

    u8 i;
    u16 crc = 0xFFFF;
    if(len==0)
    {

        len = 1;
    
}
    while(len--)
    {

        crc ^= *ptr;
        for(i=0; i<8; i++)
    	{

            if(crc&1)
        	{

                crc >>= 1;
                crc ^= 0xA001;
        	
}
        	else
    		{

                crc >>= 1;
        	
}
        
}
        ptr++;
    
}
    return(crc);

}

/*
 * info : 清空接收缓存
*/
void flush_revBuff(void)
{

	u16 i;
	for (i = 0; i < Rev_Buff_Max; ++i)
	{

		uart_rev_buff[i]=0;
	
}

}

void flush_sendBuff(void)
{

	u16 i;
	for (i = 0; i < MB_SEND_BUF_MAX; ++i)
	{

		uart_send_buff[i]=0;
	
}

}

/*
 * info:协议栈解析
*/
void MB_Poll(void)
{

    if(uart_rev_flag == 1)//接收完成标志=1处理,否则退出
    {

        if(uart_rev_buff[0] == slave_id)//地址错误不应答
    	{

            u16 crc;
            u8 crc_det[2];
            crc = getCRC16(uart_rev_buff,uart_rev_count-2); 	//crc校验
            crc_det[0] = (u8)crc;  //低八位
            crc_det[1] = (u8)(crc >> 8); //高八位
            if((uart_rev_buff[uart_rev_count-1] == crc_det[1])&&(uart_rev_buff[uart_rev_count-2] == crc_det[0]))//crc校验错误不应答
        	{

                switch(uart_rev_buff[1])
            	{

                    case 0x03: //读多个寄存器
	            		MB_Read_MutiReg();
	                    break;
                    case 0x06: //写单个寄存器
                    	MB_Write_SingleReg();
                        break;
                    default:
                    	uart_send_char(0x03);	//请求命令错误
                
}
    		
}
    		else
    		{

    			//uart_send_char(0x02);	//crc校验错误
    		
}
    	
}
    	else
    	{

    		//uart_send_char(0x01);	//从机地址错误
    	
}

        flush_revBuff();	//清空接收缓存
				uart_rev_count=0;
				uart_rev_flag = 0;
				//USART1->CR1|=1<<8;
				//USART1->CR1|=1<<5;
    
}

}

/*
 * info : 从机响应数据帧发送
 * para : dat 数据起始地址  send_count 发送数据长度
*/
void MB_Reply(u8* dat,u16 send_count)
{

	u16 i;
	for (i = 0; i < send_count; ++i)
	{

		uart_send_char(*(dat+i));
	
}

}

void GetVonvertionValue(void)
{

	u8 i;
	for(i=0;i<MB_REG_MAX;++i)
	{

		MB_Reg[i] = Get_Adc(i);
	
}

}

/*
 * info : 读取多个寄存器,即16位变量
 * 数据帧规则-->地址、寄存器数量Big endian : 先高后低
 *           -->其他 Little dndian :先低后高
*/
void MB_Read_MutiReg(void)
{

	u16 i,index_buf,crc_det;
	u16 regAddr=0;	//线圈起始地址
	u16 regReadCnt=0;	//读取寄存器数量
	u16 readCnt;

	flush_sendBuff();
	GetVonvertionValue();	//读取电流值

	regAddr=(uart_rev_buff[2]<<8)+uart_rev_buff[3]-MB_REG_START;	//减去起始地址
	regReadCnt=(uart_rev_buff[4]<<8)+uart_rev_buff[5];
	if ((regAddr+regReadCnt)<=MB_REG_MAX)	//防止modbus寄存器访问越界
	{

		uart_send_buff[0]=slave_id;	//从机地址
		uart_send_buff[1]=0x03;	//功能码
		uart_send_buff[2]=regReadCnt*2;	//字节数-->16位变量字符发送
		index_buf=3;	//从第三个字节开始
		for (readCnt = 0 ,i = regAddr; readCnt < regReadCnt; ++readCnt)	//读取寄存器状态
		{

			//先高后低
			uart_send_buff[index_buf]=(u8)(MB_Reg[i]>>8);
			index_buf++;
			uart_send_buff[index_buf]=(u8)MB_Reg[i];
			index_buf++;
			++i;
		
}
		crc_det=getCRC16(uart_send_buff,index_buf);
		uart_send_buff[index_buf]=(u8)(crc_det);
		index_buf++;
		uart_send_buff[index_buf]=(u8)(crc_det>>8);
		uart_send_count=1+1+1+regReadCnt*2+2;	//从机地址+功能码+字节数+寄存器状态+crc校验
		MB_Reply(uart_send_buff,uart_send_count);	//从机响应
	
}
	else
	{

		uart_send_char(0x04);	//访问数组越界
	
}

}
/*
 * info : 写单个寄存器,即16位变量
 * @单个寄存器的数值先高后低
*/
void MB_Write_SingleReg(void)
{

	u16 regAddr,index_buf,crc_det,regValue;

	flush_sendBuff();

	regAddr=(uart_rev_buff[2]<<8)+uart_rev_buff[3]-MB_REG_START;	//减去起始地址
	regValue=(uart_rev_buff[4]<<8)+uart_rev_buff[5];
	if (regAddr<MB_REG_MAX)	//防止modbus寄存器访问越界
	{

		MB_Reg[regAddr]=regValue;	//修改寄存器的值

		uart_send_buff[0]=slave_id;	//从机地址
		uart_send_buff[1]=0x06;	//功能码
		uart_send_buff[2]=uart_rev_buff[2];//寄存器地址
		uart_send_buff[3]=uart_rev_buff[3];
		uart_send_buff[4]=uart_rev_buff[4];//寄存器值
		uart_send_buff[5]=uart_rev_buff[5];

		index_buf=6;//数据校验个数

		crc_det=getCRC16(uart_send_buff,index_buf);
		uart_send_buff[6]=(u8)(crc_det);
		uart_send_buff[7]=(u8)(crc_det>>8);
		uart_send_count=1+1+2+2+2;	//从机地址+功能码+寄存器地址+寄存器值+crc校验
		MB_Reply(uart_send_buff,uart_send_count);	//从机响应
	
}
	else
	{

		uart_send_char(0x04);	//访问数组越界
	
}

}
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
01.97 kB
01.97 kB
adc.c2.48 kB2015-06-03 11:04
adc.h693.00 B2011-01-12 21:19
01.97 kB
dma.c1.31 kB2010-06-07 16:30
dma.h378.00 B2010-06-07 16:30
01.97 kB
exti.c1.71 kB2011-01-12 20:50
exti.h628.00 B2011-01-12 20:51
01.97 kB
key.c1.73 kB2013-04-03 14:18
key.h839.00 B2011-01-12 20:46
01.97 kB
FONT.H15.78 kB2010-06-21 11:18
ILI93xx.c38.75 kB2012-10-25 23:18
LCD.h8.94 kB2012-10-25 22:28
01.97 kB
led.c910.00 B2013-04-03 14:18
led.h288.00 B2010-05-27 17:30
01.97 kB
FONT.H15.78 kB2010-03-19 17:16
oled.c6.70 kB2011-01-12 21:02
oled.h1.59 kB2011-01-12 21:02
01.97 kB
rtc.c7.75 kB2011-01-12 22:04
rtc.h1.35 kB2011-01-12 21:15
01.97 kB
timer.c2.10 kB2015-06-03 11:38
timer.h724.00 B2011-01-12 20:59
01.97 kB
wdg.c2.04 kB2011-01-12 20:56
wdg.h756.00 B2011-01-12 20:56
01.97 kB
wkup.c1.85 kB2011-01-12 21:18
wkup.h728.00 B2011-01-12 21:18
keilkilll.bat399.00 B2011-04-23 10:24
README.TXT377.00 B2012-07-19 22:20
01.97 kB
01.97 kB
delay.c2.02 kB2013-04-03 12:14
delay.h905.00 B2013-04-03 12:14
01.97 kB
cortexm3_macro.h2.17 kB2009-05-07 14:42
stm32f10x_conf.h6.05 kB2009-05-07 14:42
stm32f10x_it.h4.42 kB2012-12-18 12:35
stm32f10x_map.h27.63 kB2009-05-07 14:42
stm32f10x_nvic.h18.75 kB2009-05-07 14:42
stm32f10x_type.h3.31 kB2009-05-07 14:42
sys.c6.69 kB2013-04-03 12:13
sys.h3.78 kB2013-04-03 12:13
01.97 kB
usart.c3.35 kB2015-06-03 11:28
usart.h1,010.00 B2015-06-03 10:57
01.97 kB
adc.crf34.24 kB2015-06-03 11:04
adc.d298.00 B2015-06-03 11:04
adc.o39.02 kB2015-06-03 11:04
delay.crf31.84 kB2015-06-03 10:54
delay.d286.00 B2015-06-03 10:54
delay.o37.45 kB2015-06-03 10:54
ili93xx.crf80.82 kB2015-06-03 10:57
ili93xx.d592.00 B2015-06-03 10:57
ili93xx.o79.87 kB2015-06-03 10:57
JLinkLog.txt59.16 kB2015-06-03 11:43
JLinkSettings.ini578.00 B2013-04-03 14:19
key.crf34.17 kB2015-06-03 10:54
key.d332.00 B2015-06-03 10:54
key.o40.01 kB2015-06-03 10:54
led.crf33.41 kB2015-06-03 10:54
led.d298.00 B2015-06-03 10:54
led.o38.35 kB2015-06-03 10:54
modbus.c5.44 kB2015-06-03 11:38
modbus.crf40.08 kB2015-06-03 11:38
modbus.d478.00 B2015-06-03 11:38
modbus.h404.00 B2015-06-03 10:54
modbus.o47.92 kB2015-06-03 11:38
rtc.crf41.95 kB2015-06-03 10:57
rtc.d434.00 B2015-06-03 10:57
rtc.o48.63 kB2015-06-03 10:57
STM32F10x.d28.00 B2015-06-03 10:54
STM32F10x.lst34.54 kB2015-06-03 10:54
stm32f10x.o5.58 kB2015-06-03 10:54
STM32F10x.s11.04 kB2009-12-22 19:16
sys.crf35.34 kB2015-06-03 10:54
sys.d264.00 B2015-06-03 10:54
sys.o42.51 kB2015-06-03 10:54
TEST.axf37.47 kB2015-06-03 11:43
TEST.build_log.htm1.36 kB2015-06-03 11:44
test.c601.00 B2015-06-03 11:41
test.crf51.11 kB2015-06-03 11:41
test.d792.00 B2015-06-03 11:41
TEST.hex13.17 kB2015-06-03 11:43
TEST.htm43.73 kB2015-06-03 11:43
TEST.lnp339.00 B2015-06-03 11:43
TEST.map64.78 kB2015-06-03 11:43
test.o54.43 kB2015-06-03 11:41
TEST.Opt2.03 kB2013-04-03 14:21
TEST.plg249.00 B2014-03-16 19:17
TEST.sct479.00 B2014-02-21 11:13
TEST.tra1.38 kB2014-02-21 11:13
TEST.Uv23.21 kB2013-04-03 14:21
TEST.uvgui.Administrator69.44 kB2013-04-03 14:20
TEST.uvgui.wjc72.16 kB2015-06-03 11:44
TEST.uvgui.WuJiChao67.42 kB2014-03-16 19:17
TEST.uvgui_wjc.bak72.08 kB2015-06-03 11:42
TEST.uvgui_WuJiChao.bak67.35 kB2014-02-21 11:21
TEST.uvopt10.12 kB2015-06-03 11:44
TEST.uvproj16.86 kB2015-06-03 11:42
TEST_Target8.60 kB2015-06-03 11:43
TEST_uvopt.bak10.12 kB2015-06-03 11:42
TEST_uvproj.bak17.39 kB2015-06-02 14:35
timer.crf38.84 kB2015-06-03 11:38
timer.d462.00 B2015-06-03 11:38
timer.o43.37 kB2015-06-03 11:38
usart.crf38.59 kB2015-06-03 11:28
usart.d388.00 B2015-06-03 11:28
usart.o43.36 kB2015-06-03 11:28
wdg.crf34.45 kB2015-06-03 10:54
wdg.d330.00 B2015-06-03 10:54
wdg.o41.05 kB2015-06-03 10:54
wkup.crf34.14 kB2015-06-03 10:54
wkup.d378.00 B2015-06-03 10:54
wkup.o41.15 kB2015-06-03 10:54
...
Sponsored links

modbus.c (571.52 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D