Skip to content

Commit 5be90c4

Browse files
committed
Merge remote-tracking branch 'donghoonpark/feature.custom_flush'
2 parents 11317b1 + 7b2b66b commit 5be90c4

5 files changed

Lines changed: 78 additions & 61 deletions

File tree

.vscode/launch.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
{
22
"version": "0.2.0",
33
"configurations": [
4-
// Use the CMake panel to launch/debug
4+
{
5+
"name": "Debug active CMake target",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"program": "${command:cmake.launchTargetPath}",
9+
"args": [],
10+
"stopAtEntry": false,
11+
"cwd": "${workspaceFolder}",
12+
"environment": [],
13+
"externalConsole": false,
14+
"MIMode": "lldb"
15+
}
516
]
6-
}
17+
}

nanomodbus.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ static nmbs_error send(const nmbs_t* nmbs, uint16_t count) {
183183
}
184184

185185

186-
static void flush(nmbs_t* nmbs) {
187-
nmbs->platform.read(nmbs->msg.buf, sizeof(nmbs->msg.buf), 0, nmbs->platform.arg);
186+
static int32_t flush(nmbs_t* nmbs) {
187+
return nmbs->platform.read(nmbs->msg.buf, sizeof(nmbs->msg.buf), 0, nmbs->platform.arg);
188188
}
189189

190190

@@ -212,7 +212,7 @@ static void msg_state_req(nmbs_t* nmbs, uint8_t fc) {
212212
nmbs->current_tid++;
213213

214214
// Flush the remaining data on the line before sending the request
215-
flush(nmbs);
215+
nmbs->platform.flush(nmbs);
216216

217217
msg_state_reset(nmbs);
218218
nmbs->msg.unit_id = nmbs->dest_address_rtu;
@@ -261,6 +261,7 @@ void nmbs_set_byte_timeout(nmbs_t* nmbs, int32_t timeout_ms) {
261261
void nmbs_platform_conf_create(nmbs_platform_conf* platform_conf) {
262262
memset(platform_conf, 0, sizeof(nmbs_platform_conf));
263263
platform_conf->crc_calc = nmbs_crc_calc;
264+
platform_conf->flush = flush;
264265
// Workaround for older user code not calling nmbs_platform_conf_create()
265266
platform_conf->initialized = 0xFFFFDEBE;
266267
}
@@ -1881,7 +1882,7 @@ static nmbs_error handle_req_fc(nmbs_t* nmbs) {
18811882
break;
18821883
#endif
18831884
default:
1884-
flush(nmbs);
1885+
nmbs->platform.flush(nmbs);
18851886
if (!nmbs->msg.ignored)
18861887
err = send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_FUNCTION);
18871888
}
@@ -1941,7 +1942,7 @@ nmbs_error nmbs_server_poll(nmbs_t* nmbs) {
19411942
err = handle_req_fc(nmbs);
19421943
if (err != NMBS_ERROR_NONE) {
19431944
if (err != NMBS_ERROR_TIMEOUT)
1944-
flush(nmbs);
1945+
nmbs->platform.flush(nmbs);
19451946

19461947
return err;
19471948
}

nanomodbus.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
extern "C" {
4949
#endif
5050

51+
typedef struct nmbs_t nmbs_t;
52+
5153
/**
5254
* nanoMODBUS errors.
5355
* Values <= 0 are library errors, > 0 are modbus exceptions.
@@ -117,7 +119,7 @@ typedef uint8_t nmbs_bitfield_256[32];
117119
/**
118120
* Write value v to the nmbs_bitfield bf at position b
119121
*/
120-
#define nmbs_bitfield_write(bf, b, v) ((bf)[(b) >> 3] = ((bf)[(b) >> 3] & ~(1 << ((b) & 7))) | ((v) << ((b) & 7)))
122+
#define nmbs_bitfield_write(bf, b, v) ((bf)[(b) >> 3] = ((bf)[(b) >> 3] & ~(1 << ((b) &7))) | ((v) << ((b) &7)))
121123
/**
122124
* Reset (zero) the whole bitfield
123125
*/
@@ -163,7 +165,8 @@ typedef struct nmbs_platform_conf {
163165
void* arg); /*!< Bytes write transport function pointer */
164166
uint16_t (*crc_calc)(const uint8_t* data, uint32_t length,
165167
void* arg); /*!< CRC calculation function pointer. Optional */
166-
void* arg; /*!< User data, will be passed to functions above */
168+
int32_t (*flush)(nmbs_t* nmbs);
169+
void* arg; /*!< User data, will be passed to functions above */
167170
uint32_t initialized; /*!< Reserved, workaround for older user code not calling nmbs_platform_conf_create() */
168171
} nmbs_platform_conf;
169172

@@ -241,7 +244,7 @@ typedef struct nmbs_callbacks {
241244
* nanoMODBUS client/server instance type. All struct members are to be considered private,
242245
* it is not advisable to read/write them directly.
243246
*/
244-
typedef struct nmbs_t {
247+
struct nmbs_t {
245248
struct {
246249
uint8_t buf[260];
247250
uint16_t buf_idx;
@@ -264,7 +267,7 @@ typedef struct nmbs_t {
264267
uint8_t address_rtu;
265268
uint8_t dest_address_rtu;
266269
uint16_t current_tid;
267-
} nmbs_t;
270+
};
268271

269272
/**
270273
* Modbus broadcast address. Can be passed to nmbs_set_destination_rtu_address().

0 commit comments

Comments
 (0)