add client example

This commit is contained in:
Donghoon Park 2024-10-05 16:49:47 +09:00 committed by Valerio De Benedetto
parent dc95d04fc9
commit c94da49b12
3 changed files with 48 additions and 1 deletions

View File

@ -31,7 +31,8 @@
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define TEST_SERVER 1
#define TEST_CLIENT 0
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
@ -101,7 +102,15 @@ int main(void)
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
#if TEST_SERVER
nmbs_server_init(&nmbs, &nmbs_server);
#endif
#if TEST_CLIENT
uint8_t coils_test[32];
uint16_t regs_test[32];
nmbs_client_init(&nmbs);
#endif
/* USER CODE END 2 */
@ -114,7 +123,19 @@ int main(void)
/* USER CODE BEGIN 3 */
// If you use rtos, this polling can processed in a task
#if TEST_SERVER
nmbs_server_poll(&nmbs);
#endif
#if TEST_CLIENT
nmbs_set_destination_rtu_address(&nmbs, 0x01);
nmbs_error status = nmbs_read_holding_registers(&nmbs, 0, 32, regs_test);
status = nmbs_write_multiple_registers(&nmbs, 0, 32, regs_test);
if(status != NMBS_ERROR_NONE)
{
while(true){}
}
#endif
}
/* USER CODE END 3 */
}

View File

@ -66,6 +66,31 @@ nmbs_error nmbs_server_init(nmbs_t* nmbs, nmbs_server_t* _server)
return NMBS_ERROR_NONE;
}
nmbs_error nmbs_client_init(nmbs_t* nmbs)
{
ringbuf_init(&rb, ringbuf_overflow_error);
nmbs_platform_conf conf;
nmbs_platform_conf_create(&conf);
conf.transport = NMBS_TRANSPORT_RTU;
conf.read = read_serial;
conf.write = write_serial;
nmbs_error status = nmbs_client_create(nmbs, &conf);
if(status != NMBS_ERROR_NONE)
{
return status;
}
nmbs_set_byte_timeout(nmbs, 100);
nmbs_set_read_timeout(nmbs, 1000);
HAL_UARTEx_ReceiveToIdle_DMA(&NANOMB_UART, rx_dma_buf, RX_BUF_SIZE);
return NMBS_ERROR_NONE;
}
static nmbs_server_t* get_server(uint8_t id)
{
if(id == server->id)

View File

@ -26,6 +26,7 @@ typedef struct tNmbsServer{
}nmbs_server_t;
nmbs_error nmbs_server_init(nmbs_t* nmbs, nmbs_server_t* server);
nmbs_error nmbs_client_init(nmbs_t* nmbs);
extern UART_HandleTypeDef NANOMB_UART;