Compare commits

...

10 Commits

Author SHA1 Message Date
Valerio De Benedetto
7df6f11fb0 Various fixes 2025-06-26 23:57:23 +02:00
Valerio De Benedetto
979f66fac0 Changes to nmbs_bitfield after merge 2025-06-26 23:37:48 +02:00
Valerio De Benedetto
6605f9162e Merge remote-tracking branch 'ntfreak/configure_coil_max_support' 2025-06-26 23:30:42 +02:00
Valerio De Benedetto
e39085552c
Merge pull request #93 from harbi-eng/small-optmization
optimized  nmbs_bitfield macros a little
2025-06-26 23:24:09 +02:00
arwa
99c59c2266 optmized nmbs_bitfield_write 2025-06-15 00:55:18 -04:00
arwa
2d8fee908c optmized nmbs_bitfield_write 2025-06-15 00:44:46 -04:00
arwa
b5fde1c6b3 optmized the following macros: nmbs_bitfield_read, nmbs_bitfield_set, nmbs_bitfield_unset, nmbs_bitfield_write 2025-06-14 23:24:52 -04:00
Spencer Oliver
d3ac9c1696 coils: ability to configure number of supported coils
No change to current default behaviour of allocating 2000 coils.
However memory usage can be reduced by defining NMBS_MAX_COILS.
2025-05-10 16:54:09 +01:00
Valerio De Benedetto
546040c448 recv_write_single_register_res() instead of recv_write_multiple_registers_res() 2025-04-27 13:05:14 +02:00
Valerio De Benedetto
a980f7f4c4
Update ci.yml 2025-03-07 10:14:11 +01:00
5 changed files with 28 additions and 19 deletions

View File

@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Clone repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: configure
run: |
cmake -S . -B build -DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug
@ -19,7 +19,7 @@ jobs:
- name: Compress Build Directory
run: tar -czf build.tar.gz build/
- name: Upload Build Artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: build
path: build.tar.gz
@ -27,7 +27,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Clone repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Build Arduino examples
run: |
mkdir -p build
@ -60,7 +60,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Download Build Directory
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: build
- name: Extract Build Directory

View File

@ -12,14 +12,14 @@ include_directories(tests examples/linux .)
add_library(nanomodbus nanomodbus.c)
target_include_directories(nanomodbus PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
if(BUILD_EXAMPLES)
if (BUILD_EXAMPLES)
add_executable(client-tcp examples/linux/client-tcp.c)
target_link_libraries(client-tcp nanomodbus)
add_executable(server-tcp examples/linux/server-tcp.c)
target_link_libraries(server-tcp nanomodbus)
endif()
endif ()
if(BUILD_TESTS)
if (BUILD_TESTS)
add_executable(nanomodbus_tests nanomodbus.c tests/nanomodbus_tests.c)
target_link_libraries(nanomodbus_tests pthread)
@ -38,4 +38,4 @@ if(BUILD_TESTS)
add_test(NAME test_server_disabled COMMAND $<TARGET_FILE:server_disabled>)
add_test(NAME test_client_disabled COMMAND $<TARGET_FILE:client_disabled>)
add_test(NAME test_multi_server_rtu COMMAND $<TARGET_FILE:multi_server_rtu>)
endif()
endif ()

View File

@ -114,7 +114,7 @@ FetchContent_Declare(
FetchContent_MakeAvailable(nanomodbus)
...
#...
add_executable(your_program source_codes)
target_link_libraries(your_program nanomodbus)
@ -187,4 +187,5 @@ Please refer to `examples/arduino/README.md` for more info about building and ru
- `NMBS_SERVER_READ_WRITE_REGISTERS_DISABLED`
- `NMBS_SERVER_READ_DEVICE_IDENTIFICATION_DISABLED`
- `NMBS_STRERROR_DISABLED` to disable the code that converts `nmbs_error`s to strings
- `NMBS_BITFIELD_MAX` to set the size of the `nmbs_bitfield` type, used to store coil values (default is `2000`)
- Debug prints about received and sent messages can be enabled by defining `NMBS_DEBUG`

View File

@ -546,7 +546,7 @@ static nmbs_error recv_read_discrete_res(nmbs_t* nmbs, nmbs_bitfield values) {
uint8_t coils_bytes = get_1(nmbs);
NMBS_DEBUG_PRINT("b %d\t", coils_bytes);
if (coils_bytes > 250) {
if (coils_bytes > NMBS_BITFIELD_BYTES_MAX) {
return NMBS_ERROR_INVALID_RESPONSE;
}
@ -1958,7 +1958,7 @@ nmbs_error nmbs_client_create(nmbs_t* nmbs, const nmbs_platform_conf* platform_c
static nmbs_error read_discrete(nmbs_t* nmbs, uint8_t fc, uint16_t address, uint16_t quantity, nmbs_bitfield values) {
if (quantity < 1 || quantity > 2000)
if (quantity < 1 || quantity > NMBS_BITFIELD_MAX)
return NMBS_ERROR_INVALID_ARGUMENT;
if ((uint32_t) address + (uint32_t) quantity > ((uint32_t) 0xFFFF) + 1)
@ -2126,7 +2126,7 @@ nmbs_error nmbs_write_multiple_registers(nmbs_t* nmbs, uint16_t address, uint16_
return err;
if (!nmbs->msg.broadcast)
return recv_write_single_register_res(nmbs, address, quantity);
return recv_write_multiple_registers_res(nmbs, address, quantity);
return NMBS_ERROR_NONE;
}

View File

@ -78,11 +78,21 @@ typedef enum nmbs_error {
*/
#define nmbs_error_is_exception(e) ((e) > 0 && (e) < 5)
#ifndef NMBS_BITFIELD_MAX
#define NMBS_BITFIELD_MAX 2000
#endif
/* check coil count divisible by 8 */
#if ((NMBS_BITFIELD_MAX & 7) > 0)
#error "NMBS_BITFIELD_MAX must be divisible by 8"
#endif
#define NMBS_BITFIELD_BYTES_MAX (NMBS_BITFIELD_MAX / 8)
/**
* Bitfield consisting of 2000 coils/discrete inputs
*/
typedef uint8_t nmbs_bitfield[250];
typedef uint8_t nmbs_bitfield[NMBS_BITFIELD_BYTES_MAX];
/**
* Bitfield consisting of 256 values
@ -92,24 +102,22 @@ typedef uint8_t nmbs_bitfield_256[32];
/**
* Read a bit from the nmbs_bitfield bf at position b
*/
#define nmbs_bitfield_read(bf, b) ((bool) ((bf)[(b) / 8] & (0x1 << ((b) % 8))))
#define nmbs_bitfield_read(bf, b) ((bool) ((bf)[(b) >> 3] & (0x1 << ((b) & (8 - 1)))))
/**
* Set a bit of the nmbs_bitfield bf at position b
*/
#define nmbs_bitfield_set(bf, b) (((bf)[(b) / 8]) = (((bf)[(b) / 8]) | (0x1 << ((b) % 8))))
#define nmbs_bitfield_set(bf, b) (((bf)[(b) >> 3]) = (((bf)[(b) >> 3]) | (0x1 << ((b) & (8 - 1)))))
/**
* Reset a bit of the nmbs_bitfield bf at position b
*/
#define nmbs_bitfield_unset(bf, b) (((bf)[(b) / 8]) = (((bf)[(b) / 8]) & ~(0x1 << ((b) % 8))))
#define nmbs_bitfield_unset(bf, b) (((bf)[(b) >> 3]) = (((bf)[(b) >> 3]) & ~(0x1 << ((b) & (8 - 1)))))
/**
* Write value v to the nmbs_bitfield bf at position b
*/
#define nmbs_bitfield_write(bf, b, v) \
(((bf)[(b) / 8]) = ((v) ? (((bf)[(b) / 8]) | (0x1 << ((b) % 8))) : (((bf)[(b) / 8]) & ~(0x1 << ((b) % 8)))))
#define nmbs_bitfield_write(bf, b, v) ((bf)[(b) >> 3] = ((bf)[(b) >> 3] & ~(1 << ((b) & 7))) | ((v) << ((b) & 7)))
/**
* Reset (zero) the whole bitfield
*/