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

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

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

unsigned char ModAddr = 1;

unsigned char ModFunction = 5;

unsigned short ModDataState; unsigned short readCmdLen = 6; /* int isReadOK = 0;

unsigned short byteCount;

unsigned char totalStrBack[100]; unsigned char ModAddrBack = 0; unsigned char ModFunctionBack = 0; unsigned short ModDataAddrBack; unsigned short ModDataStateBack; */

ModDataState = DOData[num];

writeCmdStr[0] = ModAddr;

writeCmdStr[1] = ModFunction;

writeCmdStr[2] = (unsigned char)(ModDataAddr >> 8 & 0x00FF); writeCmdStr[3] = (unsigned char)(ModDataAddr & 0x00FF);

if(ModDataState) writeCmdStr[4] = (unsigned char)(0x00FF);

else writeCmdStr[4] = (unsigned char)(0x0000); writeCmdStr[5] = (unsigned char)(0x0000); writeCmdStr[6] = '\\0';

/* ClearCom(comPort); */

SendRtuCmdToModBus(comPort, writeCmdStr, readCmdLen); return 1;

/*

isReadOK = ResponseSingleBitFromModBus(comPort, totalStrBack, &ModAddrBack, &ModFunctionBack, &ModDataAddrBack, &ModDataStateBack, MOD_TIMEOUT, MOD_WAITTIME);

if (1 == isReadOK) {

Print(\%u %u %u %u %u %u %u %u\ModAddr, ModAddrBack, ModFunction, ModFunctionBack, ModDataAddr, ModDataAddrBack, ModDataState, ModDataStateBack);

if((ModAddr == ModAddrBack) && (ModFunction == ModFunctionBack) && (ModDataAddr == ModDataAddrBack)

&& (ModDataState == ModDataStateBack)) {

Print(\ return 1; } else {

Print(\ return 0; } } else {

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

/*-------------------------------------------------------------------------------*/

/*********************************************************************************/ /*函数名称: WriteBitByBitToModBusSlave()

*输入参数: 共 个参数; *输出参数: 共 个参数; *返回值: 成功与否;

*需储存的参数: 共 个参数; *功能介绍:

(1) 一个一个地将BIT写如触摸屏中; (2) 可能很耗时; *修改日志:

*[2006-3-7 14:40] Ver. 1.00 开始编写; *[2006-3-7 14:51]

完成; /* */ /*********************************************************************************/ int WriteBitByBitToModBusSlave(int comPort, int DOData[], int dataOld[], unsigned short num, unsigned short ModDataAddr)

{

int i;

for(i = 0; i < num; i ++) {

if(DOData[i] != dataOld[i]) { DelayMs(1);

WriteSingleBitToModBusSlave(comPort, DOData, i, ModDataAddr + i); } }

return 1; }

/*-------------------------------------------------------------------------------*/

/*********************************************************************************/ /*函数名称: WriteAllBitByBitToModBusSlave() *输入参数: 共 个参数; *输出参数: 共 个参数; *返回值: 成功与否;

*需储存的参数: 共 个参数; *功能介绍:

(1) 一个一个地将BIT写如触摸屏中; (2) 可能很耗时; *修改日志:

*[2006-3-9 12:52] Ver. 1.00 开始编写; *[2006-3-9 12:52]

完成; /* */ /*********************************************************************************/

int WriteAllBitByBitToModBusSlave(int comPort, int DOData[], unsigned short num, unsigned short ModDataAddr) {

int i;

for(i = 0; i < num; i ++) { DelayMs(1);

WriteSingleBitToModBusSlave(comPort, DOData, i, ModDataAddr + i); }

return 1; }

/*-------------------------------------------------------------------------------*/

/*********************************************************************************/

/*函数名称: ListenToTouch() *输入参数: 共 个参数; *输出参数: 共 个参数; *返回值: 无;

*需储存的参数: 共 个参数; *功能介绍:

(1)监控Touch触摸屏;

(2) init的工作给 WriteMultipleWordToModBusSlave 是否成功。

(3) 必须要DelayMs(50)才能使一下个response正确。 *修改日志:

*[2006-3-1 17:19] Ver. 1.00 开始编写; *[2006-3-1 17:33] 完成; *[2006-3-9 14:10]

测试通过; /* */ /*********************************************************************************/ void ListenToTouch(int comPort, int buttonData[], float parameterData[], int DOData[], float AIData[]) {

int kind = 1; /* adjust */

unsigned short parameterNum = PARAMETER_DATA_NUM;

unsigned short parameterAddr = PARAMETER_MODBUS_ADDR_1; /* adjust

*/

unsigned short buttonNum = BUTTON_DATA_NUM; unsigned short buttonAddr = BUTTON_MODBUS_ADDR_1; /* adjust */

unsigned short DONum = DIGITAL_OUT_DATA_NUM;

unsigned short DOAddr = DO_MODBUS_ADDR_1; /* adjust */

unsigned short AINum = ANALOG_IN_DATA_NUM;

unsigned short AIAddr = AI_MODBUS_ADDR_1; /* adjust */

int buttonNVRAMAddr = BUTTON_ADDR_1; /* adjust */

int parameterEEPROMAddr = EEPROM_BLOCK; /* adjust */

static unsigned char oldModWordBack[MOD_STR_MAX_LEN] = {'\\0'}; /* 以前的str */

static unsigned char oldModBitBack[MOD_STR_MAX_LEN] = {'\\0'};

static int responseFromTouch_init = 0; /*首次初始化*/ /* static int responseFromTouch_P = 0;

static int responseFromTouch_Btn = 0;

static int oldButtonData[30] = {0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};

static int oldDOData[30] = {0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0}; */

int response; /*test*/

/* Print(\ */ if(0 == responseFromTouch_init) /* 第一次写入Touch_Btn失败重新写 */ { /* responseFromTouch_Btn = WriteMultipleBitToModBusSlave(comPort, buttonData, buttonNum, buttonAddr);

*/ response = WriteAllBitByBitToModBusSlave(comPort, buttonData, buttonNum, buttonAddr);

/* bitmcpy(oldButtonData, buttonData, buttonNum); */ } else {

/* 读取按钮值 */ response = ReadBitFromModBusSlave(kind, comPort, buttonData,

buttonNum, buttonAddr, buttonNVRAMAddr, oldModBitBack); }

DelayMs(50);

/* Print(\ */

if(0 == responseFromTouch_init) /* 第一次写入Touch_P失败重新写 */

{ responseFromTouch_init = WriteMultipleWordToModBusSlave(comPort, parameterData, parameterNum, parameterAddr);

response = responseFromTouch_init; /* responseFromTouch_P = 1; /*test*/ } else {

/* 读取设定参数 */

response = ReadWordFromModBusSlave(kind, comPort, parameterData, parameterNum, parameterAddr, parameterEEPROMAddr, oldModWordBack); }

/* Print(\ Print(\ */ DelayMs(2);

/* 写入开关量 */

/* response = WriteMultipleBitToModBusSlave(comPort, DOData, DONum, DOAddr); */

response = WriteAllBitByBitToModBusSlave(comPort, DOData, DONum, DOAddr);

/* bitmcpy(oldDOData, DOData, DONum); */

/* Print(\ */ DelayMs(40); /* 写入采样值 */ response = WriteMultipleWordToModBusSlave(comPort, AIData, AINum, AIAddr);

/* Print(\ */ if(response); }

/*-------------------------------------------------------------------------------*/

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

unsignedcharModAddr=1;unsignedcharModFunction=5;unsignedshortModDataState;unsignedshortreadCmdLen=6;/*intisReadOK=0;
推荐度:
点击下载文档文档为doc格式
80bt478kp07b3ef97wu606i7k4ff8500zdy
领取福利

微信扫码领取福利

微信扫码分享