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

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

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

unsigned char *ModAddr, unsigned char *ModFunction, unsigned short *ModDataAddr,

unsigned short *ModDataNum, unsigned ModTimeout, unsigned waitTime) {

unsigned char data; unsigned long t;

unsigned short CRC16; int ModBusIdx = 0; int counter = 0;

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

while((!(IsCom(ModPort)) && (counter < waitTime))) {

DelayMs(1); counter ++; }

StopWatchStart(MODBUS_STOP_WATCH); do {

while(IsCom(ModPort) && (ModBusIdx < MOD_STR_MAX_LEN - 1)) {

data = ReadCom(ModPort);

ModBusMessage[ModBusIdx ++]=data; StopWatchStart(MODBUS_STOP_WATCH); /* Print(\ }

StopWatchReadValue(MODBUS_STOP_WATCH, &t); }

while(t <= ModTimeout);

if (ModBusIdx == 0) return 0; ModBusMessage[ModBusIdx] = '\\0';

strmcpy(totalStr, ModBusMessage, ModBusIdx); StopWatchStop(MODBUS_STOP_WATCH);

CRC16 = GetCRC16(ModBusMessage,ModBusIdx - 2);

if((unsigned char)(CRC16 >> 8 & 0x00FF) == ModBusMessage[ModBusIdx - 2] && (unsigned char)(CRC16 & 0x00FF) == ModBusMessage[ModBusIdx - 1]) {

*ModAddr = ModBusMessage[0];

*ModFunction = ModBusMessage[1];

*ModDataAddr = (unsigned short)((unsigned short)ModBusMessage[2] << 8 | ModBusMessage[3]);

*ModDataNum = (unsigned short)((unsigned short)ModBusMessage[4] << 8 | ModBusMessage[5]); return 1; }

else {

return 0;

/* CRC16 error */

/* Print(\ } }

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

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

*输入参数: 共 8 个参数; *输出参数: 共 5 个参数;

*返回值: 成功与否 1:成功, 2:失败; *需储存的参数: 共 0 个参数; *功能介绍:

(1)写参数到ModBusRtuSlave后,ModbusRtuSlave反馈字符串; *修改日志:

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

int ResponseSingleBitFromModBus(int ModPort, unsigned char

totalStr[], unsigned char *ModAddr, unsigned char *ModFunction, unsigned short *ModDataAddr,

unsigned short *ModDataState, unsigned ModTimeout, unsigned waitTime) {

unsigned char data; unsigned long t;

unsigned short CRC16; int ModBusIdx = 0; int counter = 0;

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

while((!(IsCom(ModPort)) && (counter < waitTime))) {

DelayMs(1); counter ++; }

StopWatchStart(MODBUS_STOP_WATCH); do {

while(IsCom(ModPort) && (ModBusIdx < MOD_STR_MAX_LEN - 1)) {

data = ReadCom(ModPort);

ModBusMessage[ModBusIdx ++]=data; StopWatchStart(MODBUS_STOP_WATCH); /* Print(\ }

StopWatchReadValue(MODBUS_STOP_WATCH, &t); }

while(t <= ModTimeout);

if (ModBusIdx == 0) return 0; ModBusMessage[ModBusIdx] = '\\0';

strmcpy(totalStr, ModBusMessage, ModBusIdx); StopWatchStop(MODBUS_STOP_WATCH);

CRC16 = GetCRC16(ModBusMessage,ModBusIdx - 2);

if((unsigned char)(CRC16 >> 8 & 0x00FF) == ModBusMessage[ModBusIdx - 2] && (unsigned char)(CRC16 & 0x00FF) == ModBusMessage[ModBusIdx - 1]) {

*ModAddr = ModBusMessage[0]; *ModFunction = ModBusMessage[1];

*ModDataAddr = (unsigned short)((unsigned short)ModBusMessage[2] << 8 | ModBusMessage[3]);

if(ModBusMessage[4]) *ModDataState = 1; else *ModDataState = 0; return 1; }

else {

return 0;

/* CRC16 error */

/* Print(\ } }

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

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

*输入参数: 共 个参数; *输出参数: 共 个参数; *返回值: 无

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

(1) 将MODBUSSLAVE返回的数据字符转化为参数值;

(2) void *memcpy(void *dest, const void *src, size_t n) (3) 四个字符的顺序应倒一下。 *修改日志:

*[2006-3-1 14:10] Ver. 1.00 开始编写; *[2006-3-1 14:35] 完成; *[2006-3-6 15:57]

四个字符的顺序应倒一下。

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

void ChangeAllParameterDataByModBus(float parameterData[], unsigned char ModDataBack[], unsigned short num) {

int i;

char singleStr[5] = {'\\0','\\0','\\0','\\0','\\0'}; float *pf;

float dataf = 0; int len = 4;

pf = &dataf;

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

singleStr[0] = ModDataBack[i * 4 + 3]; singleStr[1] = ModDataBack[i * 4 + 2]; singleStr[2] = ModDataBack[i * 4 + 1]; singleStr[3] = ModDataBack[i * 4 ]; singleStr[4] = '\\0';

memcpy(pf, singleStr, len); parameterData[i] = *pf;

/* Print(\ /*test*/ } }

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

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

*输入参数: 共 个参数; *输出参数: 共 个参数; *返回值: 无

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

(1) 将MODBUSSLAVE返回的数据字符转化为按钮值;

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

unsignedchar*ModAddr,unsignedchar*ModFunction,unsignedshort*ModDataAddr,unsignedshort*ModDataNum,unsignedModTimeout,unsignedwaitTime){unsi
推荐度:
点击下载文档文档为doc格式
80bt478kp07b3ef97wu606i7k4ff8500zdy
领取福利

微信扫码领取福利

微信扫码分享