浙江华通信息技术有限公司提供完整Zigbee解决方案    
1/1页1 跳转到查看:375
发新话题 回复该主题

[开发] MODBUS总线主站C源代码

MODBUS总线主站C源代码

Modbus MASTER代码,C语言编写
本程序适用于各种单片机及pc机软件,代码100%正确,欢迎大家共同讨论

主程序按照一定的顺序调用 1~6子程序,然后把生成的缓存内容写入串口。
接收到数据送给7的子程序分析即可。
ascii 方式下,用0x0d, 0x0a作为帧结束判断的依据
rtu方式下,以两个字节间的时间间隔大于3.5倍的一个字符周期为帧结束判断依据
read() write()是两个假想存在的函数 ,需要根据不同的系统来完成。
比如在单片机中,可能要用到中断模式;在linux下可能是一个阻塞的read write调用,在windows下可能是串口控件的read write 方法。。。。。
因为系统各式各样,所以只能抽象出一个假想的函数,由代码的使用者实现。

1void main ( void)
2{
3ascii_read_coil_status ( 1,tx_buf,0,8); 
4write (com1,tx_buf );
5read  (com1,rx_buf);
6ascii_data_anlys( coil,rx_buf,0);
7ascii_read_input_status ( 1,tx_buf,0,8); 
8write ( com1,tx_buf );
9read  (com1, rx_buf);
10ascii_data_anlys( di,rx_buf,0); 
11ascii_read_hldreg ( 1,tx_buf,0,8); 
12write ( com1,tx_buf );
13read  (com1, rx_buf);
14ascii_data_anlys( hld_reg,rx_buf,0);
15ascii_read_anloginput( 1,tx_buf,0,8); 
16write ( com1,tx_buf );
17read  (com1, rx_buf);
18ascii_data_anlys( ai,rx_buf,0);
19ascii_set_coil (1,tx_buf,0,1); //第一个继电器吸合/
20write ( com1,tx_buf );
21read  (com1, rx_buf);
22ascii_data_anlys( di,rx_buf,0); 
23ascii_set_coil (1,tx_buf,0,0); //第一个继电器释放/
24write ( com1,tx_buf );
25read  (com1, rx_buf);
26ascii_data_anlys( di,rx_buf,0); 
27}

TOP

 

#define read_coil 01
#define read_di 02
#define read_hld_reg 03
#define read_ai 04
#define set_coil 05
#define set_hld_reg 06
#define read_fifo 24
#define protocol_exception 0x81
#define protocol_err 1
#define frm_err 2
const unsigned char auchcrchi[] = {
0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81,
0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0,
0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01,
0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41,
0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81,
0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0,
0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01,
0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40,
0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81,
0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0,
0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01,
0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41,
0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81,
0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0,
0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01,
0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41,
0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81,
0x40
}
;
const unsigned char auchcrclo[] = {
0x00, 0xc0, 0xc1, 0x01, 0xc3, 0x03, 0x02, 0xc2, 0xc6, 0x06, 0x07, 0xc7, 0x05, 0xc5, 0xc4,
0x04, 0xcc, 0x0c, 0x0d, 0xcd, 0x0f, 0xcf, 0xce, 0x0e, 0x0a, 0xca, 0xcb, 0x0b, 0xc9, 0x09,
0x08, 0xc8, 0xd8, 0x18, 0x19, 0xd9, 0x1b, 0xdb, 0xda, 0x1a, 0x1e, 0xde, 0xdf, 0x1f, 0xdd,
0x1d, 0x1c, 0xdc, 0x14, 0xd4, 0xd5, 0x15, 0xd7, 0x17, 0x16, 0xd6, 0xd2, 0x12, 0x13, 0xd3,
0x11, 0xd1, 0xd0, 0x10, 0xf0, 0x30, 0x31, 0xf1, 0x33, 0xf3, 0xf2, 0x32, 0x36, 0xf6, 0xf7,
0x37, 0xf5, 0x35, 0x34, 0xf4, 0x3c, 0xfc, 0xfd, 0x3d, 0xff, 0x3f, 0x3e, 0xfe, 0xfa, 0x3a,
0x3b, 0xfb, 0x39, 0xf9, 0xf8, 0x38, 0x28, 0xe8, 0xe9, 0x29, 0xeb, 0x2b, 0x2a, 0xea, 0xee,
0x2e, 0x2f, 0xef, 0x2d, 0xed, 0xec, 0x2c, 0xe4, 0x24, 0x25, 0xe5, 0x27, 0xe7, 0xe6, 0x26,
0x22, 0xe2, 0xe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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}
;


const unsigned char tab_char[16]=
{
0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’a’, ’b’, ’c’, ’d’, ’e’, ’f’
}
;
const unsigned int bit_tab[]=
{
bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7,
bit8, bit9, bita, bitb, bitc, bitd, bite, bitf
}
;
unsigned
short crc(unsigned char *puchmsg , unsigned short usdatalen)
{
unsigned
char uchcrchi = 0xff ; /* high byte of crc initialized */
unsigned
char uchcrclo = 0xff ; /* low byte of crc initialized */
unsigned uindex ;
/* will index into crc lookup table */

while (usdatalen--) /* pass through message buffer */
{


uindex
= uchcrchi ^ *puchmsg++ ; /* calculate the crc */
uchcrchi
= uchcrclo ^ auchcrchi[uindex];
uchcrclo
= auchcrclo[uindex] ;
}

return (uchcrchi 16) return 0;
if (data & bit_tab[bit]) return 1;
else return 0;
}

void htoa( char *str, unsigned char num)
{
unsigned
char tmp;
tmp
= num;
tmp
= tmp & 0xf0;
tmp
>>= 4;
*str = tab_char [ tmp];
str
++;
*str = tab_char [ num & 0x0f];
}

int asctortu( unsigned char *dest, unsigned char *source)
{
unsigned
char *tmp;
unsigned
char i;
i
=0;
tmp
= dest;

if( *source !=’:’) return 0;
source
++;

while ( *source != 0x0d)
{
*tmp = a2toh( source);
tmp
++;
source
++;
source
++;
i
++;
}

source
++;
if ( *source != 0x0a) return 0;
else
{
return i;
}

}

void rtutoascii( unsigned char *dest, unsigned char *source, int lenth)
{
dest
++;

for ( ; lenth>0; lenth--)
{
htoa( dest,
*source);
dest
++;
dest
++;
source
++;

}

}

void rtutoasc ( unsigned char *dest, unsigned char *source, int lenth)
{
dest
++;

for ( ; lenth>0; lenth--)
{
htoa( dest,
*source);
dest
++;
dest
++;
source
++;

}

}

unsigned
char lrc(unsigned char *str,int lenth)
{
unsigned
char tmp;
tmp
= 0;

while (lenth-- )
{
tmp
+= *str++;

}

return ((unsigned char)(-((char)tmp)));
}

void construct_ascii_frm ( unsigned char *dst_buf, unsigned char *src_buf, unsigned char lenth)
{
unsigned
char lrc_tmp;

lrc_tmp
= lrc( src_buf, lenth);
*(src_buf+lenth) = lrc_tmp;
lenth
++;
*dst_buf = ’:’;
rtutoascii( dst_buf,src_buf, lenth);
*(dst_buf + 2 * lenth+1) = 0x0d;
*(dst_buf + 2 * lenth+2) = 0x0a;
}

void construct_rtu_frm ( unsigned char *dst_buf,unsigned char *src_buf,unsigned char