crc16(uint8 //puchMsg, usDataLen)
{
uint8 uchCRCHi = 0xFF ; /// 高CRC字节初始化 ///
uint8 uchCRCLo = 0xFF ; /// 低CRC 字节初始化 ///
uint32 uIndex ; /// CRC循环中的索引 ///
while (usDataLen--) /// 传输消息缓冲区 ///
{
uIndex = uchCRCHi ^ //puchMsg++ ; /// 计算CRC ///
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return (uchCRCHi < 8 | uchCRCLo) ;
}//uint16 crc16(uint8 //puchMsg, uint16 usDataLen)
//开始发送
void beginSend(void)
{
b485Send = 1; //设为发送
sendPosi = 0;
if(sendCount > 1)
sendCount--;
ACC = sendBuf[0];
TB8 = P;
SBUF = 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
{//字节位置
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)
//读寄存器
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
{
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
{
sendBuf[i] = receBuf[i];
}
sendCount = receCount;
beginSend();
}//void forceSingleCoil(void)
//设置多个寄存器
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 & 0xff;
//setCount = (receBuf[4]<8) + receBuf[5];
setCount = receBuf[5];
byteCount = receBuf[6];
for(i=0;i
{
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])