好文档 - 专业文书写作范文服务资料分享网站

MODBUS通讯+C语言源代码 - 图文 

天下 分享 时间: 加入收藏 我要投稿 点赞

ModDataNum = (unsigned short)num * 2;

readCmdStr[0] = ModAddr;

readCmdStr[1] = ModFunction;

readCmdStr[2] = (unsigned char)(ModDataAddr >> 8 & 0x00FF); readCmdStr[3] = (unsigned char)(ModDataAddr & 0x00FF); readCmdStr[4] = (unsigned char)(ModDataNum >> 8 & 0x00FF); readCmdStr[5] = (unsigned char)(ModDataNum & 0x00FF); readCmdStr[6] = '\\0';

ClearCom(comPort);

SendRtuCmdToModBus(comPort, readCmdStr, readCmdLen);

isReadOK = ReadModBusRtuSlave(comPort, &ModAddrBack, &ModFunctionBack, &ModByteNumBack,

ModDataBack, &ModDataLenBack, MOD_TIMEOUT, MOD_WAITTIME);

if (1 == isReadOK) {

if((ModAddr == ModAddrBack) && (ModFunction == ModFunctionBack) && (ModByteNumBack == ModDataLenBack)) { if (ModDataNum * 2 == ModDataLenBack) /*!!!*/

{

ChangeAllParameterDataByModBus(parameterJudge, ModDataBack, num);

/* Print(\%f %f %f \\t\parameterData[0], parameterData[1],parameterData[2]);

Print(\ %f %f \\t\parameterJudge[1],parameterJudge[2]); */ if(parameterJudge[0] >= 0.1) /* 以温度设定值为判断点判断正确性 */ {

/* Print(\

if(Datamcmp(parameterData, parameterJudge, num))

{

Print(\

ChangeAllParameterDataByModBus(parameterData, ModDataBack, num);

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, parameterStartAddr);

}

strmcpy(oldModDataBack, ModDataBack, ModDataLenBack);

} else {

/*未初始化,发送数据以初始*/

WriteMultipleWordToModBusSlave(comPort, parameterData, num, ModDataAddr); }

/* Print(\ %f %f \\t\parameterData[1],parameterData[2]); */ }

return 1; } else {

/*收到的字符串不匹配*/ return 0; } } else {

/*未收到字符串或CRC校验出错*/ 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 18:10] Ver. 1.00 开始编写; *[2005-11-28 18:42]

完成初稿,未测试; *[2006-3-3 9:38]

static unsigned char oldModDataBack[400];改为形参; *[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 ReadBitFromModBusSlave(int kind, int comPort, int buttonData[], unsigned short num,

unsigned short ModDataAddr, int buttonNVRAMAddr, unsigned char oldModDataBack[]) {

int buttonJudge[30];

unsigned char readCmdStr[15]; unsigned char ModAddr = 1;

unsigned char ModFunction = 1; unsigned short ModDataNum; unsigned short readCmdLen = 6; int isReadOK = 0;

unsigned char ModAddrBack = 0; unsigned char ModFunctionBack = 0; unsigned char ModByteNumBack = 0;

unsigned char ModDataBack[300] = {'\\0'};

unsigned char ModDataLenBack = 0;

/* unsigned short ModBit[100]; */ char i7188Addr[5];

strcpy(i7188Addr, I7188_ADDRESS); /* adjust */

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;

readCmdStr[0] = ModAddr;

readCmdStr[1] = ModFunction;

readCmdStr[2] = (unsigned char)(ModDataAddr >> 8 & 0x00FF); readCmdStr[3] = (unsigned char)(ModDataAddr & 0x00FF); readCmdStr[4] = (unsigned char)(ModDataNum >> 8 & 0x00FF); readCmdStr[5] = (unsigned char)(ModDataNum & 0x00FF); readCmdStr[6] = '\\0';

ClearCom(comPort);

SendRtuCmdToModBus(comPort, readCmdStr, readCmdLen);

isReadOK = ReadModBusRtuSlave(comPort, &ModAddrBack, &ModFunctionBack, &ModByteNumBack,

ModDataBack, &ModDataLenBack, MOD_TIMEOUT, MOD_WAITTIME);

if (1 == isReadOK) {

if((ModAddr == ModAddrBack) && (ModFunction == ModFunctionBack) && (ModByteNumBack == ModDataLenBack)) {

ChangeAllButtonsDataByModBus(buttonJudge, ModDataBack, num);

if(Bitmcmp(buttonData, 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(buttonData, num, buttonNVRAMAddr);

}

strmcpy(oldModDataBack, ModDataBack, ModDataLenBack); return 1; } else {

/*收到的字符串不匹配*/ return 1; } } else {

/*未收到字符串或CRC校验出错*/ return 0; } }

MODBUS通讯+C语言源代码 - 图文 

ModDataNum=(unsignedshort)num*2;readCmdStr[0]=ModAddr;readCmdStr[1]=ModFunction;readCmdStr[2]=(unsignedchar)(ModDataAddr>>8
推荐度:
点击下载文档文档为doc格式
80bt478kp07b3ef97wu606i7k4ff8500zdy
领取福利

微信扫码领取福利

微信扫码分享