Skip to content

Commit

Permalink
add client example
Browse files Browse the repository at this point in the history
  • Loading branch information
DongHoonPark committed Oct 5, 2024
1 parent ae3c174 commit 2ea16cb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
23 changes: 22 additions & 1 deletion examples/stm32/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

#define TEST_SERVER 1
#define TEST_CLIENT 0
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
Expand Down Expand Up @@ -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 */

Expand All @@ -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 */
}
Expand Down
25 changes: 25 additions & 0 deletions examples/stm32/nanomodbus_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions examples/stm32/nanomodbus_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 2ea16cb

Please sign in to comment.