Skip to content

Commit af44ba0

Browse files
committed
Fixes to rp2040 example
1 parent 7e4ef3e commit af44ba0

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

examples/rp2040/rtu-client.c

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#include <stdio.h>
2-
#include "pico/stdlib.h"
3-
#include "hardware/uart.h"
41
#include "hardware/gpio.h"
52
#include "hardware/timer.h"
3+
#include "hardware/uart.h"
64
#include "nanomodbus.h"
5+
#include "pico/stdlib.h"
6+
#include <stdio.h>
77

88
// Define UART pins and settings
99
#define UART_ID uart0
@@ -16,59 +16,52 @@
1616
// datasheet for information on which other pins can be used.
1717
#define UART_TX_PIN 0
1818
#define UART_RX_PIN 1
19-
#define UART_DE_PIN 2 // Data Enable pin for RS485
19+
#define UART_DE_PIN 2 // Data Enable pin for RS485
2020

21-
#define PICO_LED_PIN 25 // Onboard LED pin for the Pico
21+
#define PICO_LED_PIN 25 // Onboard LED pin for the Pico
2222

2323
// The server address
2424
#define RTU_SERVER_ADDRESS 1
2525

2626
// Function prototypes
2727
void onError();
28-
int32_t read_serial(uint8_t *buf, uint16_t count, int32_t byte_timeout_ms, void *arg);
29-
int32_t write_serial(const uint8_t *buf, uint16_t count, int32_t byte_timeout_ms, void *arg);
28+
int32_t read_serial(uint8_t* buf, uint16_t count, int32_t byte_timeout_ms, void* arg);
29+
int32_t write_serial(const uint8_t* buf, uint16_t count, int32_t byte_timeout_ms, void* arg);
3030

31-
void onError()
32-
{
31+
void onError() {
3332
// Make the LED blink on error
3433
const uint LED_PIN = PICO_LED_PIN;
3534
gpio_init(LED_PIN);
3635
gpio_set_dir(LED_PIN, GPIO_OUT);
37-
while (true)
38-
{
36+
while (true) {
3937
gpio_put(LED_PIN, 1);
4038
sleep_ms(1000);
4139
gpio_put(LED_PIN, 0);
4240
sleep_ms(1000);
4341
}
4442
}
4543

46-
int32_t read_serial(uint8_t *buf, uint16_t count, int32_t byte_timeout_ms, void *arg)
47-
{
44+
int32_t read_serial(uint8_t* buf, uint16_t count, int32_t byte_timeout_ms, void* arg) {
4845
uint64_t start_time = time_us_64();
4946
int32_t bytes_read = 0;
50-
uint64_t timeout_us = (uint64_t)byte_timeout_ms * 1000;
47+
uint64_t timeout_us = (uint64_t) byte_timeout_ms * 1000;
5148

52-
while (time_us_64() - start_time < timeout_us && bytes_read < count)
53-
{
54-
if (uart_is_readable(UART_ID))
55-
{
49+
while (time_us_64() - start_time < timeout_us && bytes_read < count) {
50+
if (uart_is_readable(UART_ID)) {
5651
buf[bytes_read++] = uart_getc(UART_ID);
57-
start_time = time_us_64(); // Reset start time after a successful read
52+
start_time = time_us_64(); // Reset start time after a successful read
5853
}
5954
}
6055

6156
return bytes_read;
6257
}
6358

64-
int32_t write_serial(const uint8_t *buf, uint16_t count, int32_t byte_timeout_ms, void *arg)
65-
{
59+
int32_t write_serial(const uint8_t* buf, uint16_t count, int32_t byte_timeout_ms, void* arg) {
6660
uart_write_blocking(UART_ID, buf, count);
6761
return count;
6862
}
6963

70-
void pico_setup()
71-
{
64+
void pico_setup() {
7265
printf("Initializing UART...\n");
7366
// Initialize the UART
7467
uart_init(UART_ID, BAUD_RATE);
@@ -86,7 +79,7 @@ void pico_setup()
8679
// Initialize the DE pin as output for RS485
8780
gpio_init(UART_DE_PIN);
8881
gpio_set_dir(UART_DE_PIN, GPIO_OUT);
89-
gpio_put(UART_DE_PIN, 0); // Set DE low to enable receiving initially
82+
gpio_put(UART_DE_PIN, 0); // Set DE low to enable receiving initially
9083

9184
// Initialize the onboard LED
9285
const uint LED_PIN = PICO_LED_PIN;
@@ -95,25 +88,24 @@ void pico_setup()
9588
gpio_put(LED_PIN, 0);
9689
}
9790

98-
int main()
99-
{
91+
int main() {
10092
stdio_init_all();
101-
sleep_ms(5000); // Initial pause to catch prints
93+
sleep_ms(5000); // Initial pause to catch prints
10294

10395
printf("Starting Pico setup...\n");
10496
pico_setup();
10597

10698
printf("Setting up Modbus platform configuration...\n");
10799
nmbs_platform_conf platform_conf;
100+
nmbs_platform_conf_create(&platform_conf);
108101
platform_conf.transport = NMBS_TRANSPORT_RTU;
109102
platform_conf.read = read_serial;
110103
platform_conf.write = write_serial;
111104

112105
printf("Creating Modbus client...\n");
113106
nmbs_t nmbs;
114107
nmbs_error err = nmbs_client_create(&nmbs, &platform_conf);
115-
if (err != NMBS_ERROR_NONE)
116-
{
108+
if (err != NMBS_ERROR_NONE) {
117109
printf("Error creating Modbus client: %d\n", err);
118110
onError();
119111
}
@@ -130,35 +122,31 @@ int main()
130122
nmbs_bitfield_write(coils, 0, 1);
131123
nmbs_bitfield_write(coils, 1, 1);
132124
err = nmbs_write_multiple_coils(&nmbs, 64, 2, coils);
133-
if (err != NMBS_ERROR_NONE)
134-
{
125+
if (err != NMBS_ERROR_NONE) {
135126
printf("Error writing multiple coils: %d\n", err);
136127
onError();
137128
}
138129

139130
printf("Reading 3 coils from address 64...\n");
140-
nmbs_bitfield_reset(coils); // Reset whole bitfield to zero
131+
nmbs_bitfield_reset(coils); // Reset whole bitfield to zero
141132
err = nmbs_read_coils(&nmbs, 64, 3, coils);
142-
if (err != NMBS_ERROR_NONE)
143-
{
133+
if (err != NMBS_ERROR_NONE) {
144134
printf("Error reading coils: %d\n", err);
145135
onError();
146136
}
147137

148138
printf("Writing 2 holding registers at address 26...\n");
149139
uint16_t w_regs[2] = {123, 124};
150140
err = nmbs_write_multiple_registers(&nmbs, 26, 2, w_regs);
151-
if (err != NMBS_ERROR_NONE)
152-
{
141+
if (err != NMBS_ERROR_NONE) {
153142
printf("Error writing multiple registers: %d\n", err);
154143
onError();
155144
}
156145

157146
printf("Reading 2 holding registers from address 26...\n");
158147
uint16_t r_regs[2];
159148
err = nmbs_read_holding_registers(&nmbs, 26, 2, r_regs);
160-
if (err != NMBS_ERROR_NONE)
161-
{
149+
if (err != NMBS_ERROR_NONE) {
162150
printf("Error reading holding registers: %d\n", err);
163151
onError();
164152
}

0 commit comments

Comments
 (0)