//基本參數(shù)
#define baudrate 115200 //定義通訊波特率
#define slaveID 1 //定義modbus RTU從站站號(hào)
#define modbusDataSize 100 //定義modbus數(shù)據(jù)庫空間大小,可根據(jù)實(shí)際情況自行修改大小
unsigned int modbusData[modbusDataSize]={}; //建立modbus數(shù)據(jù)庫
//系統(tǒng)參數(shù)
#define bufferSize 255 //一幀數(shù)據(jù)的最大字節(jié)數(shù)量
unsigned char frame[bufferSize]; //用于保存接收或發(fā)送的數(shù)據(jù)
HardwareSerial* ModbusPort;
//函數(shù)聲明
unsigned int calculateCRC(unsigned char* _regs,unsigned char arraySize); //聲明CRC校驗(yàn)函數(shù)
void modbusRTU_slave(); //聲明modbus RTU從站函數(shù)
void responseError(unsigned char ID,unsigned char function,unsigned char wrongNumber); //聲明錯(cuò)誤信息返回函數(shù)
void modbusRTU_INI(HardwareSerial *SerialPort); //聲明modbus RTU端口初始化函數(shù)
//初始化函數(shù)
void setup()
{
delay(100);
modbusRTU_INI(&Serial); //定義modbus通訊端口 端口0:&Serial 端口1:&Serial1 端口2:&Serial2
}
//主循環(huán)
void loop()
{
modbusRTU_slave(); //執(zhí)行modbus函數(shù)
}
//modbus RTU端口初始化函數(shù)
//參數(shù):端口號(hào)
void modbusRTU_INI(HardwareSerial *SerialPort)
{
ModbusPort = SerialPort;
(*ModbusPort).begin(baudrate);
(*ModbusPort).flush();
}
//modbus RTU從站函數(shù)
//支持功能碼03,06,16
void modbusRTU_slave()
{
unsigned int characterTime; //字符時(shí)間
unsigned char errorFlag=0; //錯(cuò)誤標(biāo)志
unsigned int crc16; //校驗(yàn)位
unsigned char address=0;
if (baudrate > 19200) //波特率大于19200時(shí)進(jìn)入條件
{
characterTime = 750;
}
else
{
characterTime = 15000000/baudrate; //1.5字符時(shí)間
}
while((*ModbusPort).available()>0) //如果串口緩沖區(qū)數(shù)據(jù)量大于0進(jìn)入條件
{
if(address
{
frame[address]=(*ModbusPort).read();
address++;
}
else //條件不滿足時(shí)直接清空緩沖區(qū)
{
(*ModbusPort).read();
}
delayMicroseconds(characterTime); //等待1.5個(gè)字符時(shí)間
if((*ModbusPort).available()==0) //1.5個(gè)字符時(shí)間后緩沖區(qū)仍然沒有收到數(shù)據(jù),認(rèn)為一幀數(shù)據(jù)已經(jīng)接收完成,進(jìn)入條件
{
unsigned char function=frame[1]; //讀取功能碼
if(frame[0]==slaveID||frame[0]==0) //站號(hào)匹配或者消息為廣播形式,進(jìn)入條件
{
crc16 = ((frame[address - 2] << 8) | frame[address - 1]);
if(calculateCRC(&frame[0],address - 2)==crc16) //數(shù)據(jù)校驗(yàn)通過,進(jìn)入條件
{
if (frame[0]!=0 && (function == 3)) //功能碼03不支持廣播消息
{
unsigned int startData=((frame[2] << 8) | frame[3]); //讀取modbus數(shù)據(jù)庫起始地址
unsigned int dataSize=((frame[4] << 8) | frame[5]); //需要讀取的modbus數(shù)據(jù)庫數(shù)據(jù)長度
unsigned int endData=startData+dataSize; //需要讀取的modbus數(shù)據(jù)庫數(shù)據(jù)的結(jié)束地址
unsigned char responseSize=5+dataSize*2; //計(jì)算應(yīng)答的數(shù)據(jù)長度
unsigned int temp1,temp2,temp3;
if(dataSize>125 || endData>=modbusDataSize) //讀取數(shù)據(jù)的結(jié)束地址超過了modbus數(shù)據(jù)庫的范圍或單次讀取的數(shù)據(jù)數(shù)量大于125
{
errorFlag=0x02; //數(shù)據(jù)超過范圍
responseError(slaveID,function,errorFlag); //返回錯(cuò)誤消息
}
else
{
frame[0]=slaveID; //設(shè)定站號(hào)
frame[1]=function; //設(shè)定功能碼
frame[2]=dataSize*2; //設(shè)定數(shù)據(jù)長度
temp3=3;
for(temp1=startData;temp1
{
temp2=modbusData[temp1]; //取出modbus數(shù)據(jù)庫中的數(shù)據(jù)
frame[temp3]=temp2>>8;
temp3++;
frame[temp3]=temp2 & 0xFF;
temp3++;
}
crc16 = calculateCRC(&frame[0],responseSize-2);
frame[responseSize-2] = crc16 >> 8; //填寫校驗(yàn)位
frame[responseSize-1] = crc16 & 0xFF;
(*ModbusPort).write(&frame[0],responseSize); //返回功能碼03的消息
}
}
else if(function == 6) //功能碼為06時(shí)進(jìn)入條件
{
unsigned int startData=((frame[2] << 8) | frame[3]); //寫入modbus數(shù)據(jù)庫的地址
unsigned int setData=((frame[4] << 8) | frame[5]); //寫入modbus數(shù)據(jù)庫的數(shù)值
if(startData>=modbusDataSize)
{
errorFlag=0x02; //數(shù)據(jù)超過范圍
responseError(slaveID,function,errorFlag); //返回錯(cuò)誤消息
}
else
{
modbusData[startData]=setData; //寫入數(shù)據(jù)到modbus數(shù)據(jù)庫
frame[0]=slaveID; //設(shè)定站號(hào)
frame[1]=function; //設(shè)定功能碼
frame[2] = startData >> 8; //填寫數(shù)據(jù)庫地址
frame[3] = startData & 0xFF;
frame[4] = modbusData[startData] >> 8; //填寫數(shù)據(jù)庫數(shù)值
frame[5] = modbusData[startData] & 0xFF;
crc16 = calculateCRC(&frame[0],6); //計(jì)算校驗(yàn)值
frame[6] = crc16 >> 8; //填寫校驗(yàn)位
frame[7] = crc16 & 0xFF;
(*ModbusPort).write(&frame[0],8); //返回功能碼06的消息
}
}
else if(function == 16) //功能碼為16時(shí)進(jìn)入條件
{
if(frame[6]!=address-9) //校驗(yàn)數(shù)據(jù)長度
{
errorFlag=0x03; //數(shù)據(jù)長度不符
responseError(slaveID,function,errorFlag); //返回錯(cuò)誤消息
}
else //校驗(yàn)數(shù)據(jù)長度正確
{
unsigned int startData=((frame[2] << 8) | frame[3]); //寫入modbus數(shù)據(jù)庫起始地址
unsigned int dataSize=((frame[4] << 8) | frame[5]); //需要寫入的modbus數(shù)據(jù)庫數(shù)據(jù)長度
unsigned int endData=startData+dataSize; //需要寫入的modbus數(shù)據(jù)庫數(shù)據(jù)的結(jié)束地址
if(dataSize>125 || endData>=modbusDataSize) //讀取數(shù)據(jù)的結(jié)束地址超過了modbus數(shù)據(jù)庫的范圍或單次讀取的數(shù)據(jù)數(shù)量大于125
{
errorFlag=0x02; //數(shù)據(jù)超過范圍
responseError(slaveID,function,errorFlag); //返回錯(cuò)誤消息
}
else
{
unsigned int temp1,temp2;
temp2 = 7; //從數(shù)據(jù)貞的第8個(gè)數(shù)據(jù)開始讀取
for(temp1=startData;temp1
{
modbusData[temp1]=(frame[temp2]<<8|frame[temp2+1]); //將數(shù)據(jù)寫入modbus數(shù)據(jù)庫中
temp2+=2;
}
frame[0]=slaveID; //填寫站號(hào),frame[1]到frame[5]不變
crc16 = calculateCRC(&frame[0],6); //計(jì)算CRC校驗(yàn)
frame[6] = crc16 >> 8; //填寫校驗(yàn)位
frame[7] = crc16 & 0xFF;
(*ModbusPort).write(&frame[0],8); //發(fā)送功能碼16的應(yīng)答數(shù)據(jù)
}
}
}
else //其他功能碼
{
errorFlag = 0x01; //不支持收到的功能碼
responseError(slaveID,function,errorFlag); //返回錯(cuò)誤消息
}
}
else //數(shù)據(jù)校驗(yàn)錯(cuò)誤
{
errorFlag = 0x03;
responseError(slaveID,function,errorFlag); //返回錯(cuò)誤消息
}
}
}
}
}
void responseError(unsigned char ID,unsigned char function,unsigned char wrongNumber) //錯(cuò)誤信息返回函數(shù)
{
unsigned int crc16; //校驗(yàn)位
frame[0] = ID; //設(shè)定站號(hào)
frame[1] = function+0x80;
frame[2] = wrongNumber; //填寫錯(cuò)誤代碼
crc16 = calculateCRC(&frame[0],3); //計(jì)算校驗(yàn)值
frame[3] = crc16 >> 8; //填寫校驗(yàn)位
frame[4] = crc16 & 0xFF;
(*ModbusPort).write(&frame[0],5); //返回錯(cuò)誤代碼
}
//CRC校驗(yàn)函數(shù)
//參數(shù)1:待校驗(yàn)數(shù)組的起始地址
//參數(shù)2:待校驗(yàn)數(shù)組的長度
//返回值CRC校驗(yàn)結(jié)果,16位,低字節(jié)在前
unsigned int calculateCRC(unsigned char* _regs,unsigned char arraySize)
{
unsigned int temp, temp2, flag;
temp = 0xFFFF;
for (unsigned char i = 0; i < arraySize; i++)
{
temp = temp ^ *(_regs+i);
for (unsigned char j = 1; j <= 8; j++)
{
flag = temp & 0x0001;
temp >>= 1;
if (flag)
temp ^= 0xA001;
}
}
temp2 = temp >> 8;
temp = (temp << 8) | temp2;
temp &= 0xFFFF;
return temp;
}