简介:本项目专注于基于51单片机实现Modbus RTU通信协议的应用开发,旨在通过简洁高效的代码实现工业设备间的数据交换与控制。
```c
void presetSingleRegister(void) // 设置单个寄存器{
U8 addr;
U8 tempAddr;
U8 setCount;
U16 crcData;
U16 tempData;
addr = receBuf[3];
tempAddr = addr & 0xff;
tempData = (receBuf[4]<<8) + receBuf[5];
setRegisterVal(tempAddr, tempData);
sendBuf[0] = localAddr;
sendBuf[1] = 6;
sendBuf[2] = addr >> 8;
sendBuf[3] = addr & 0xff;
sendBuf[4] = receBuf[4];
sendBuf[5] = receBuf[5];
setCount = 6; // 共计6个字节
crcData = crc16(sendBuf, 6);
sendBuf[6] = crcData >> 8;
sendBuf[7] = crcData & 0xff;
sendCount = 8;
beginSend();
}
// 设置多个寄存器
void presetMultipleRegisters(void) {
UINT8 addr;
UINT8 tempAddr;
UINT8 byteCount;
UINT16 crcData;
UINT16 tempData;
UINT8 i;
addr = receBuf[3];
tempAddr = addr & 0xff;
setCount = receBuf[5];
byteCount = receBuf[6];
for (i=0; i> 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();
}
```