Skip to content

Commit 80cdf44

Browse files
committed
Updated for version 1.0
1 parent 2ffbc54 commit 80cdf44

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

README.md

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Future stuff:
2828

2929
# Usage
3030
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
3434

3535
//Describe limitations of each here
3636
| 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
4141
All three of these methods assume you have a UART peripheral that is set up and working.
4242
It also assumes you have a way of getting time in microseconds (at least 32 bits of it).
4343

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+
4476
## Only in Interrupts
4577
When we use it via the interrupts we must have the receive interrupt (RX) directly setup properly on the UART peripheral.
4678
This is not trivial in arduinos, since they Arduino libraries don't expose the RX interrupt directly.
4779

4880
This will call the callback methods directly from the interrupt -- allowing your application to be as responsive as possible.
4981
```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"
6382
#include "bmodbus.h"
6483

84+
85+
TK
86+
87+
6588
//Call from interrupt
6689
void HARDWARE_UART_RX_ISR(void){
6790
//Arguments are the current time in microseconds and the next byte received

bmodbus.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
//
2-
// Created by billm on 3/18/2025.
3-
//
1+
/* MIT Style License
2+
3+
Copyright (c) 2025 Bill McCartney
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8+
persons to whom the Software is furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11+
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
418
#include <stdio.h>
519
#include <stdint.h>
620
#include "bmodbus.h"

bmodbus.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//
2-
// Created by billm on 3/18/2025.
3-
//
4-
51
/**
62
* @file bmodbus.h
73
* @author Bill McCartney
@@ -10,6 +6,22 @@
106
*
117
*/
128

9+
/* MIT Style License
10+
* Copyright (c) 2025 Bill McCartney
11+
12+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
14+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
15+
persons to whom the Software is furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
1325
#ifndef BMODBUS_H
1426
#define BMODBUS_H
1527
#ifdef __cplusplus

0 commit comments

Comments
 (0)