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

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

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

*修改日志:

*[2006-3-1 14:03] Ver. 1.00 开始编写; *[2006-3-1 14:10] 完成; /* */ /*********************************************************************************/

void ChangeAllButtonsDataByModBus(int buttonData[], unsigned char ModDataBack[], unsigned short num) {

unsigned char button8Data = 0; int i, j;

for(j = 0; j < (int)(num / 8); j ++) {

button8Data = ModDataBack[j]; for(i = 0; i < 8; i ++) {

buttonData[i + j * 8] = (int)(button8Data & (int) pow(2, i)) / (int) pow(2, i); } }

button8Data = ModDataBack[j]; for(i = 0; i < num % 8; i ++) {

buttonData[i + j * 8] = (int)(button8Data & (int) pow(2, i)) / (int) pow(2, i); } }

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

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

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

(1)parameterData 或AIData转化为MODBUSSLAVE的WORD值的STR;

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

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

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

void WordDataToModbusStr(unsigned char writeCmdStr[], float data[], unsigned short num, unsigned short startAddr) {

int i;

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

unsigned char *pstr;

float dataf = 0; int len = 4;

pf = &dataf;

pstr = singleStr;

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

*pf = data[i];

memcpy(pstr, pf, len);

writeCmdStr[startAddr + i * 4 + 3] = singleStr[0]; writeCmdStr[startAddr + i * 4 + 2] = singleStr[1]; writeCmdStr[startAddr + i * 4 + 1] = singleStr[2];

writeCmdStr[startAddr + i * 4 ] = singleStr[3]; } }

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

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

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

(1)data转化为MODBUSSLAVE的WORD值的STR; *修改日志:

*[2006-3-1 14:51] Ver. 1.00 开始编写; *[2006-3-1 15:02] 完成; /* */ /*********************************************************************************/

void BitDataToModbusStr(unsigned char writeCmdStr[], int data[], unsigned short num, unsigned short startAddr) {

int i, j;

unsigned char button8Data = 0;

for(j = 0; j < (int)(num / 8); j ++) {

button8Data = 0;

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

button8Data += data[i + j * 8] * (int) pow(2, i); }

writeCmdStr[startAddr + j] = button8Data; }

if(num % 8) {

button8Data = 0;

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

button8Data += data[i + j * 8] * (int) pow(2, i); }

writeCmdStr[startAddr + j] = button8Data; } }

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

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

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

(1) 从触摸屏中读取设定参数值; (2) 读取指令为 01 03 HA LA HN LN HC LC;其中 01 为MODBUSSLAVE的地址,

03 为读连续多个WORD寄存器的功能码,HA LA 为第一个寄存器的高低位地址,

HN LN 为寄存器数量的高低位,HC LC 为CRC校验码; (3) 触摸屏reset后各个参数归零,7188检测到此状态后向除摸屏发送设定参数; *修改日志:

*[2005-11-28 16:51] Ver. 1.00 开始编写; *[2005-11-28 18:10]

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

static unsigned char oldModDataBack[400];改为形参; *[2006-3-3 17:58]

增加int kind形参; *[2006-3-6 16:34]

strmcpy(oldModDataBack, ModDataBack, ModDataLenBack); *[2006-3-9 12:33]

去掉 (strmcmp(oldModDataBack, ModDataBack, ModDataLenBack)) /* */ /********************************************************************

*************/ int ReadWordFromModBusSlave(int kind, int comPort, float parameterData[], unsigned short num, unsigned short ModDataAddr, int parameterEEPROMAddr, unsigned char oldModDataBack[]) {

float parameterJudge[50];

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

unsigned char ModFunction = 3;

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[MOD_STR_MAX_LEN] = {'\\0'};

unsigned char ModDataLenBack = 0;

/* unsigned short ModWord[100]; */ int parameterDecimalEEPROM[PARAMETER_DATA_NUM] PARAMETER_DECIMAL_EEPROM; /* adjust */ int parameterStartAddr = 0; 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); }

=

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

*修改日志:*[2006-3-114:03]Ver.1.00开始编写;*[2006-3-114:10]完成;/**//************
推荐度:
点击下载文档文档为doc格式
80bt478kp07b3ef97wu606i7k4ff8500zdy
领取福利

微信扫码领取福利

微信扫码分享