@@ -28,9 +28,9 @@ Future stuff:
28
28
29
29
# Usage
30
30
There are three main ways to use this library, and they all share some common elements.
31
- 1 . Runs in interrupts (requires callbacks and serial writing routine).
32
- 2 . Runs only in the main loop.
33
- 3 . In both interrupts and the main loop.
31
+ 1 . Runs in interrupts (requires callbacks and serial writing routine). -- Not tested
32
+ 2 . Runs only in the main loop. Currently tested.
33
+ 3 . In both interrupts and the main loop. -- Not tested
34
34
35
35
//Describe limitations of each here
36
36
| Feature | Only in interrupts | Only in main loop | In both |
@@ -41,27 +41,50 @@ There are three main ways to use this library, and they all share some common el
41
41
All three of these methods assume you have a UART peripheral that is set up and working.
42
42
It also assumes you have a way of getting time in microseconds (at least 32 bits of it).
43
43
44
+ ## Only in Main Loop
45
+ This is the simplest implementation and is easy to do in an arduino or any other environment.
46
+ ``` c
47
+ #include " bmodbus.h"
48
+
49
+ bmodbus_client_t mb;
50
+ void setup (){
51
+ Serial.begin(38400);
52
+ //Setup a modbus instance for client/slave address 2
53
+ bmodbus_client_init (&mb, INTERFRAME_DELAY_MICROSECONDS(38400), 2);
54
+ }
55
+
56
+ void loop (){
57
+ if(Serial.available(){
58
+ uint8_t next = Serial.read();
59
+ bmodbus_client_next_byte (&mb, micros(), Serial.read());
60
+ //If there's a message, handle it
61
+ bmodbus_client_request_t * request = bmodbus_client_get_request(&mb);
62
+ if(request != NULL){
63
+ //HANDLE the requests by updating the * request
64
+ ...
65
+ //End of handlers
66
+
67
+ //Now grab the response in a format the serial port understands
68
+ modbus_uart_response_t * response = bmodbus_client_get_response(&mb);
69
+ Serial.write(response->data, response->length);
70
+
71
+ }
72
+ }
73
+ }
74
+ ```
75
+
44
76
## Only in Interrupts
45
77
When we use it via the interrupts we must have the receive interrupt (RX) directly setup properly on the UART peripheral.
46
78
This is not trivial in arduinos, since they Arduino libraries don't expose the RX interrupt directly.
47
79
48
80
This will call the callback methods directly from the interrupt -- allowing your application to be as responsive as possible.
49
81
``` c
50
- // bmb_config.h
51
- #define BMB1_CLIENT
52
- #define BMB1_USE_INTERRUPTS
53
- #define BMB1_USE_REGISTERS
54
- // #define BMB1_UART_INTERRUPT_HANDLER HAL_UART_IRQHandler
55
- #define BMB1_REGISTERS_WRITE modbus_registers_write
56
- #define BMB1_REGISTERS_READ modbus_regsiters_read
57
- #define BMB1_SERIAL_WRITE uart_transmit
58
- #define BMB1_BAUD 19200
59
- ```
60
-
61
- ``` c
62
- #include " bmb_config.h"
63
82
#include " bmodbus.h"
64
83
84
+
85
+ TK
86
+
87
+
65
88
// Call from interrupt
66
89
void HARDWARE_UART_RX_ISR (void){
67
90
//Arguments are the current time in microseconds and the next byte received
0 commit comments