新手上路
- 积分
- 47
- 金钱
- 47
- 注册时间
- 2018-4-21
- 在线时间
- 13 小时
|
楼主 |
发表于 2021-4-19 10:28:10
|
显示全部楼层
做了几天,先写好modbus读取各类指令的函数,再在主函数配置触发轮循读取。
#include "Modbus485.h"
#include "USART.h"
#include "TempandHumidityAD.h"
#include "Fault.h"
uint16_t SlaveAddr; //从机地址
uint8_t FunctionCode;//功能码
uint16_t StartAddr;//起始地址
uint16_t OutputAddr;//输出地址
uint16_t RegisterNum;//寄存器数量
uint8_t WrongCode;//差错码
uint8_t ErrorCode;//异常码
uint16_t SysSwitchOrder;//系统开关指令
uint16_t AirCleanSwitchOrder; //空净开关指令
uint16_t SelectorSwitchOrder; //切换开关指令
uint8_t time200ms;
uint8_t time200msflag;
uint8_t Flag_of_Modbus_Ok = 0;
void time200msdly(void)
{
if(time200ms>=200)
{
time200msflag = 1;
time200ms = 0;
}
}
// CRC 高位字节值表
static const uint8_t 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 低位字节值表
static const uint8_t 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,
0x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5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
};
/* 扩展变量 ------------------------------------------------------------------*/
/* 私有函数原形 --------------------------------------------------------------*/
/* 函数体 --------------------------------------------------------------------*/
/**
* 函数功能: Modbus CRC16 校验计算函数
* 输入参数: pushMsg:待计算的数据首地址,usDataLen:数据长度
* 返 回 值: CRC16 计算结果
* 说 明: 计算结果是高位在前,需要转换才能发送
*/
uint16_t modbus_CRC16(uint8_t *_pushMsg,uint8_t _usDataLen)
{
uint8_t uchCRCHi = 0xFF;
uint8_t uchCRCLo = 0xFF;
uint16_t uIndex;
while(_usDataLen--)
{
uIndex = uchCRCLo ^ *_pushMsg++;
uchCRCLo = uchCRCHi^auchCRCHi[uIndex];
uchCRCHi = auchCRCLo[uIndex];
}
return (uchCRCHi<<8|uchCRCLo);
}
uint8_t RegisterLen;
//接收
volatile uint8_t ModbusFrame = 0;
volatile uint16_t ModbusRxCount=0;
volatile uint8_t ModbusRxBuff[MODBUS_RXBUFF_SIZE]={0};
//发送
volatile uint16_t ModbusTxCount =0 ;
volatile uint8_t ModbusTxBuff[MODBUS_TXBUFF_SIZE]={0};
void ReadSysValue(void) //读取系统温度湿度
{
uint8_t DataLen;
uint8_t Len;
uint16_t crc;
//ModbusTxBuff[0] =
ModbusTxBuff[0] = SlaveAddr>>8;
ModbusTxBuff[1] = (SlaveAddr&0xff);
ModbusTxBuff[2] = FunctionCode;
ModbusTxBuff[3] = RegisterNum*2;
// ModbusTxBuff[4] = ((uint16_t)(EvaporateCoil_Tem))>>8;
// ModbusTxBuff[5] = ((uint16_t)(EvaporateCoil_Tem)&0xff);
// ModbusTxBuff[6] = ((uint16_t)(CompressorFunOut_Tem))>>8;
// ModbusTxBuff[7] = ((uint16_t)(CompressorFunOut_Tem)&0xff);
// ModbusTxBuff[8] = ((uint16_t)(CompressorFunIn_Tem))>>8;
// ModbusTxBuff[9] = ((uint16_t)(CompressorFunIn_Tem)&0xff);
ModbusTxBuff[4] = ((uint16_t)(Environment_Tem))>>8;
ModbusTxBuff[5] = ((uint16_t)(Environment_Tem)&0xff);
ModbusTxBuff[6] = ((uint16_t)(Environment_Humidity))>>8;
ModbusTxBuff[7] = ((uint16_t)(Environment_Humidity)&0xff);
//RegisterLen = 10;
DataLen = 10;
crc = modbus_CRC16((uint8_t*)&ModbusTxBuff,DataLen);//校验
ModbusTxBuff[8] = crc&0xff; //crc低字节
ModbusTxBuff[9] = crc>>8; //crc高字节
Len = 16;
RS485_Send_Data((uint8_t*)ModbusTxBuff,Len);
}
void ReadSysFaultCode(void) //读故障码
{
uint8_t DataLen;
uint8_t Len;
uint16_t crc;
ModbusTxBuff[0] = SlaveAddr>>8;
ModbusTxBuff[1] = (SlaveAddr&0xff);
ModbusTxBuff[2] = FunctionCode;
ModbusTxBuff[3] = RegisterNum*2;
ModbusTxBuff[4] = CommFualtCode;
ModbusTxBuff[5] = CompressorFunInSensorFaultCode;
ModbusTxBuff[6] = CompressorFunInTemLowFaultCode;
ModbusTxBuff[7] = CompressorFunOutSensorFaultCode;
ModbusTxBuff[8] = CompressorFunOutTemOverFaultCode;
ModbusTxBuff[9] = EvaporateCoil_TemSensorFaultCode;
ModbusTxBuff[10] = EvaporateCoil_TemLowFaultCode;
ModbusTxBuff[11] = Environment_TemSensorFaultCode;
ModbusTxBuff[12] = Environment_HumiditySensorFaultCode;
ModbusTxBuff[13] = SysLackFreonFaultCode;
ModbusTxBuff[14] = SysCurrentOveraultCode;
ModbusTxBuff[15] = 0xFF;
//RegisterLen = 12;
DataLen = 16;
crc = modbus_CRC16((uint8_t*)&ModbusTxBuff,DataLen);//校验
ModbusTxBuff[16] = crc&0xff; //crc低字节
ModbusTxBuff[17] = crc>>8; //crc高字节
Len = 18;
RS485_Send_Data((uint8_t*)ModbusTxBuff,Len);
}
void WriteSysSwitch(void) //系统开关命令相应
{
uint8_t DataLen;
uint8_t Len;
uint16_t crc;
ModbusTxBuff[0] = SlaveAddr>>8;
ModbusTxBuff[1] = (SlaveAddr&0xff);
ModbusTxBuff[2] = FunctionCode;
ModbusTxBuff[3] = OutputAddr>>8;
ModbusTxBuff[4] = OutputAddr&0xff;
ModbusTxBuff[5] = SysSwitchOrder>>8;
ModbusTxBuff[6] = SysSwitchOrder&0xff;
DataLen = 7;
crc = modbus_CRC16((uint8_t*)&ModbusTxBuff,DataLen);//校验
ModbusTxBuff[7] = crc&0xff; //crc低字节
ModbusTxBuff[8] = crc>>8; //crc高字节
Len = 9;
RS485_Send_Data((uint8_t*)ModbusTxBuff,Len);
}
void WriteAirCleanSwitch(void) //空净开关命令响应
{
uint8_t DataLen;
uint8_t Len;
uint16_t crc;
ModbusTxBuff[0] = SlaveAddr>>8;
ModbusTxBuff[1] = (SlaveAddr&0xff);
ModbusTxBuff[2] = FunctionCode;
ModbusTxBuff[3] = OutputAddr>>8;
ModbusTxBuff[4] = OutputAddr&0xff;
ModbusTxBuff[5] = AirCleanSwitchOrder>>8;
ModbusTxBuff[6] = AirCleanSwitchOrder&0xff;
DataLen = 7;
crc = modbus_CRC16((uint8_t*)&ModbusTxBuff,DataLen);//校验
ModbusTxBuff[7] = crc&0xff; //crc低字节
ModbusTxBuff[8] = crc>>8; //crc高字节
Len = 9;
RS485_Send_Data((uint8_t*)ModbusTxBuff,Len);
}
void WriteSelectorSwitch(void) //切换开关命令响应
{
uint8_t DataLen;
uint8_t Len;
uint16_t crc;
ModbusTxBuff[0] = SlaveAddr>>8;
ModbusTxBuff[1] = (SlaveAddr&0xff);
ModbusTxBuff[2] = FunctionCode;
ModbusTxBuff[3] = OutputAddr>>8;
ModbusTxBuff[4] = OutputAddr&0xff;
ModbusTxBuff[5] = SelectorSwitchOrder>>8;
ModbusTxBuff[6] = SelectorSwitchOrder&0xff;
DataLen = 7;
crc = modbus_CRC16((uint8_t*)&ModbusTxBuff,DataLen);//校验
ModbusTxBuff[7] = crc&0xff; //crc低字节
ModbusTxBuff[8] = crc>>8; //crc高字节
Len = 9;
RS485_Send_Data((uint8_t*)ModbusTxBuff,Len);
}
void ErrResponse(uint8_t FunCode,uint8_t ErrorCode)
{
uint8_t DataLen;
uint8_t Len;
uint16_t crc;
ModbusTxBuff[0] = SlaveAddr>>8;
ModbusTxBuff[1] = (SlaveAddr&0xff);
ModbusTxBuff[2] = 0x80 + FunCode;
ModbusTxBuff[3] = ErrorCode;
DataLen = 4;
crc = modbus_CRC16((uint8_t*)&ModbusTxBuff,DataLen);//校验
ModbusTxBuff[4] = crc&0xff; //crc低字节
ModbusTxBuff[5] = crc>>8; //crc高字节
Len = 6;
RS485_Send_Data((uint8_t*)ModbusTxBuff,Len);
}
//响应
void Modbuss_Analyze_RSP(void)
{
uint8_t err = EX_CODE_NONE;
// uint16_t crc =0;
volatile uint16_t _RegNum;
volatile uint16_t _Addr;
// _Addr = (ModbusRxBuff[2] <<8) | ModbusRxBuff[3];
// _RegNum = (ModbusRxBuff[4] <<8) | ModbusRxBuff[5];
// ModbusTxCount =0;
//
// ModbusTxBuff[ModbusTxCount++] = MB_SLAVEADDR; /* 从站地址 */
// ModbusTxBuff[ModbusTxCount++] = ModbusRxBuff[1]; /* 功能码 + 0x80*/
SlaveAddr = uart_buff[0];
FunctionCode = uart_buff[1];
StartAddr = (uart_buff[2]<<8)|uart_buff[3];
OutputAddr = (uart_buff[2]<<8)|uart_buff[3];
RegisterNum = ((uart_buff[4]<<8)|uart_buff[5])*2;
WrongCode = 0x80 + FunctionCode;
//参数合法性判断
switch(FunctionCode)//功能码判断
{
case FUN_CODE_03H : //读多个寄存器
{
if((RegisterNum <0x0001) || (RegisterNum>0x007D))//长度判断
{
err =EX_CODE_03H; //寄存器数量超过长度限制
ErrResponse(FunctionCode,err);
}
else
{
if( StartAddr==0x0000)
{
if( RegisterNum > 10)//寄存器数量判断
{
err = EX_CODE_02H;// 异常码 02H 寄存器数量不匹配
ErrResponse(FunctionCode,err);//读故障代码
}
else
{
// ReadSysValue();//按键读取
}
}
else if(StartAddr == 0x0005)
{
if( RegisterNum > 12)//寄存器数量判断
{
err = EX_CODE_02H;// 异常码 02H 寄存器数量不匹配
ErrResponse(FunctionCode,err);
}
else
{
// ReadSysFaultCode();//按键读取
}
}
else
{
err = EX_CODE_04H; //非法起始地址
ErrResponse(FunctionCode,err);
}
}
// else
// {
// ModbusTxBuff[ModbusTxCount++] = _RegNum*2;
// //MCGS_Read_Data((unsigned short *)&ModbusTxBuff[3],_Addr,_RegNum);
//
// ModbusTxCount += (_RegNum*2);
// }
}
break;
case FUN_CODE_05H : //写单个线圈
{
if((RegisterNum <0x0001) || (RegisterNum>0x007B))//长度判断
{
err = EX_CODE_03H; //寄存器数量超过长度限制
ErrResponse(FunctionCode,err);
}
else
{
if(OutputAddr==0x0000)
{
WriteSysSwitch();
}
else if(OutputAddr==0x0001)
{
WriteAirCleanSwitch();
}
else if(OutputAddr==0x0002)
{
WriteSelectorSwitch();
}
else
{
err = EX_CODE_04H; //非法输出地址
ErrResponse(FunctionCode,err);
}
}
}
break;
default :
err = EX_CODE_01H; //不支持的功能码
ErrResponse(FunctionCode,err);
break;
}
}
//数据分析函数
void Modbus_Analyze_Execute(void )
{
if(Flag_of_Modbus_Ok ==1)
{
//未收到数据或者数据不完整
if(ModbusRxCount == 0 )
{
return ;
}
//判断收到的数据合法性 数据长度不足
if((ModbusRxCount <=3) || ModbusRxBuff[0] != MB_SLAVEADDR ||
(ModbusRxBuff[ModbusRxCount-1]<<8 | ModbusRxBuff[ModbusRxCount-2]) != modbus_CRC16((uint8_t *)ModbusRxBuff,ModbusRxCount-2))
{
Modbus_Reset_Rx();
return ;
}
//执行
Modbuss_Analyze_RSP();
//数据重置
Modbus_Reset_Tx();
Modbus_Reset_Rx();
ModbusRxCount = 0;
Flag_of_Modbus_Ok = 0;
}
else
{
//数据重置
Modbus_Reset_Tx();
Modbus_Reset_Rx();
ModbusRxCount = 0;
Flag_of_Modbus_Ok = 0;
}
}
void Debuge485_test(void) //系统开关命令相应
{
uint8_t DataLen;
uint8_t Len;
uint16_t crc;
ModbusTxBuff[0] = 0x01;
ModbusTxBuff[1] = 0x02;
ModbusTxBuff[2] = 0x03;
ModbusTxBuff[3] = 0x04;
ModbusTxBuff[4] = 0x05;
ModbusTxBuff[5] = 0x06;
ModbusTxBuff[6] = 0x07;
DataLen = 7;
crc = modbus_CRC16((uint8_t*)&ModbusTxBuff,DataLen);//校验
ModbusTxBuff[7] = crc&0xff; //crc低字节
ModbusTxBuff[8] = crc>>8; //crc高字节
Len = 9;
RS485_Send_Data((uint8_t*)ModbusTxBuff,Len);
}
|
|