/*-------------------------------------------------------------------------------*/
/*********************************************************************************/ /*函数名称: WriteMultipleWordToModBusSlave()
*输入参数: 共 2 个参数; *输出参数: 共 1 个参数; *返回值: 成功与否;
*需储存的参数: 共 1 个参数; *功能介绍:
(1) 从触摸屏中写入设定参数值;
(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 parameterData[], unsigned short num, unsigned short ModDataAddr) {
unsigned char writeCmdStr[MOD_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 totalStrBack[300] = {'\\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;
writeCmdStr[0] = ModAddr;
writeCmdStr[1] = ModFunction;
writeCmdStr[2] = (unsigned char)(ModDataAddr >> 8 & 0x00FF); writeCmdStr[3] = (unsigned char)(ModDataAddr & 0x00FF); writeCmdStr[4] = (unsigned char)(ModDataNum >> 8 & 0x00FF); writeCmdStr[5] = (unsigned char)(ModDataNum & 0x00FF); writeCmdStr[6] = (unsigned char)(byteCount & 0x00FF); WordDataToModbusStr(writeCmdStr, parameterData, num, 7); writeCmdStr[7 + byteCount] = '\\0';
ClearCom(comPort);
SendRtuCmdToModBus(comPort, writeCmdStr, readCmdLen); isReadOK = ResponseFromModBusRtuSlave(comPort, totalStrBack,
&ModAddrBack, &ModFunctionBack, &ModDataAddrBack, &ModDataNumBack, MOD_TIMEOUT, MOD_WAITTIME);
if (1 == isReadOK) {
if((ModAddr == ModAddrBack) && (ModFunction == ModFunctionBack) && (ModDataAddr == ModDataAddrBack) && (ModDataNum == ModDataNumBack)) { return 1; } else
{ return 0; } } else {
/*未收到字符串或CRC校验出错*/
return 0; } }
/*-------------------------------------------------------------------------------*/
/*********************************************************************************/ /*函数名称: WriteMultipleBitToModBusSlave() *输入参数: 共 2 个参数; *输出参数: 共 1 个参数; *返回值: 成功与否;
*需储存的参数: 共 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 WriteMultipleBitToModBusSlave(int comPort, int DOData[], unsigned short num, unsigned short ModDataAddr) {
unsigned char writeCmdStr[100]; unsigned char ModAddr = 1;
unsigned char ModFunction = 15; unsigned short ModDataNum; unsigned short readCmdLen; int isReadOK = 0;
unsigned short byteCount;
unsigned char totalStrBack[300] = {'\\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 + byteCount;
writeCmdStr[0] = ModAddr;
writeCmdStr[1] = ModFunction;
writeCmdStr[2] = (unsigned char)(ModDataAddr >> 8 & 0x00FF); writeCmdStr[3] = (unsigned char)(ModDataAddr & 0x00FF); writeCmdStr[4] = (unsigned char)(ModDataNum >> 8 & 0x00FF); writeCmdStr[5] = (unsigned char)(ModDataNum & 0x00FF); writeCmdStr[6] = (unsigned char)(byteCount & 0x00FF); BitDataToModbusStr(writeCmdStr, DOData, num, 7); writeCmdStr[7 + byteCount] = '\\0';
ClearCom(comPort);
SendRtuCmdToModBus(comPort, writeCmdStr, readCmdLen); isReadOK = ResponseFromModBusRtuSlave(comPort, totalStrBack,
&ModAddrBack, &ModFunctionBack, &ModDataAddrBack, &ModDataNumBack, MOD_TIMEOUT, MOD_WAITTIME);
if (1 == isReadOK) {
if((ModAddr == ModAddrBack) && (ModFunction == ModFunctionBack) && (ModDataAddr == ModDataAddrBack) && (ModDataNum == ModDataNumBack)) {
return 1; } else {
return 0; } }
else {
/*未收到字符串或CRC校验出错*/ return 0; } }
/*-------------------------------------------------------------------------------*/
/*********************************************************************************/ /*函数名称: WriteSingleBitToModBusSlave()
*输入参数: 共 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 开始编写; *[2006-3-7 14:40] 完成; *[2006-3-9 12:44]
去掉了等待接收反馈的功能;
/* */ /*********************************************************************************/
int WriteSingleBitToModBusSlave(int comPort, int DOData[], unsigned short num, unsigned short ModDataAddr) {
unsigned char writeCmdStr[15];