Compare commits

..

No commits in common. "7df6f11fb0e07dc5d5101f43f1a0c372147f137c" and "7938f1ecb19ffd90d14f0f5520ac609b8b7481fc" have entirely different histories.

5 changed files with 19 additions and 28 deletions

View File

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

View File

@ -12,14 +12,14 @@ include_directories(tests examples/linux .)
add_library(nanomodbus nanomodbus.c) add_library(nanomodbus nanomodbus.c)
target_include_directories(nanomodbus PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(nanomodbus PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
if (BUILD_EXAMPLES) if(BUILD_EXAMPLES)
add_executable(client-tcp examples/linux/client-tcp.c) add_executable(client-tcp examples/linux/client-tcp.c)
target_link_libraries(client-tcp nanomodbus) target_link_libraries(client-tcp nanomodbus)
add_executable(server-tcp examples/linux/server-tcp.c) add_executable(server-tcp examples/linux/server-tcp.c)
target_link_libraries(server-tcp nanomodbus) target_link_libraries(server-tcp nanomodbus)
endif () endif()
if (BUILD_TESTS) if(BUILD_TESTS)
add_executable(nanomodbus_tests nanomodbus.c tests/nanomodbus_tests.c) add_executable(nanomodbus_tests nanomodbus.c tests/nanomodbus_tests.c)
target_link_libraries(nanomodbus_tests pthread) 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_server_disabled COMMAND $<TARGET_FILE:server_disabled>)
add_test(NAME test_client_disabled COMMAND $<TARGET_FILE:client_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>) 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) FetchContent_MakeAvailable(nanomodbus)
#... ...
add_executable(your_program source_codes) add_executable(your_program source_codes)
target_link_libraries(your_program nanomodbus) target_link_libraries(your_program nanomodbus)
@ -187,5 +187,4 @@ Please refer to `examples/arduino/README.md` for more info about building and ru
- `NMBS_SERVER_READ_WRITE_REGISTERS_DISABLED` - `NMBS_SERVER_READ_WRITE_REGISTERS_DISABLED`
- `NMBS_SERVER_READ_DEVICE_IDENTIFICATION_DISABLED` - `NMBS_SERVER_READ_DEVICE_IDENTIFICATION_DISABLED`
- `NMBS_STRERROR_DISABLED` to disable the code that converts `nmbs_error`s to strings - `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` - 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); uint8_t coils_bytes = get_1(nmbs);
NMBS_DEBUG_PRINT("b %d\t", coils_bytes); NMBS_DEBUG_PRINT("b %d\t", coils_bytes);
if (coils_bytes > NMBS_BITFIELD_BYTES_MAX) { if (coils_bytes > 250) {
return NMBS_ERROR_INVALID_RESPONSE; 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) { static nmbs_error read_discrete(nmbs_t* nmbs, uint8_t fc, uint16_t address, uint16_t quantity, nmbs_bitfield values) {
if (quantity < 1 || quantity > NMBS_BITFIELD_MAX) if (quantity < 1 || quantity > 2000)
return NMBS_ERROR_INVALID_ARGUMENT; return NMBS_ERROR_INVALID_ARGUMENT;
if ((uint32_t) address + (uint32_t) quantity > ((uint32_t) 0xFFFF) + 1) 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; return err;
if (!nmbs->msg.broadcast) if (!nmbs->msg.broadcast)
return recv_write_multiple_registers_res(nmbs, address, quantity); return recv_write_single_register_res(nmbs, address, quantity);
return NMBS_ERROR_NONE; return NMBS_ERROR_NONE;
} }

View File

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