板子是银杏的stm32h750 icore4t,前面移植freemodbus,不是报错误3就是错误4,所以来用Libmodbus,但是还是有问题 因为我没找到RS485的控制引脚,所以就用的232,主程序代码如下: - //#define RS485_RE GET_PIN(G, 8)
- //#define HAVE_DECL_TIOCM_RTS 0
- static void test_thread(void *param)
- {
- //#define RS485_RE GET_PIN(G, 8)
- //#define HAVE_DECL_TIOCM_RTS 0
- static void test_thread(void *param)
- {
- uint16_t tab_reg[64] = {0};
- modbus_t *ctx = RT_NULL;
- ctx = modbus_new_rtu("/dev/uart1", 115200, 'N', 8, 1);
- modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS232);
- // modbus_rtu_set_rts(ctx, RS485_RE, MODBUS_RTU_RTS_UP);
- modbus_set_slave(ctx, 1);
- modbus_connect(ctx);
- modbus_set_response_timeout(ctx, 0, 1000000);
- int num = 0;
- while (1)
- {
- memset(tab_reg, 0, 64 * 2);
- int regs = modbus_read_registers(ctx, 0, 10, tab_reg);
- printf("-------------------------------------------\n");
- printf("[%4d][read num = %d]", num, regs);
- num++;
- int i;
- for (i = 0; i < 10; i++)
- {
- printf("<%#x>", tab_reg[i]);
- }
- printf("\n");
- printf("-------------------------------------------\n");
- rt_thread_mdelay(5000);
- }
- //7-关闭modbus端口
- modbus_close(ctx);
- //8-释放modbus资源
- modbus_free(ctx);
- }
- int rtu_test_init(void)
- {
- // rt_pin_mode(RS485_RE, PIN_MODE_OUTPUT);
- rt_thread_t tid;
- tid = rt_thread_create("test",
- test_thread, RT_NULL,
- 2048,
- 12, 10);
- if (tid != RT_NULL)
- rt_thread_startup(tid);
- return RT_EOK;
- }
- INIT_APP_EXPORT(rtu_test_init);
- }
复制代码串口设置如下: - if(huart->Instance==USART1)
- {
- /* USER CODE BEGIN USART1_MspInit 0 */
- /* USER CODE END USART1_MspInit 0 */
- /** Initializes the peripherals clock
- */
- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1;
- PeriphClkInitStruct.Usart16ClockSelection = RCC_USART16CLKSOURCE_D2PCLK2;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /* Peripheral clock enable */
- __HAL_RCC_USART1_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /**USART1 GPIO Configuration
- PA9 ------> USART1_TX
- PB15 ------> USART1_RX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_9;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /* USER CODE BEGIN USART1_MspInit 1 */
- /* USER CODE END USART1_MspInit 1 */
- }
复制代码现在的情况就是slave模拟软件这边窗口只看到了RX,没有TX,在freemodbus测试的时候,RXTX都是有的,不知道libmodbus为什么没有
测试图如下:
|