ImageVerifierCode 换一换
格式:DOC , 页数:35 ,大小:93.50KB ,
资源ID:10246504      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-10246504.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(MODBUS通讯+C语言源代码.doc)为本站会员(精品资料)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

MODBUS通讯+C语言源代码.doc

1、/*/ /*函数名称: strmcpy() *输入参数: 共 个参数; *输出参数: 共 个参数; *返回值: *需储存的参数: 共 个参数; *功能介绍: (1)字符数组拷贝; *修改日志: *2006-3-6 17:07 Ver. 1.00 开始编写; 完成; /* */ /*/ void strmcpy(unsigned char dest, unsigned char src, int count) int i; for(i = 0; i -0.0001) /* */ /*/ int Datamcmp(float data1, float data2, int count) int i

2、; for(i = 0; i -0.0001) return 1; return 0; /*/ /*/ /*函数名称: Bitmcmp() *输入参数: 共 个参数; *输出参数: 共 个参数; *返回值: *需储存的参数: 共 个参数; *功能介绍: (1)开关量数组比较;相同则返回 0,不相同则返回 1; *修改日志: *2006-3-6 18:10 Ver. 1.00 开始编写; 完成; /* */ /*/ int Bitmcmp(int data1, int data2, int count) int i; for(i = 0; i 8 /* send CRC16 high */ To

3、Com(ModPort, (unsigned char)(CRC16 /* send CRC16 low */ /*/ /*/ /*函数名称: ReadModBusRtuSlave() *输入参数: 共 8 个参数; *输出参数: 共 5 个参数; *返回值: 成功与否 1:成功, 2:失败; *需储存的参数: 共 0 个参数; *功能介绍: (1)读取 ModBusRtuSlave,并解码输出反馈字符串相关内容; ReadModBusRtuSlave(COMPORT1, ModAddr, ModFunction, ModByteNum, ModData, unsigned char ModB

4、usMessageMOD_STR_MAX_LEN; *2006-3-1 13:46 增加了 strmcpy(ModData, ModBusMessage + 3, ModBusIdx - 5); /* */ /*/ int ReadModBusRtuSlave(int ModPort, unsigned char *ModAddr, unsigned char *ModFunction, unsigned char *ModByteNum, unsigned char ModData, unsigned char *ModDataLen, unsigned ModTimeout, unsign

5、ed waitTime) unsigned char data; unsigned long t; int i; unsigned short CRC16; int ModBusIdx = 0; int counter = 0; unsigned char ModBusMessageMOD_STR_MAX_LEN = 0; while(!(IsCom(ModPort) *ModFunction = ModBusMessage1; *ModByteNum = ModBusMessage2; strmcpy(ModData, ModBusMessage + 3, ModBusIdx - 5); /

6、* Print(“ idx%d “, ModBusIdx - 5); /*test*/ /* for(i = 3; i 8 *ModFunction = ModBusMessage1; *ModDataAddr = (unsigned short)(unsigned short)ModBusMessage2 8 *ModFunction = ModBusMessage1; *ModDataAddr = (unsigned short)(unsigned short)ModBusMessage2 8 readCmdStr3 = (unsigned char)(ModDataAddr readCm

7、dStr4 = (unsigned char)(ModDataNum 8 readCmdStr5 = (unsigned char)(ModDataNum readCmdStr6 = 0; ClearCom(comPort); SendRtuCmdToModBus(comPort, readCmdStr, readCmdLen); isReadOK = ReadModBusRtuSlave(comPort, if (1 = isReadOK) if(ModAddr = ModAddrBack) /* Print(“ %f %f %f t“, parameterData0, parameterD

8、ata1,parameterData2); Print(“n%f %f %f t“, parameterJudge0, parameterJudge1,parameterJudge2); */ if(parameterJudge0 = 0.1) /* 以温度设定值为判断点判断正确性 */ /* Print(“ cp1.5 n“); */ if(Datamcmp(parameterData, parameterJudge, num) Print(“ cp1.6 n“); ChangeAllParameterDataByModBus(parameterData, ModDataBack, num)

9、; if(IS_RS485) Set485DirToTransmit(COMPORT1); SendParameterDataToPCForwardly(COMPORT1, parameterData, i7188Addr); if(IS_RS485) WaitTransmitOver(COMPORT1); if(IS_RS485) DelayMs(2); Set485DirToReceive(COMPORT1); WriteDataToEEPROM(parameterData, parameterDecimalEEPROM, num, parameterEEPROMAddr, paramet

10、erStartAddr); strmcpy(oldModDataBack, ModDataBack, ModDataLenBack); else /*未初始化,发送数据以初始*/ WriteMultipleWordToModBusSlave(comPort, parameterData, num, ModDataAddr); /* Print(“n %f %f %f t“, parameterData0, parameterData1,parameterData2); */ return 1; else /*收到的字符串不匹配*/ return 0; else /*未收到字符串或 CRC 校验

11、出错*/ return 0; /*-*/ /*/ /*函数名称: ReadBitFromModBusSlave() *输入参数: 共 2 个参数; *输出参数: 共 1 个参数; *返回值: 成功与否; *需储存的参数: 共 1 个参数; *功能介绍: (1) 从触摸屏中读取按钮值; (2) 读取指令为 01 01 HA LA HN LN HC LC;其中 01 为MODBUSSLAVE 的地址, 01 为读连续多个 BIT 线圈的功能码, HA LA 为第一个 BIT 的高低位地址, HN LN 为 BIT 数量的高低位,HC LC 为 CRC 校验码; *修改日志: *2005-11-28

12、 18:10 Ver. 1.00 开始编写; *2005-11-28 18:42 完成初稿,未测试; *2006-3-3 9:38 static unsigned char oldModDataBack400;改为形参; *2006-3-3 17:58 增加 int kind 形参; *2006-3-6 16:35 strmcpy(oldModDataBack, ModDataBack, ModDataLenBack); *2006-3-9 12:35 去掉 (strmcmp(oldModDataBack, ModDataBack, ModDataLenBack) /* */ /*/ int

13、ReadBitFromModBusSlave(int kind, int comPort, int buttonData, unsigned short num, unsigned short ModDataAddr, int buttonNVRAMAddr, unsigned char oldModDataBack) int buttonJudge30; unsigned char readCmdStr15; unsigned char ModAddr = 1; unsigned char ModFunction = 1; unsigned short ModDataNum; unsigne

14、d short readCmdLen = 6; int isReadOK = 0; unsigned char ModAddrBack = 0; unsigned char ModFunctionBack = 0; unsigned char ModByteNumBack = 0; unsigned char ModDataBack300 = 0; unsigned char ModDataLenBack = 0; /* unsigned short ModBit100; */ char i7188Addr5; strcpy(i7188Addr, I7188_ADDRESS); /* adju

15、st */ if(kind = 1) strcpy(i7188Addr, I7188_ADDRESS); else if(kind = 2) strcpy(i7188Addr, I7188_ADDRESS_2); else if(kind = 3) strcpy(i7188Addr, I7188_ADDRESS_3); ModDataNum = (unsigned short)num; readCmdStr0 = ModAddr; readCmdStr1 = ModFunction; readCmdStr2 = (unsigned char)(ModDataAddr 8 readCmdStr3

16、 = (unsigned char)(ModDataAddr readCmdStr4 = (unsigned char)(ModDataNum 8 readCmdStr5 = (unsigned char)(ModDataNum readCmdStr6 = 0; ClearCom(comPort); SendRtuCmdToModBus(comPort, readCmdStr, readCmdLen); isReadOK = ReadModBusRtuSlave(comPort, if (1 = isReadOK) if(ModAddr = ModAddrBack) if(Bitmcmp(bu

17、ttonData, buttonJudge, num) ChangeAllButtonsDataByModBus(buttonData, ModDataBack, num); if(IS_RS485) Set485DirToTransmit(COMPORT1); SendButtonDataToPC(COMPORT1, buttonData, i7188Addr); if(IS_RS485) WaitTransmitOver(COMPORT1); if(IS_RS485) DelayMs(2); Set485DirToReceive(COMPORT1); WriteButtonToNVRAM(

18、buttonData, num, buttonNVRAMAddr); strmcpy(oldModDataBack, ModDataBack, ModDataLenBack); return 1; else /*收到的字符串不匹配*/ return 1; else /*未收到字符串或 CRC 校验出错*/ return 0; /*-*/ /*/ /*函数名称: WriteMultipleWordToModBusSlave() *输入参数: 共 2 个参数; *输出参数: 共 1 个参数; *返回值: 成功与否; *需储存的参数: 共 1 个参数; *功能介绍: (1) 从触摸屏中写入设定参数值

19、; (2) 指令为 01 16 HA LA HN LN HD LD HC LC;其中 01 为MODBUSSLAVE 的地址, 16 为写连续多个 WORD 寄存器的功能码, HA LA 为第一个寄存器的高低位地址, HN LN 为寄存器数量的高低位,HC LC 为 CRC 校验码; HD LD 为数据高低位; *修改日志: *2005-11-28 18:43 Ver. 1.00 开始编写; *2006-3-1 12:20 中间隔了很多时间; 完成初稿,未测试; /* */ /*/ int WriteMultipleWordToModBusSlave(int comPort, float pa

20、rameterData, unsigned short num, unsigned short ModDataAddr) unsigned char writeCmdStrMOD_STR_MAX_LEN; /* adjust */ unsigned char ModAddr = 1; unsigned char ModFunction = 16; unsigned short ModDataNum; unsigned short readCmdLen; int isReadOK = 0; unsigned short byteCount; unsigned char totalStrBack3

21、00 = 0; unsigned char ModAddrBack = 0; unsigned char ModFunctionBack = 0; unsigned short ModDataAddrBack; unsigned short ModDataNumBack; ModDataNum = (unsigned short)num * 2; byteCount = (unsigned short)num * 4; readCmdLen = 7 + byteCount; writeCmdStr0 = ModAddr; writeCmdStr1 = ModFunction; writeCmd

22、Str2 = (unsigned char)(ModDataAddr 8 writeCmdStr3 = (unsigned char)(ModDataAddr writeCmdStr4 = (unsigned char)(ModDataNum 8 writeCmdStr5 = (unsigned char)(ModDataNum writeCmdStr6 = (unsigned char)(byteCount WordDataToModbusStr(writeCmdStr, parameterData, num, 7); writeCmdStr7 + byteCount = 0; ClearC

23、om(comPort); SendRtuCmdToModBus(comPort, writeCmdStr, readCmdLen); isReadOK = ResponseFromModBusRtuSlave(comPort, totalStrBack, if (1 = isReadOK) if(ModAddr = ModAddrBack) else return 0; else /*未收到字符串或 CRC 校验出错*/ return 0; /*-*/ /*/ /*函数名称: WriteMultipleBitToModBusSlave() *输入参数: 共 2 个参数; *输出参数: 共 1

24、个参数; *返回值: 成功与否; *需储存的参数: 共 1 个参数; *功能介绍: (1) 从触摸屏中写入 BIT; (2) 指令为 01 15 HA LA HN LN HD LD HC LC;其中 01 为MODBUSSLAVE 的地址, 16 为读连续多个 WORD 寄存器的功能码, HA LA 为第一个 BIT 的高低位地址, HN LN 为 BIT 数量的高低位,HC LC 为 CRC 校验码; HD LD 为数据高低位; *修改日志: *2006-3-1 12:58 Ver. 1.00 开始编写; *2006-3-1 13:29 完成; /* */ /*/ int WriteMult

25、ipleBitToModBusSlave(int comPort, int DOData, unsigned short num, unsigned short ModDataAddr) unsigned char writeCmdStr100; unsigned char ModAddr = 1; unsigned char ModFunction = 15; unsigned short ModDataNum; unsigned short readCmdLen; int isReadOK = 0; unsigned short byteCount; unsigned char total

26、StrBack300 = 0; unsigned char ModAddrBack = 0; unsigned char ModFunctionBack = 0; unsigned short ModDataAddrBack; unsigned short ModDataNumBack; ModDataNum = (unsigned short)num; if(num % 8) byteCount = (unsigned short)(num / 8 + 1); else byteCount = (unsigned short)(num / 8); readCmdLen = 7 + byteC

27、ount; writeCmdStr0 = ModAddr; writeCmdStr1 = ModFunction; writeCmdStr2 = (unsigned char)(ModDataAddr 8 writeCmdStr3 = (unsigned char)(ModDataAddr writeCmdStr4 = (unsigned char)(ModDataNum 8 writeCmdStr5 = (unsigned char)(ModDataNum writeCmdStr6 = (unsigned char)(byteCount BitDataToModbusStr(writeCmd

28、Str, DOData, num, 7); writeCmdStr7 + byteCount = 0; ClearCom(comPort); SendRtuCmdToModBus(comPort, writeCmdStr, readCmdLen); isReadOK = ResponseFromModBusRtuSlave(comPort, totalStrBack, if (1 = isReadOK) if(ModAddr = ModAddrBack) else return 0; else /*未收到字符串或 CRC 校验出错*/ return 0; /*-*/ /*/ /*函数名称: W

29、riteSingleBitToModBusSlave() *输入参数: 共 2 个参数; *输出参数: 共 1 个参数; *返回值: 成功与否; *需储存的参数: 共 1 个参数; *功能介绍: (1) 从触摸屏中写入 BIT; (2) 指令为 01 05 HA LA HD LD HC LC;其中 01 为MODBUSSLAVE 的地址, 05 为写单个 BIT 功能码,HA LA 为第一个 BIT 的高低位地址, HN LN 为 BIT 数量的高低位,HC LC 为 CRC 校验码; HD LD 为数据高低位; *修改日志: *2006-3-7 14:06 Ver. 1.00 开始编写; *

30、2006-3-7 14:40 完成; *2006-3-9 12:44 去掉了等待接收反馈的功能; /* */ /*/ int WriteSingleBitToModBusSlave(int comPort, int DOData, unsigned short num, unsigned short ModDataAddr) unsigned char writeCmdStr15; unsigned char ModAddr = 1; unsigned char ModFunction = 5; unsigned short ModDataState; unsigned short readC

31、mdLen = 6; /* int isReadOK = 0; unsigned short byteCount; unsigned char totalStrBack100; unsigned char ModAddrBack = 0; unsigned char ModFunctionBack = 0; unsigned short ModDataAddrBack; unsigned short ModDataStateBack; */ ModDataState = DODatanum; writeCmdStr0 = ModAddr; writeCmdStr1 = ModFunction;

32、 writeCmdStr2 = (unsigned char)(ModDataAddr 8 writeCmdStr3 = (unsigned char)(ModDataAddr if(ModDataState) writeCmdStr4 = (unsigned char)(0x00FF); else writeCmdStr4 = (unsigned char)(0x0000); writeCmdStr5 = (unsigned char)(0x0000); writeCmdStr6 = 0; /* ClearCom(comPort); */ SendRtuCmdToModBus(comPort

33、, writeCmdStr, readCmdLen); return 1; /* isReadOK = ResponseSingleBitFromModBus(comPort, totalStrBack, if (1 = isReadOK) Print(“tB %u %u %u %u %u %u %u %u“, ModAddr, ModAddrBack, ModFunction, ModFunctionBack, ModDataAddr, ModDataAddrBack, ModDataState, ModDataStateBack); if(ModAddr = ModAddrBack) return 1;

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报