赞
踩
#include <Modbus.h> #define ModbusSendMaxCount (16*2+9) //串口发送最大字节数,发送 16 Word(Register_Address) #define ModbusReceMaxCount (16*2+9) //串口接收最大字节数,接收 16 Word(Register_Address) unsigned char ModbusSendBuf[ModbusSendMaxCount]; unsigned char ModbusReceBuf[ModbusReceMaxCount]; unsigned char Demo; uchar WBDO15[8];// unsigned int WWAO16[8];// //=========================================================================================== //CRC校验字节值表 //=========================================================================================== //CRC校验高位字节值表 const unsigned char code auchCRCHi[] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 } ; //CRC校验低位字节值表 const unsigned char code auchCRCLo[] = { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36,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unsigned short CRC16 ( unsigned char *puchMsg, unsigned short usDataLen ) { unsigned char uchCRCHi = 0xFF ; //\* 高字节初始化值 \*/ unsigned char uchCRCLo = 0xFF ; //\* 低字节初始化值 \*/ unsigned uIndex ; while (usDataLen--) { uIndex = uchCRCLo ^ *puchMsg++ ; uchCRCLo = uchCRCHi ^ auchCRCHi[uIndex] ; uchCRCHi = auchCRCLo[uIndex]; } return (uchCRCHi << 8 | uchCRCLo) ; } //=========================================================================================== //Modbus功能函数,功能码:01 02 03 04 05 06 //=========================================================================================== void Modbus_Reduce_ReadFunction01_02_03_04_WriteFunction05_06(uchar ModbusSalveAddress,uchar ModbusFunction,uint RegisterStartAddress,uint RegisterNumberOrData) { unsigned short CRC_Data; ModbusSendBuf[0] = ModbusSalveAddress; ModbusSendBuf[1] = ModbusFunction; ModbusSendBuf[2] = RegisterStartAddress>>8; ModbusSendBuf[3] = RegisterStartAddress&0x00ff; ModbusSendBuf[4] = RegisterNumberOrData>>8; ModbusSendBuf[5] = RegisterNumberOrData&0x00ff; CRC_Data = CRC16(ModbusSendBuf,6); ModbusSendBuf[6] = CRC_Data&0x00ff; ModbusSendBuf[7] = CRC_Data>>8; if((ModbusFunction == 0x01)||(ModbusFunction == 0x02)||(ModbusFunction == 0x03)||(ModbusFunction == 0x04)||(ModbusFunction == 0x05)||(ModbusFunction == 0x06)) SendByte(ModbusSendBuf,8); } //=========================================================================================== //打包读多路数字量输出子西数:功能码01 //=========================================================================================== void ModbusReduce_Read_Multiple_Digital_Output (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Number) { Modbus_Reduce_ReadFunction01_02_03_04_WriteFunction05_06(ModbusSlaveAddress,0x01,RegisterStartAddress,Register_Number); } //=========================================================================================== //打包读多路数字量输入子函数:功能码02 //=========================================================================================== void ModbusReduce_Read_Multiple_Digital_Input (uchar ModbusSlaveAddress ,uint RegisterStartAddress, uint Register_Number) { Modbus_Reduce_ReadFunction01_02_03_04_WriteFunction05_06(ModbusSlaveAddress,0x02,RegisterStartAddress,Register_Number); } //=========================================================================================== //打包读多路模拟量输出子函数:功能码03 //=========================================================================================== void ModbusReduce_Read_Multiple_Hold_Registers (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Number) { Modbus_Reduce_ReadFunction01_02_03_04_WriteFunction05_06(ModbusSlaveAddress,0x03,RegisterStartAddress,Register_Number); } //=========================================================================================== //打包读多路模拟量输入子西数:功能码04 //=========================================================================================== void ModbusReduce_Read_Multiple_Analog_Input (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Number) { Modbus_Reduce_ReadFunction01_02_03_04_WriteFunction05_06(ModbusSlaveAddress,0x04,RegisterStartAddress,Register_Number); } //=========================================================================================== //打包写单路数字三输出子西数:功能码05 //=========================================================================================== void ModbusReduce_Write_Single_Digital_Output (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Data) { Modbus_Reduce_ReadFunction01_02_03_04_WriteFunction05_06(ModbusSlaveAddress,0x05,RegisterStartAddress,Register_Data); } //=========================================================================================== //打包写单路模拟量输出子函数:功能码06 //=========================================================================================== void ModbusReduce_Write_Single_Hold_Registers (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Data) { Modbus_Reduce_ReadFunction01_02_03_04_WriteFunction05_06(ModbusSlaveAddress,0x06,RegisterStartAddress,Register_Data); } //=========================================================================================== //打包写多路数字量输出子西数:功能码15 //=========================================================================================== void ModbusReduce_Write_Multiple_Digital_Output(uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Number, uchar *UcharData) { unsigned short CRC_Data; uchar Byte_Count , i; Byte_Count= Register_Number/8; if(Register_Number%8 != 0) Byte_Count++; ModbusSendBuf[0] = ModbusSlaveAddress; ModbusSendBuf[1] = 0x0f; ModbusSendBuf[2] = RegisterStartAddress>>8; ModbusSendBuf[3] = RegisterStartAddress&0x00ff; ModbusSendBuf[4] = Register_Number>>8; ModbusSendBuf[5] = Register_Number&0x00ff; ModbusSendBuf[6] = Byte_Count; for(i= 0;i<Byte_Count;i++) ModbusSendBuf[i+7] = UcharData[i]; CRC_Data = CRC16(ModbusSendBuf,Byte_Count+7); ModbusSendBuf[i+7] = CRC_Data&0x00ff; ModbusSendBuf[i+8] = CRC_Data>>8; SendByte(ModbusSendBuf,Byte_Count+9); } //=========================================================================================== //打包写多路数字量输出子西数:功能码16 //=========================================================================================== void ModbusReduce_Write_Multiple_Hold_Registers (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Number, unsigned int *UintData) { unsigned short CRC_Data; uchar Byte_Count , i; Byte_Count = Register_Number*2; ModbusSendBuf[0] = ModbusSlaveAddress; ModbusSendBuf[1] = 0x10; ModbusSendBuf[2] = RegisterStartAddress>>8; ModbusSendBuf[3] = RegisterStartAddress&0x00ff; ModbusSendBuf[4] = Register_Number>>8; ModbusSendBuf[5] = Register_Number&0x00ff; ModbusSendBuf[6] = Byte_Count; for(i= 0;i<Register_Number;i++) { ModbusSendBuf[i*2+7] = UintData[i]>>8; ModbusSendBuf[i*2+8] = UintData[i]&0x00ff; } CRC_Data = CRC16(ModbusSendBuf,Byte_Count+7); ModbusSendBuf[Byte_Count+7] = CRC_Data&0x00ff; ModbusSendBuf[Byte_Count+8] = CRC_Data>>8; SendByte(ModbusSendBuf,Byte_Count+9); } void DemonstrationReduce(void) { unsigned char i; if(Demo>3) Demo = 0; switch(Demo) { case 0: { ModbusReduce_Read_Multiple_Digital_Output(1,0,10);//Modbus Slave 测试通过 Demo = 1; break; } case 1: { ModbusReduce_Read_Multiple_Digital_Input(1,0,10);//Modbus Slave 测试通过 Demo = 2; break; } case 2: { ModbusReduce_Read_Multiple_Hold_Registers(1,0,6);//Modbus Slave 测试通过 Demo = 3; break; } case 3: { ModbusReduce_Read_Multiple_Analog_Input(1,0,6);//Modbus Slave 测试通过 Demo = 4; break; } case 4: { ModbusReduce_Write_Single_Digital_Output(1,9,0xff00);//Modbus Slave 测试通过,注意最高字节为0xff00才能正确置位相应位地址,地址从0开始计算 Demo = 5; break; } case 5: { ModbusReduce_Write_Single_Hold_Registers(1,0,2222);//Modbus Slave 测试通过 Demo = 6; break; } case 6: { WBDO15[0] = 0x0f; WBDO15[1] = 0x01; ModbusReduce_Write_Multiple_Digital_Output(1,0,10,WBDO15);//Modbus Slave 测试通过 Demo = 7; break; } case 7: { for(i=0;i<6;i++) WWAO16[i] = i+10; ModbusReduce_Write_Multiple_Hold_Registers(1,0,6,WWAO16);//Modbus Slave 测试通过 保持寄存器 Demo = 0; break; } default: { Demo = 0; } } } uchar ReceState = 0; uchar ReceiveDataCounter = 0; uchar ActualDataCounter = 0; uchar ReceCount = 0; sbit LCD7=P3^7; sbit LCD6=P3^6; sbit LCD5=P3^5; sbit LCD4=P3^4; void DemonstrationUnpark(unsigned char ReceiveData) { // unsigned short CRCData; switch (ReceState) { case 0: if(ReceiveData == ModbusSendBuf[0] )//从站地址 { ReceiveDataCounter = 0; ModbusReceBuf[ReceiveDataCounter] = ReceiveData; ReceState = 1; } else ReceState = 0; break; case 1: if(ReceiveData == ModbusSendBuf[1])//Modbus功能码 { ReceiveDataCounter++; ModbusReceBuf[ReceiveDataCounter] = ReceiveData; ReceState = 2; } else ReceState = 0; break; case 2: //接收字节数量 if((ModbusSendBuf[1] == 0x01)||(ModbusSendBuf[1] == 0x02)||(ModbusSendBuf[1] == 0x03)||(ModbusSendBuf[1] == 0x04))//Modbus功能码 { ReceiveDataCounter++; ModbusReceBuf[ReceiveDataCounter] = ReceiveData; ActualDataCounter = ReceiveData; //接收字节数量 ReceState = 3; } else ReceState = 0; break; case 3: if(ReceCount<ActualDataCounter)//开始接收数据 { ReceiveDataCounter++; ModbusReceBuf[ReceiveDataCounter] = ReceiveData; // ucharDataBuff[ReceCount]=ReceiveData; P0 = ~ModbusReceBuf[3]; P2 = ~ModbusReceBuf[4]; ReceCount++; } else//接收完成 { // CRCData = CRC16(ModbusReceBuf,ActualDataCounter+5); // if((ModbusReceBuf[ActualDataCounter+5-2] == (CRCData>>8))&&(ModbusReceBuf[ActualDataCounter+5-1] == (CRCData&0x00ff))) { // P0 = ~ModbusReceBuf[3]; // P2 = ~ModbusReceBuf[4]; } //数据CRC校验判断数据完整性 ReceiveDataCounter = 0; ReceCount = 0; ReceState = 0; } // if(ReceCount == ActualDataCounter) // { // ReceiveDataCounter = 0; // ReceCount = 0; // ReceState = 0; // } break; default: ReceState=0; break; } }
#ifndef __Modbus_H__ #define __Modbus_H__ #include <STC12C5A_Uart2.H> typedef unsigned char uchar; typedef unsigned int uint; extern unsigned char ModbusSendBuf[]; extern unsigned char ModbusReceBuf[]; /* //解包读多路数字量输出子函数:功能码01 extern void ModbusUnpark_Read_Multiple_Digtal_Output(void); //解包读多路数字量输出子函数:功能码02 extern void ModbusUnpark_Read_Multiple_Digtal_Input(void); //解包读多路数字量输出子函数:功能码03 extern void ModbusUnpark_Read_Multiple_Analog_Output(void); //解包读多路数字量输出子函数:功能码04 extern void ModbusUnpark_Read_Multiple_Analog_Input(void); //解包读单路数字量输出子函数:功能码05 extern void ModbusUnpark_Write_Single_Analog_Input(void); //解包写单路数字量输出子函数:功能码06 extern void ModbusUnpark_Write_Singlee_Analog_Output(void); //解包写多路数字量输出子函数:功能码15 extern void ModbusUnpark_Write_Multiple_Digtal_Output(void); //解包写多路数字量输出子函数:功能码16 extern void ModbusUnpark_Write_Multiple_Analog_Output(void);*/ //打包读多路数字量输出子西数:功能码01 extern void ModbusReduce_Read_Multiple_Digital_Output (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Number) ; //打包读多路数字量输入子函数:功能码o2 extern void ModbusReduce_Read_Multiple_Digital_Input (uchar ModbusSlaveAddress ,uint RegisterStartAddress, uint Register_Number) ; //打包读多路模拟量输出子函数:功能码03 extern void ModbusReduce_Read_Multiple_Hold_Registers (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Number); //打包读多路模拟量输入子西数:功能码04 extern void ModbusReduce_Read_Multiple_Analog_Input (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Number); //打包写单路数字三输出子西数:功能码g5 extern void ModbusReduce_Write_Single_Digital_Output (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Data); //打包写单路模拟量输出子函数:功能码06 extern void ModbusReduce_Write_Single_Hold_Registers (uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Data); //打包写多路数字量输出子西数:功能码15 extern void ModbusReduce_Write_Multiple_Digital_Output(uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Number, uchar *UcharData); //打包写多路数字量输出子西数:功能码16 extern void ModbusReduce_Write_Multiple_Hold_Registers(uchar ModbusSlaveAddress, uint RegisterStartAddress, uint Register_Number, uint *UintData); extern void DemonstrationReduce(void);//在主程序里调用,发送指定的功能码 extern void DemonstrationUnpark(unsigned char ReceiveData) ;//解包程序在串口中断程序中调用,按字节接收 #endif
DemonstrationReduce();
DemonstrationUnpark(unsigned char ReceiveData);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。