新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)的MODBUS C語(yǔ)言程序(從機(jī))

51單片機(jī)的MODBUS C語(yǔ)言程序(從機(jī))

作者: 時(shí)間:2016-11-28 來(lái)源:網(wǎng)絡(luò) 收藏

//檢查uart0數(shù)據(jù)
void checkComm0Modbus(void)
{
uint16 crcData;
uint16 tempData;
if(receCount > 4)
{
switch(receBuf[1])
{
case 1://讀取線圈狀態(tài)(讀取點(diǎn) 16位以內(nèi))
case 3://讀取保持寄存器(一個(gè)或多個(gè))
case 5://強(qiáng)制單個(gè)線圈
case 6://設(shè)置單個(gè)寄存器
if(receCount >= 8)
{//接收完成一組數(shù)據(jù)
//應(yīng)該關(guān)閉接收中斷
if(receBuf[0]==localAddr && checkoutError==0)
{
crcData = crc16(receBuf,6);
if(crcData == receBuf[7]+(receBuf[6]<<8))
{//校驗(yàn)正確
if(receBuf[1] == 1)
{//讀取線圈狀態(tài)(讀取點(diǎn) 16位以內(nèi))
readCoil();
}
else if(receBuf[1] == 3)
{//讀取保持寄存器(一個(gè)或多個(gè))
readRegisters();
}
else if(receBuf[1] == 5)
{//強(qiáng)制單個(gè)線圈
forceSingleCoil();
}
else if(receBuf[1] == 6)
{
//presetSingleRegister();
}
}
}
receCount = 0;
checkoutError = 0;
}
break;
case 15://設(shè)置多個(gè)線圈
tempData = receBuf[6];
tempData += 9; //數(shù)據(jù)個(gè)數(shù)
if(receCount >= tempData)
{
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;
}
break;
case 16://設(shè)置多個(gè)寄存器
tempData = (receBuf[4]<<8) + receBuf[5];
tempData = tempData // 2; //數(shù)據(jù)個(gè)數(shù)
tempData += 9;
if(receCount >= tempData)
{
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;
}
break;
default:
break;
}
}
}//void checkComm0(void)
//取線圈狀態(tài) 返回0表示成功
uint16 getCoilVal(uint16 addr,uint16 //tempData)
{
uint16 result = 0;
uint16 tempAddr;
tempAddr = addr & 0xfff;
//只取低8位地址
switch(tempAddr & 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;
}//uint16 getCoilVal(uint16 addr,uint16 //data)
//設(shè)定線圈狀態(tài) 返回0表示成功
uint16 setCoilVal(uint16 addr,uint16 tempData)
{
uint16 result = 0;
uint16 tempAddr;
tempAddr = addr & 0xfff;
switch(tempAddr & 0xff)
{
case 0:
break;
case 1:
testCoil = tempData;
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;
}//uint16 setCoilVal(uint16 addr,uint16 data)
//取寄存器值 返回0表示成功
uint16 getRegisterVal(uint16 addr,uint16 //tempData)
{
uint16 result = 0;
uint16 tempAddr;
tempAddr = addr & 0xfff;
switch(tempAddr & 0xff)
{
case 0:
break;
case 1:
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:
//tempData = testRegister;
break;
default:
break;
}
return result;
}//uint16 getRegisterVal(uint16 addr,uint16 &data)
//設(shè)置寄存器值 返回0表示成功
uint16 setRegisterVal(uint16 addr,uint16 tempData)
{
uint16 result = 0;
uint16 tempAddr;
tempAddr = addr & 0xfff;
switch(tempAddr & 0xff)
{
case 0:
break;
case 1:
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:
testRegister = tempData;
break;
default:
break;
}
return result;
}

上一頁(yè) 1 2 下一頁(yè)

關(guān)鍵詞: 51單片機(jī)MODBUSC語(yǔ)

評(píng)論


技術(shù)專區(qū)

關(guān)閉