📄 c51_modbus.c
字号:
i=CommBuf[2]+3;
for(CommIndex=3;CommIndex<i;CommIndex++) // send data Reg to Master
CommBuf[CommIndex]=SlaveOutputBuf[CommIndex-3]; // hight 8 bit is first send
crc16tem=crc16(CommBuf,CommIndex); // then send low 8 bit data
CommBuf[CommIndex++]=(uchar)(crc16tem & 0x00ff); // send crccheck low 8 bit is front
CommBuf[CommIndex]=(uchar)(crc16tem>>8); // then send hight 8 bit
CommIndexEnd=CommIndex;
CommIndex=0;
SBUF=CommBuf[CommIndex++];
break;
case ForceSingleCoil://强制单线圈
// SlaveInputBuf[0]=CommBuf[4]; // get data 0x00 or 0xff
if (CommBuf[4]==0xff)//????
LED0=0;
else
LED0=1;
// LED0=!LED0;
ForceCoilBuf[count]=CommBuf[4];
forceval=CommBuf[4];
count++;
CommBuf[CommIndexSlaveID]=SlaveID; // 0
CommBuf[CommIndexFunction]=ForceSingleCoil; // 1
CommIndex=2;
// CommBuf[CommIndex++]=0x00;
// CommBuf[CommIndex++]=0x01;
// CommBuf[CommIndex++]=0x00;
// CommBuf[CommIndex++]=0xff;
// forcesendF=1;
CommBuf[CommIndex++]=CommBuf[CommIndexStartAdrHi]; // 2
CommBuf[CommIndex++]=CommBuf[CommIndexStartAdrLo]; // 3
CommBuf[CommIndex++]=CommBuf[CommIndexNoPointHi]; // 4
CommBuf[CommIndex++]=CommBuf[CommIndexNoPointLo]; // 5
crc16tem=crc16(CommBuf,CommIndex); // then send low 8 bit data
CommBuf[CommIndex++]=(uchar)(crc16tem & 0x00ff); // send crccheck low 8 bit is front
CommBuf[CommIndex]=(uchar)(crc16tem>>8); // then send hight 8 bit
CommIndexEnd=CommIndex;
CommIndex=0;
SBUF=CommBuf[CommIndex++];
break;
case ReadHoldReg:
CommBuf[CommIndexSlaveID]=SlaveID; // 0
CommBuf[CommIndexFunction]=ReadHoldReg; // 1
CommBuf[2]=CommBuf[CommIndexNoPointLo]*2; // 2 Byte Count
for (i=0;i<MaxRegLen;i++) // sim inputval for test
SlaveOutputBuf[i]=SlaveAdr;
i=CommBuf[2]+3;
for(CommIndex=3;CommIndex<i;CommIndex++) // send data Reg to Master
CommBuf[CommIndex]=SlaveOutputBuf[CommIndex-3]; // hight 8 bit is first send
crc16tem=crc16(CommBuf,CommIndex); // then send low 8 bit data
CommBuf[CommIndex++]=(uchar)(crc16tem & 0x00ff); // send crccheck low 8 bit is front
CommBuf[CommIndex]=(uchar)(crc16tem>>8); // then send hight 8 bit
CommIndexEnd=CommIndex;
CommIndex=0;
SBUF=CommBuf[CommIndex++];
break;
case PresetMulReg:
j=CommBuf[CommIndexNoPointLo]*2;
for (i=0;i<j;i++)
SlaveOutputBuf[0]=CommBuf[i+7]; // get data that master send start 7th byte ????
CommBuf[CommIndexSlaveID]=SlaveID; // 0
CommBuf[CommIndexFunction]=PresetMulReg; // 1
CommIndex=2;
CommBuf[CommIndex++]=CommBuf[CommIndexStartAdrHi]; // 2
CommBuf[CommIndex++]=CommBuf[CommIndexStartAdrLo]; // 3
CommBuf[CommIndex++]=CommBuf[CommIndexNoPointHi]; // 4
CommBuf[CommIndex++]=CommBuf[CommIndexNoPointLo]; // 5
crc16tem=crc16(CommBuf,CommIndex); // then send low 8 bit data
CommBuf[CommIndex++]=(uchar)(crc16tem & 0x00ff); // send crccheck low 8 bit is front
CommBuf[CommIndex]=(uchar)(crc16tem>>8); // then send hight 8 bit
CommIndexEnd=CommIndex;
CommIndex=0;
SBUF=CommBuf[CommIndex++];
break;
default:
for (i=0;i<=80;i++) // delay // error recieve again
_nop_();
CommIndex=0;
T0=RECIEVE;
break;
}
}
else
{
for (i=0;i<=80;i++) // delay
_nop_();
CommIndex=0;
T0=RECIEVE;
}
}
}
/*------------- intr code is following ----------------*/
void slavecomm(void) interrupt 4 using 2
{
uint i;
ES=0;
if(TI)//发送中断标志
{
TI=0;
if (CommIndex<=CommIndexEnd)
{
SBUF=CommBuf[CommIndex++];
}
else
{
// if (forcesendF)
// _nop_();
for (i=0;i<=SendRecieveDelay;i++) // delay
_nop_();
CommIndex=0;
T0=RECIEVE;
}
}
if(RI)
{
RI=0;
if (CommIndex<CommIndexEnd) // CommIndex less than recieve lenght
{
CommBuf[CommIndex]=SBUF;
if (CommIndex==CommIndexFunction)
{
switch (CommBuf[CommIndexFunction])
{
case ReadCoilSta:
CommIndexEnd=ReadCoilStaIndexEnd;
break;
case ForceSingleCoil:
CommIndexEnd=ForceSingleCoilIndexEnd;
break;
case ReadHoldReg:
CommIndexEnd=ReadHoldRegIndexEnd;
break;
case PresetMulReg:
CommIndexEnd=6+CommBuf[CommIndexNoPointLo]*2+2;
break;
default:
break;
}
}
CommIndex++;
}
else // recieve finished CommIndex==CommIndexEnd
{
CommBuf[CommIndexEnd]=SBUF;
RecFinishF=1;
for (i=0;i<=SendRecieveDelay;i++) // delay
_nop_();
T0=SEND;
}
}
ES=1;//串口中断允许
}
/*------------------ main code following -----------------*/
void main(void)
{
uint data i;
init();
while(1)
{
// LED0=!LED0;
for(i=0;i<=9;i++)
_nop_();
if (RecFinishF)
{
AnalyzeRecieve();
RecFinishF=0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -