#include "main.h"
//字地址 0 - 255
//位地址 0 - 255
uint8 testCoil=0x4444; //用于测试 位地址1
uint16 testRegister=0x5233; //用于测试 字地址0
uint8 localAddr = 1; //地址
uint8 sendCount; //发送字节个数
uint8 receCount; //接收字节个数
uint8 sendPosi; //发送位置
uint8 sendBuf[32],receBuf[32]; //发送,接收缓冲区
uint8 checkoutError; //校验结果
uint8 receTimeOut; //接收超时
uint16 crc16(uint8 *data,uint8 length)
{
uint8 i;
uint16 crc_result=0xffff;
while(length--)
{
crc_result^=*data++;
for(i=0; i<8; i++)
{
if(crc_result&0x01)
{crc_result=(crc_result>>1) ^0xa001; }
else
{crc_result=crc_result>>1; }
}
}
return (crc_result=((crc_result&0xff) <<8) |(crc_result>>8) );
}
//开始发送
void beginSend(void)
{
sendEnable(); //设为发送
sendPosi = 0;
if(sendCount > 1)
{
sendCount--;
UDR = sendBuf[0];
}
}//void beginSend(void)
//读线圈状态
void readCoil(void)
{
uint8 addr;
uint8 tempAddr;
uint8 byteCount;
uint8 bitCount;
uint16 crcData;
uint8 position;
uint8 i,k;
uint8 result;
uint16 tempData;
uint8 exit = 0;
//addr = (receBuf[2]<<8) + receBuf[3];
//tempAddr = addr & 0xfff;
addr = receBuf[3];
tempAddr = addr;
//bitCount = (receBuf[4]<<8) + receBuf[5]; //读取的位个数
bitCount = receBuf[5];
byteCount = bitCount / 8; //字节个数
if(bitCount%8 != 0)
byteCount++;
for(k=0;k<byteCount;k++)
{//字节位置
position = k + 3;
sendBuf[position] = 0;
for(i=0;i<8;i++)
{
getCoilVal(tempAddr,&tempData);
sendBuf[position] |= tempData << i;
tempAddr++;
if(tempAddr >= addr+bitCount)
{ //读完
exit = 1;
break;
}
}
if(exit == 1)
break;
}
sendBuf[0] = localAddr;
sendBuf[1] = 0x01;
sendBuf[2] = byteCount;
byteCount += 3;
crcData = crc16(sendBuf,byteCount);
sendBuf[byteCount] = crcData >> 8;
byteCount++;
sendBuf[byteCount] = crcData & 0xff;
sendCount = byteCount + 1;
beginSend();
}//void readCoil(void)
//读多个寄存器0X3功能
void readRegisters(void)
{
uint8 addr;
uint8 tempAddr;
uint16 result;
uint16 crcData;
uint8 readCount;
uint8 byteCount;
uint8 finsh; //1完成 0出错
uint16 i;
uint16 tempData = 0;
//addr = (receBuf[2]<<8) + receBuf[3];
//tempAddr = addr & 0xfff;
addr = receBuf[3];
tempAddr = addr;
//readCount = (receBuf[4]<<8) + receBuf[5]; //要读的个数
readCount = receBuf[5];
byteCount = readCount * 2;
for(i=0;i<byteCount;i+=2,tempAddr++)
{
getRegisterVal(tempAddr,&tempData);
sendBuf[i+3] = tempData >> 8;
sendBuf[i+4] = tempData & 0xff;
}
sendBuf[0] = localAddr;
sendBuf[1] = 3;
sendBuf[2] = byteCount;
byteCount += 3;
crcData = crc16(sendBuf,byteCount);
sendBuf[byteCount] = crcData >> 8;
byteCount++;
sendBuf[byteCount] = crcData & 0xff;
sendCount = byteCount + 1;
beginSend();
}//void readRegisters(void)
//强制单个线圈
void forceSingleCoil(void)
{
uint8 addr;
uint8 tempAddr;
uint16 tempData;
uint8 onOff;
uint8 i;
//addr = (receBuf[2]<<8) + receBuf[3];
//tempAddr = addr & 0xfff;
addr = receBuf[3];
tempAddr = addr;
//onOff = (receBuf[4]<<8) + receBuf[5];
onOff = receBuf[4];
//if(onOff == 0xff00)
if(onOff == 0xff)
{ //设为ON
tempData = 1;
}
//else if(onOff == 0x0000)
else if(onOff == 0x00)
{ //设为OFF
tempData = 0;
}
setCoilVal(tempAddr,tempData);
for(i=0;i<receCount;i++)
{
sendBuf[i] = receBuf[i];
}
sendCount = receCount;
beginSend();
}//void forceSingleCoil(void)
//设置单个寄存器0x06功能
void presetSingleRegister()
{
uint8 addr;
uint8 tempAddr;
uint8 byteCount;
uint8 setCount;
uint16 crcData;
uint16 tempData;
uint8 finsh; //为1时完成 为0时出错
uint8 i;
addr = (receBuf[2]<<8) + receBuf[3];
tempAddr = addr;
tempData=(receBuf[4]<<8) + receBuf[5];
setRegisterVal(tempAddr,tempData);
for(i=0;i<receCount;i++)
{
sendBuf[i] = receBuf[i];
}
sendCount = receCount;
beginSend();
}
//设置多个寄存器0x10
void presetMultipleRegisters(void)
{
uint8 addr;
uint8 tempAddr;
uint8 byteCount;
uint8 setCount;
uint16 crcData;
uint16 tempData;
uint8 finsh; //为1时完成 为0时出错
uint8 i;
addr = (receBuf[2]<<8) + receBuf[3];
//tempAddr = addr & 0xfff;
//addr = receBuf[3];
tempAddr = addr;
setCount = (receBuf[4]<<8) + receBuf[5];
//setCount = receBuf[5];
byteCount = receBuf[6];
for(i=0;i<setCount;i++,tempAddr++)
{
tempData = (receBuf[i*2+7]<<8) + receBuf[i*2+8];
setRegisterVal(tempAddr,tempData);
}
sendBuf[0] = localAddr;
sendBuf[1] = 16;
sendBuf[2] = addr >> 8;
sendBuf[3] = addr & 0xff;
sendBuf[4] = setCount >> 8;
sendBuf[5] = setCount & 0xff;
crcData = crc16(sendBuf,6);
sendBuf[6] = crcData >> 8;
sendBuf[7] = crcData & 0xff;
sendCount = 8;
beginSend();
}//void presetMultipleRegisters(void)
//检查uart0数据
void checkComm0Modbus(void)
{
uint16 crcData;
uint16 tempData;
if(receCount > 4)
{
switch(receBuf[1])
{
case 1://读取线圈状态(读取点 16位以内)
case 3://读取保持寄存器(一个或多个)
case 5://强制单个线圈
case 6://设置单个寄存器
if(receCount >= 8)//接收完成一组数据
{
UCSRB &= ~BIT(7); //应该关闭接收中断
if(receBuf[0]==localAddr && checkoutError==0)
{
crcData = crc16(receBuf,6);
if(crcData == receBuf[7]+(receBuf[6]<<8))
{//校验正确
if(receBuf[1] == 1)
{//读取线圈状态(读取点 16位以内)
readCoil();
}
else if(receBuf[1] == 3)
{//读取保持寄存器(一个或多个)
readRegisters();
}
else if(receBuf[1] == 5)
{//强制单个线圈
forceSingleCoil();
}
else if(receBuf[1] == 6)
{
presetSingleRegister();
}
}
}
receCount = 0;
checkoutError = 0;
UCSRB |= BIT(7);
}
break;
case 15://设置多个线圈
tempData = receBuf[6];
tempData += 9; //数据个数
if(receCount >= tempData)
{//应该关闭接收中断
UCSRB &= ~BIT(7);
if(receBuf[0]==localAddr && checkoutError==0)
{
crcData = crc16(receBuf,tempData-2);
if(crcData == (receBuf[tempData-2]<<8)+ receBuf[tempData-1])
{
//forceMultipleCoils();
}
}
receCount = 0;
checkoutError = 0;
UCSRB |= BIT(7);
}
break;
case 16://设置多个寄存器
tempData = (receBuf[4]<<8) + receBuf[5];
tempData = tempData * 2; //数据个数
tempData += 9;
if(receCount >= tempData)
{//应该关闭接收中断
UCSRB &= ~BIT(7);
if(receBuf[0]==localAddr && checkoutError==0)
{
crcData = crc16(receBuf,tempData-2);
if(crcData == (receBuf[tempData-2]<<8)+ receBuf[tempData-1])
{
presetMultipleRegisters();
}
}
receCount = 0;
checkoutError = 0;
UCSRB |= BIT(7);
}
break;
default:
break;
}
}
}//void checkComm0(void)
//取线圈状态 返回0表示成功
uint16 getCoilVal(uint16 addr,uint16 *tempData)
{
uint16 result = 0;
switch(addr & 0xff)
{
case 0:
break;
case 1:
*tempData = testCoil;
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
break;
case 13:
break;
case 14:
break;
case 15:
break;
case 16:
break;
default:
break;
}
return result;
}//uint1