发表于:2008/10/21 16:43:00
#0楼
/*
* 适用于各种单片机及pc机软件,代码100%正确,
*/
#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, 0xe3, 0x23, 0xe1, 0x21, 0x20, 0xe0, 0xa0, 0x60, 0x61, 0xa1, 0x63, 0xa3, 0xa2,
0x62, 0x66, 0xa6, 0xa7, 0x67, 0xa5, 0x65, 0x64, 0xa4, 0x6c, 0xac, 0xad, 0x6d, 0xaf, 0x6f,
0x6e, 0xae, 0xaa, 0x6a, 0x6b, 0xab, 0x69, 0xa9, 0xa8, 0x68, 0x78, 0xb8, 0xb9, 0x79, 0xbb,
0x7b, 0x7a, 0xba, 0xbe, 0x7e, 0x7f, 0xbf, 0x7d, 0xbd, 0xbc, 0x7c, 0xb4, 0x74, 0x75, 0xb5,
0x77, 0xb7, 0xb6, 0x76, 0x72, 0xb2, 0xb3, 0x73, 0xb1, 0x71, 0x70, 0xb0, 0x50, 0x90, 0x91,
0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9c, 0x5c,
0x5d, 0x9d, 0x5f, 0x9f, 0x9e, 0x5e, 0x5a, 0x9a, 0x9b, 0x5b, 0x99, 0x59, 0x58, 0x98, 0x88,
0x48, 0x49, 0x89, 0x4b, 0x8b, 0x8a, 0x4a, 0x4e, 0x8e, 0x8f, 0x4f, 0x8d, 0x4d, 0x4c, 0x8c,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
0x40
};
const unsigned char char_tab[128]=
{
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, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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,
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 lenth)
{
unsigned short crc_tmp;
crc_tmp = crc(src_buf, lenth);
*(src_buf+lenth) = crc_tmp >> 8 ;
*(src_buf+lenth+1) = crc_tmp & 0xff;
lenth++;
lenth++;
while ( lenth--)
{
*dst_buf = *src_buf;
dst_buf++;
src_buf++;
}
}
/*
1 读取继电器状态
发送:
*/
int ascii_read_coil_status ( unsigned char board_adr, unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256],tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_coil;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_ascii_frm (com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_read_coil_status ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_coil;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_rtu_frm ( com_buf, tmp, tmp_lenth);
return 8;
}
/*
2 读取开关量输入
发送:
*/
int ascii_read_input_status ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_di;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_ascii_frm ( com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_read_input_status ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_di;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_rtu_frm ( com_buf,tmp,tmp_lenth);
return 8;
}
/*
3 读取保持寄存器
发送:
*/
int ascii_read_hldreg ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_hld_reg;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_ascii_frm ( com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_read_hldreg ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_hld_reg;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_rtu_frm ( com_buf,tmp,tmp_lenth);
return 8;
}
// 4 发送读取模拟量输入
int ascii_read_anloginput ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_ai;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_ascii_frm ( com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_read_anloginput ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256],tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_ai;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_rtu_frm ( com_buf,tmp,tmp_lenth);
return 8;
}
/*
5 设置继电器
发送:status =0 继电器释放 否则继电器吸合,address 为吸合的继电器编号,0为第一个继电器,依次类推
*/
int ascii_set_coil ( unsigned char board_adr,unsigned char *com_buf,int start_address,int status )
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = set_coil ;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
if ( status )
{
tmp[4] = 0xff;
tmp[5] = 0;
}
else
{
tmp[4]= 0;
tmp[5]= 0;
}
tmp_lenth = 6;
construct_ascii_frm ( com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_set_coil ( unsigned char board_adr,unsigned char *com_buf,int start_address,int status )
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = set_coil ;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
if ( status )
{
tmp[4] = 0xff;
tmp[5] = 0;
}
else
{
tmp[4]= 0;
tmp[5]= 0;
}
tmp_lenth = 6;
construct_rtu_frm ( com_buf, tmp, tmp_lenth);
return 8 ;
}
/*
6 设置保持寄存器
*/
int ascii_set_hldreg( unsigned char board_adr,unsigned char *com_buf,int start_address, unsigned int value )
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = set_hld_reg;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(value);
tmp[5] = low(value);
tmp_lenth = 6;
construct_ascii_frm ( com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_set_hldreg( unsigned char board_adr,unsigned char *com_buf, int start_address, unsigned int value )
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = set_hld_reg;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(value);
tmp[5] = low(value);
tmp_lenth = 6;
construct_rtu_frm ( com_buf, tmp, tmp_lenth);
return 8 ;
}
/*
7
接收分析:
dest_p 接收到数据指针
sourc_p 串口接收缓冲区指针
data_start_address 开始地址
*/
/* rtu 接收分析 */
int rtu_data_anlys( int *dest_p, unsigned char *source_p, int data_start_address, int fr_lenth)
{
unsigned short crc_result, crc_tmp;
unsigned char tmp1, tmp2, shift;
crc_tmp = *(source_p + fr_lenth-2); // crc 第一字节
crc_tmp = crc_tmp * 256 + *( source_p+fr_lenth-1); // crc 值
crc_result = crc(source_p, fr_lenth-2); // 计算crc 值
if ( crc_tmp != crc_result ) // crc 校验正确
{
hld_reg[0x31]++;
return -1;
}
switch ( *(source_p+1) ) // 功能码
{
case read_coil: /*读取继电器状态 */
for ( tmp1=0; tmp1>= 1;
}
}
break;
case read_di: /*读取开关量输入*/
for ( tmp1=0; tmp1>=1;
}
}
break;
case read_hld_reg: /*读取保持寄存器*/
for ( tmp1=0; tmp1>= 1;
}
}
break;
case read_di: /*读取开关量输入*/
for ( tmp1=0; tmp1 {
shift = 1;
for (tmp2=0; tmp2>=1;
}
}
break;
case read_hld_reg: /*读取保持寄存器*/
for (tmp1=0; tmp1 {
*(dest_p + data_start_address+ tmp1/2) = tmp[tmp1+3]*256 + tmp[tmp1+4] ;
}
break ;
case 4: /*读取模拟量输入*/
for (tmp1=0; tmp1 {
*(dest_p+data_start_address+ tmp1/2) = tmp[tmp1+3]*256 + tmp[tmp1+4] ;
}
break;
case protocol_exception:
return -1*protocol_err;
//break;
default:
break;
}
return 0;
}
/*
主程序按照一定的顺序调用 1~6子程序,然后把生成的缓存内容写入串口。
接收到数据送给7的子程序分析即可。
ascii 方式下,用0x0d, 0x0a作为帧结束判断的依据
rtu方式下,以两个字节间的时间间隔大于3.5倍的一个字符周期为帧结束判断依据
read() write()是两个假想存在的函数 ,需要根据不同的系统来完成。
比如在单片机中,可能要用到中断模式;在linux下可能是一个阻塞的read write调用,在windows下可能是串口控件的read write 方法。。。。。
因为系统各式各样,所以只能抽象出一个假想的函数,由代码的使用者实现。
*/
/*void main ( void)
{
ascii_read_coil_status ( 1,tx_buf,0,8);
write (com1,tx_buf );
read (com1,rx_buf);
ascii_data_anlys( coil,rx_buf,0);
ascii_read_input_status ( 1,tx_buf,0,8);
write ( com1,tx_buf );
read (com1, rx_buf);
ascii_data_anlys( di,rx_buf,0);
ascii_read_hldreg ( 1,tx_buf,0,8);
write ( com1,tx_buf );
read (com1, rx_buf);
ascii_data_anlys( hld_reg,rx_buf,0);
ascii_read_anloginput( 1,tx_buf,0,8);
write ( com1,tx_buf );
read (com1, rx_buf);
ascii_data_anlys( ai,rx_buf,0);
ascii_set_coil (1,tx_buf,0,1); //第一个继电器吸合/
write ( com1,tx_buf );
read (com1, rx_buf);
ascii_data_anlys( di,rx_buf,0);
ascii_set_coil (1,tx_buf,0,0); //第一个继电器释放/
write ( com1,tx_buf );
read (com1, rx_buf);
ascii_data_anlys( di,rx_buf,0);
}*/
----------------------------------------------
此篇文章从博客转发
原文地址: Http://blog.gkong.com/more.asp?id=64954&Name=jiayoua
* 适用于各种单片机及pc机软件,代码100%正确,
*/
#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, 0xe3, 0x23, 0xe1, 0x21, 0x20, 0xe0, 0xa0, 0x60, 0x61, 0xa1, 0x63, 0xa3, 0xa2,
0x62, 0x66, 0xa6, 0xa7, 0x67, 0xa5, 0x65, 0x64, 0xa4, 0x6c, 0xac, 0xad, 0x6d, 0xaf, 0x6f,
0x6e, 0xae, 0xaa, 0x6a, 0x6b, 0xab, 0x69, 0xa9, 0xa8, 0x68, 0x78, 0xb8, 0xb9, 0x79, 0xbb,
0x7b, 0x7a, 0xba, 0xbe, 0x7e, 0x7f, 0xbf, 0x7d, 0xbd, 0xbc, 0x7c, 0xb4, 0x74, 0x75, 0xb5,
0x77, 0xb7, 0xb6, 0x76, 0x72, 0xb2, 0xb3, 0x73, 0xb1, 0x71, 0x70, 0xb0, 0x50, 0x90, 0x91,
0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9c, 0x5c,
0x5d, 0x9d, 0x5f, 0x9f, 0x9e, 0x5e, 0x5a, 0x9a, 0x9b, 0x5b, 0x99, 0x59, 0x58, 0x98, 0x88,
0x48, 0x49, 0x89, 0x4b, 0x8b, 0x8a, 0x4a, 0x4e, 0x8e, 0x8f, 0x4f, 0x8d, 0x4d, 0x4c, 0x8c,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
0x40
};
const unsigned char char_tab[128]=
{
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, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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,
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 lenth)
{
unsigned short crc_tmp;
crc_tmp = crc(src_buf, lenth);
*(src_buf+lenth) = crc_tmp >> 8 ;
*(src_buf+lenth+1) = crc_tmp & 0xff;
lenth++;
lenth++;
while ( lenth--)
{
*dst_buf = *src_buf;
dst_buf++;
src_buf++;
}
}
/*
1 读取继电器状态
发送:
*/
int ascii_read_coil_status ( unsigned char board_adr, unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256],tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_coil;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_ascii_frm (com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_read_coil_status ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_coil;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_rtu_frm ( com_buf, tmp, tmp_lenth);
return 8;
}
/*
2 读取开关量输入
发送:
*/
int ascii_read_input_status ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_di;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_ascii_frm ( com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_read_input_status ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_di;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_rtu_frm ( com_buf,tmp,tmp_lenth);
return 8;
}
/*
3 读取保持寄存器
发送:
*/
int ascii_read_hldreg ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_hld_reg;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_ascii_frm ( com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_read_hldreg ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_hld_reg;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_rtu_frm ( com_buf,tmp,tmp_lenth);
return 8;
}
// 4 发送读取模拟量输入
int ascii_read_anloginput ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_ai;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_ascii_frm ( com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_read_anloginput ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth)
{
unsigned char tmp[256],tmp_lenth;
tmp[0] = board_adr;
tmp[1] = read_ai;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(lenth);
tmp[5] = low(lenth);
tmp_lenth = 6;
construct_rtu_frm ( com_buf,tmp,tmp_lenth);
return 8;
}
/*
5 设置继电器
发送:status =0 继电器释放 否则继电器吸合,address 为吸合的继电器编号,0为第一个继电器,依次类推
*/
int ascii_set_coil ( unsigned char board_adr,unsigned char *com_buf,int start_address,int status )
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = set_coil ;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
if ( status )
{
tmp[4] = 0xff;
tmp[5] = 0;
}
else
{
tmp[4]= 0;
tmp[5]= 0;
}
tmp_lenth = 6;
construct_ascii_frm ( com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_set_coil ( unsigned char board_adr,unsigned char *com_buf,int start_address,int status )
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = set_coil ;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
if ( status )
{
tmp[4] = 0xff;
tmp[5] = 0;
}
else
{
tmp[4]= 0;
tmp[5]= 0;
}
tmp_lenth = 6;
construct_rtu_frm ( com_buf, tmp, tmp_lenth);
return 8 ;
}
/*
6 设置保持寄存器
*/
int ascii_set_hldreg( unsigned char board_adr,unsigned char *com_buf,int start_address, unsigned int value )
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = set_hld_reg;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(value);
tmp[5] = low(value);
tmp_lenth = 6;
construct_ascii_frm ( com_buf, tmp, tmp_lenth);
return 18;
}
int rtu_set_hldreg( unsigned char board_adr,unsigned char *com_buf, int start_address, unsigned int value )
{
unsigned char tmp[256], tmp_lenth;
tmp[0] = board_adr;
tmp[1] = set_hld_reg;
tmp[2] = hi(start_address);
tmp[3] = low(start_address);
tmp[4] = hi(value);
tmp[5] = low(value);
tmp_lenth = 6;
construct_rtu_frm ( com_buf, tmp, tmp_lenth);
return 8 ;
}
/*
7
接收分析:
dest_p 接收到数据指针
sourc_p 串口接收缓冲区指针
data_start_address 开始地址
*/
/* rtu 接收分析 */
int rtu_data_anlys( int *dest_p, unsigned char *source_p, int data_start_address, int fr_lenth)
{
unsigned short crc_result, crc_tmp;
unsigned char tmp1, tmp2, shift;
crc_tmp = *(source_p + fr_lenth-2); // crc 第一字节
crc_tmp = crc_tmp * 256 + *( source_p+fr_lenth-1); // crc 值
crc_result = crc(source_p, fr_lenth-2); // 计算crc 值
if ( crc_tmp != crc_result ) // crc 校验正确
{
hld_reg[0x31]++;
return -1;
}
switch ( *(source_p+1) ) // 功能码
{
case read_coil: /*读取继电器状态 */
for ( tmp1=0; tmp1>= 1;
}
}
break;
case read_di: /*读取开关量输入*/
for ( tmp1=0; tmp1>=1;
}
}
break;
case read_hld_reg: /*读取保持寄存器*/
for ( tmp1=0; tmp1>= 1;
}
}
break;
case read_di: /*读取开关量输入*/
for ( tmp1=0; tmp1 {
shift = 1;
for (tmp2=0; tmp2>=1;
}
}
break;
case read_hld_reg: /*读取保持寄存器*/
for (tmp1=0; tmp1 {
*(dest_p + data_start_address+ tmp1/2) = tmp[tmp1+3]*256 + tmp[tmp1+4] ;
}
break ;
case 4: /*读取模拟量输入*/
for (tmp1=0; tmp1 {
*(dest_p+data_start_address+ tmp1/2) = tmp[tmp1+3]*256 + tmp[tmp1+4] ;
}
break;
case protocol_exception:
return -1*protocol_err;
//break;
default:
break;
}
return 0;
}
/*
主程序按照一定的顺序调用 1~6子程序,然后把生成的缓存内容写入串口。
接收到数据送给7的子程序分析即可。
ascii 方式下,用0x0d, 0x0a作为帧结束判断的依据
rtu方式下,以两个字节间的时间间隔大于3.5倍的一个字符周期为帧结束判断依据
read() write()是两个假想存在的函数 ,需要根据不同的系统来完成。
比如在单片机中,可能要用到中断模式;在linux下可能是一个阻塞的read write调用,在windows下可能是串口控件的read write 方法。。。。。
因为系统各式各样,所以只能抽象出一个假想的函数,由代码的使用者实现。
*/
/*void main ( void)
{
ascii_read_coil_status ( 1,tx_buf,0,8);
write (com1,tx_buf );
read (com1,rx_buf);
ascii_data_anlys( coil,rx_buf,0);
ascii_read_input_status ( 1,tx_buf,0,8);
write ( com1,tx_buf );
read (com1, rx_buf);
ascii_data_anlys( di,rx_buf,0);
ascii_read_hldreg ( 1,tx_buf,0,8);
write ( com1,tx_buf );
read (com1, rx_buf);
ascii_data_anlys( hld_reg,rx_buf,0);
ascii_read_anloginput( 1,tx_buf,0,8);
write ( com1,tx_buf );
read (com1, rx_buf);
ascii_data_anlys( ai,rx_buf,0);
ascii_set_coil (1,tx_buf,0,1); //第一个继电器吸合/
write ( com1,tx_buf );
read (com1, rx_buf);
ascii_data_anlys( di,rx_buf,0);
ascii_set_coil (1,tx_buf,0,0); //第一个继电器释放/
write ( com1,tx_buf );
read (com1, rx_buf);
ascii_data_anlys( di,rx_buf,0);
}*/
----------------------------------------------
此篇文章从博客转发
原文地址: Http://blog.gkong.com/more.asp?id=64954&Name=jiayoua