至此,就移植完毕了,测试函数如下:
int ret;
uint8_t ucBuf[10];
uint16_t usBuf[10];
int main_example(void)
{
// 定时器初始化,设置为3.5个字符的超时时间
// Timer_Init();
// 串口初始化,初始化波特率等
// UART_Init();
// 写单个线圈
ret = MBRTUMasterWriteSingleCoil(&MBRTUHandle, 1, 0, 1, 500);
printf(" write single coil %s. \r\n", ret < 0 ? "failed" : "ok");
HAL_Delay(100);
// 写单个寄存器
ret = MBRTUMasterWriteSingleRegister(&MBRTUHandle, 1, 0, 0XAABB, 500);
printf(" write single reg %s. \r\n", ret < 0 ? "failed" : "ok");
HAL_Delay(100);
// 写多个线圈
memset(ucBuf, 0X01, 10);
ret = MBRTUMasterWriteMultipleCoils(&MBRTUHandle, 1, 0, 10, ucBuf, 500);
printf(" write coils %s. \r\n", ret < 0 ? "failed" : "ok");
HAL_Delay(100);
// 写多个寄存器
memset(usBuf, 0XFF, 20);
ret = MBRTUMasterWriteMultipleRegisters(&MBRTUHandle, 1, 0, 10, usBuf, 500);
printf(" write regs %s. \r\n", ret < 0 ? "failed" : "ok");
HAL_Delay(100);
// 读线圈
MBRTUMasterReadCoils(&MBRTUHandle, 1, 0, 10, 500, ucBuf);
printf(" read coils %s. \r\n", ret < 0 ? "failed" : "ok");
HAL_Delay(100);
// 读离散量输入
MBRTUMasterReadDiscreteInputs(&MBRTUHandle, 1, 0, 10, 500, ucBuf);
printf(" read discs %s. \r\n", ret < 0 ? "failed" : "ok");
HAL_Delay(100);
// 读保持寄存器
MBRTUMasterReadHoldingRegisters(&MBRTUHandle, 1, 0, 10, 500, usBuf);
printf(" read hold regs %s. \r\n", ret < 0 ? "failed" : "ok");
HAL_Delay(100);
// 读输入寄存器
MBRTUMasterReadInputRegisters(&MBRTUHandle, 1, 0, 10, 500, usBuf);
printf(" read input regs %s. \r\n", ret < 0 ? "failed" : "ok");
HAL_Delay(100);
return 0;
}
|