【AAA】C语言编写modbus协议

上传人:Jerm****014 文档编号:93099704 上传时间:2019-07-16 格式:DOC 页数:12 大小:24.54KB
返回 下载 相关 举报
【AAA】C语言编写modbus协议_第1页
第1页 / 共12页
【AAA】C语言编写modbus协议_第2页
第2页 / 共12页
【AAA】C语言编写modbus协议_第3页
第3页 / 共12页
【AAA】C语言编写modbus协议_第4页
第4页 / 共12页
【AAA】C语言编写modbus协议_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《【AAA】C语言编写modbus协议》由会员分享,可在线阅读,更多相关《【AAA】C语言编写modbus协议(12页珍藏版)》请在金锄头文库上搜索。

1、【MeiWei_81重点借鉴文档】modbus.cincludemain.h/字地址0-255(只取低8位)/位地址0-255(只取低8位)/RCRC高位字节值表R/constuint8codeauchCRCHi=0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R8

2、1,0R40,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R00,0RC1,0R8

3、1,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R8

4、1,0R40,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R00,0RC1,0R8

5、1,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40,0R01,0RC0,0R80,0R41,0R01,0RC0,0R80,0R41,0R00,0RC1,0R81,0R40;/RCRC低位字节值表R/constuint8codeauchCRCLo=0R00,0RC0,0RC1,0R01,0RC3,0R03,0R02,0RC2,0RC6,0R06,0R07,0RC7,0R05,0RC5,0RC4,0

6、R04,0RCC,0R0C,0R0D,0RCD,0R0F,0RCF,0RCE,0R0E,0R0A,0RCA,0RCB,0R0B,0RC9,0R09,0R08,0RC8,0RD8,0R18,0R19,0RD9,0R1B,0RDB,0RDA,0R1A,0R1E,0RDE,0RDF,0R1F,0RDD,0R1D,0R1C,0RDC,0R14,0RD4,0RD5,0R15,0RD7,0R17,0R16,0RD6,0RD2,0R12,0R13,0RD3,0R11,0RD1,0RD0,0R10,0RF0,0R30,0R31,0RF1,0R33,0RF3,0RF2,0R32,0R36,0RF6,0RF7,0

7、R37,0RF5,0R35,0R34,0RF4,0R3C,0RFC,0RFD,0R3D,0RFF,0R3F,0R3E,0RFE,0RFA,0R3A,0R3B,0RFB,0R39,0RF9,0RF8,0R38,0R28,0RE8,0RE9,0R29,0REB,0R2B,0R2A,0REA,0REE,0R2E,0R2F,0REF,0R2D,0RED,0REC,0R2C,0RE4,0R24,0R25,0RE5,0R27,0RE7,0RE6,0R26,0R22,0RE2,0RE3,0R23,0RE1,0R21,0R20,0RE0,0RA0,0R60,0R61,0RA1,0R63,0RA3,0RA2,0

8、R62,0R66,0RA6,0RA7,0R67,0RA5,0R65,0R64,0RA4,0R6C,0RAC,0RAD,0R6D,0RAF,0R6F,0R6E,0RAE,0RAA,0R6A,0R6B,0RAB,0R69,0RA9,0RA8,0R68,0R78,0RB8,0RB9,0R79,0RBB,0R7B,0R7A,0RBA,0RBE,0R7E,0R7F,0RBF,0R7D,0RBD,0RBC,0R7C,0RB4,0R74,0R75,0RB5,0R77,0RB7,0RB6,0R76,0R72,0RB2,0RB3,0R73,0RB1,0R71,0R70,0RB0,0R50,0R90,0R91,0

9、R51,0R93,0R53,0R52,0R92,0R96,0R56,0R57,0R97,0R55,0R95,0R94,0R54,0R9C,0R5C,0R5D,0R9D,0R5F,0R9F,0R9E,0R5E,0R5A,0R9A,0R9B,0R5B,0R99,0R59,0R58,0R98,0R88,0R48,0R49,0R89,0R4B,0R8B,0R8A,0R4A,0R4E,0R8E,0R8F,0R4F,0R8D,0R4D,0R4C,0R8C,0R44,0R84,0R85,0R45,0R87,0R47,0R46,0R86,0R82,0R42,0R43,0R83,0R41,0R81,0R80,0

10、R40;uint8testCoil;/用于测试位地址1uint16testRegister;/用于测试字址址16uint8localAddr=1;/单片机控制板的地址uint8sendCount;/发送字节个数uint8receCount;/接收到的字节个数uint8sendPosi;/发送位置uint16crc16(uint8RpuchMsg,uint16usDataLen)uint8uchCRCHi=0RFF;/R高CRC字节初始化R/uint8uchCRCLo=0RFF;/R低CRC字节初始化R/uint32uIndeR;/RCRC循环中的索引R/while(usDataLen-)/R传

11、输消息缓冲区R/uIndeR=uchCRCHiRpuchMsg+;/R计算CRCR/uchCRCHi=uchCRCLoauchCRCHiuIndeR;uchCRCLo=auchCRCLouIndeR;return(uchCRCHi1)sendCount-;ACC=sendBuf0;TB8=P;SBUF=sendBuf0;/voidbeginSend(void)/读线圈状态voidreadCoil(void)uint8addr;uint8tempAddr;uint8bRteCount;uint8bitCount;uint16crcData;uint8position;uint8i,k;uint8

12、result;uint16tempData;uint8eRit=0;/addr=(receBuf28)+receBuf3;/tempAddr=addr&0Rfff;addr=receBuf3;tempAddr=addr;/bitCount=(receBuf48)+receBuf5;/读取的位个数bitCount=receBuf5;bRteCount=bitCount/8;/字节个数if(bitCount%8!=0)bRteCount+;for(k=0;kbRteCount;k+)/字节位置position=k+3;sendBufposition=0;for(i=0;i8;i+)getCoilV

13、al(tempAddr,&tempData);sendBufposition|=tempData=addr+bitCount)if(tempAddr=addr+bitCount)/读完eRit=1;break;if(eRit=1)break;sendBuf0=localAddr;sendBuf1=0R01;sendBuf2=bRteCount;bRteCount+=3;crcData=crc16(sendBuf,bRteCount);sendBufbRteCount=crcData8;bRteCount+;sendBufbRteCount=crcData&0Rff;sendCount=bRte

14、Count+1;beginSend();/voidreadCoil(void)/读寄存器voidreadRegisters(void)uint8addr;uint8tempAddr;uint16result;uint16crcData;uint8readCount;uint8bRteCount;uint8finsh;/1完成0出错uint16i;uint16tempData=0;/addr=(receBuf28)+receBuf3;/tempAddr=addr&0Rfff;addr=receBuf3;tempAddr=addr;/readCount=(receBuf48)+receBuf5;/要读的个数readCount=receBuf5;bRteCount=readCountR2;for(i=0;i8;sendBufi+4=tempData&0Rff;sendBuf0=localAddr

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 商业合同/协议

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号