收藏 分享(赏)

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

上传人:精品资料 文档编号:10246504 上传时间:2019-10-24 格式:DOC 页数:35 大小:93.50KB
下载 相关 举报
MODBUS通讯+C语言源代码.doc_第1页
第1页 / 共35页
MODBUS通讯+C语言源代码.doc_第2页
第2页 / 共35页
MODBUS通讯+C语言源代码.doc_第3页
第3页 / 共35页
MODBUS通讯+C语言源代码.doc_第4页
第4页 / 共35页
MODBUS通讯+C语言源代码.doc_第5页
第5页 / 共35页
点击查看更多>>
资源描述

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营业执照举报