📄 modbus ascii 开发2.txt
字号:
Modbus ASCII 开发2-基于Scilab得SCADA system开发手记7
0
推荐
#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,
} ;
const unsigned char auchCRCLo[] = {
0x00, 0xC0, 0xC1, 0x01,
};
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 << 8 | uchCRCLo) ;
}
unsigned char a2toh(unsigned char *str)
{
unsigned char tmp;
tmp = 0;
tmp = char_tab[*str];
str++;
tmp = tmp*16 + char_tab[* str];
return tmp;
}
void htoa( char *str, unsigned char num)/*10进制转16进制*/
{
unsigned char tmp;
tmp = num;
tmp = tmp & 0xf0;
tmp >>= 4;
*str = tab_char [ tmp];
str++;
*str = tab_char [ num & 0x0f];
}
unsigned char HI(int num_16)
{
unsigned char tt;
tt=num_16 & 0xff00;
tt >>= 8;
return tt;
}
unsigned char LOW(int num_16)
{
unsigned char tt;
tt=num_16 & 0x00ff;
return tt;
}
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)));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -