Switch to multibyte transport read/write funcs, removed sleep
This commit is contained in:
parent
afcdae0ec8
commit
b6fee48160
55
nanomodbus.c
55
nanomodbus.c
@ -113,7 +113,6 @@ int nmbs_create(nmbs_t* nmbs, const nmbs_platform_conf* platform_conf) {
|
||||
|
||||
nmbs->byte_timeout_ms = -1;
|
||||
nmbs->read_timeout_ms = -1;
|
||||
nmbs->byte_spacing_ms = 0;
|
||||
|
||||
if (!platform_conf)
|
||||
return NMBS_ERROR_INVALID_ARGUMENT;
|
||||
@ -121,7 +120,7 @@ int nmbs_create(nmbs_t* nmbs, const nmbs_platform_conf* platform_conf) {
|
||||
if (platform_conf->transport != NMBS_TRANSPORT_RTU && platform_conf->transport != NMBS_TRANSPORT_TCP)
|
||||
return NMBS_ERROR_INVALID_ARGUMENT;
|
||||
|
||||
if (!platform_conf->read_byte || !platform_conf->write_byte || !platform_conf->sleep)
|
||||
if (!platform_conf->read || !platform_conf->write)
|
||||
return NMBS_ERROR_INVALID_ARGUMENT;
|
||||
|
||||
nmbs->platform = *platform_conf;
|
||||
@ -164,11 +163,6 @@ void nmbs_set_byte_timeout(nmbs_t* nmbs, int32_t timeout_ms) {
|
||||
}
|
||||
|
||||
|
||||
void nmbs_set_byte_spacing(nmbs_t* nmbs, uint32_t spacing_ms) {
|
||||
nmbs->byte_spacing_ms = spacing_ms;
|
||||
}
|
||||
|
||||
|
||||
void nmbs_set_destination_rtu_address(nmbs_t* nmbs, uint8_t address) {
|
||||
nmbs->dest_address_rtu = address;
|
||||
}
|
||||
@ -198,43 +192,36 @@ static uint16_t crc_calc(const uint8_t* data, uint32_t length) {
|
||||
|
||||
|
||||
static nmbs_error recv(nmbs_t* nmbs, uint32_t count) {
|
||||
uint32_t r = 0;
|
||||
while (r != count) {
|
||||
int ret = nmbs->platform.read_byte(nmbs->msg.buf + nmbs->msg.buf_idx + r, nmbs->byte_timeout_ms,
|
||||
nmbs->platform.arg);
|
||||
if (ret == 0) {
|
||||
return NMBS_ERROR_TIMEOUT;
|
||||
}
|
||||
else if (ret != 1) {
|
||||
return NMBS_ERROR_TRANSPORT;
|
||||
}
|
||||
int ret = nmbs->platform.read(nmbs->msg.buf + nmbs->msg.buf_idx, count, nmbs->byte_timeout_ms, nmbs->platform.arg);
|
||||
|
||||
r++;
|
||||
if (ret == (int) count)
|
||||
return NMBS_ERROR_NONE;
|
||||
|
||||
if (ret < (int) count) {
|
||||
if (ret < 0)
|
||||
return NMBS_ERROR_TRANSPORT;
|
||||
|
||||
return NMBS_ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
return NMBS_ERROR_NONE;
|
||||
return NMBS_ERROR_TRANSPORT;
|
||||
}
|
||||
|
||||
|
||||
static nmbs_error send(nmbs_t* nmbs) {
|
||||
uint32_t spacing_ms = 0;
|
||||
if (nmbs->platform.transport == NMBS_TRANSPORT_RTU)
|
||||
spacing_ms = nmbs->byte_spacing_ms;
|
||||
static nmbs_error send(nmbs_t* nmbs, uint32_t count) {
|
||||
int ret = nmbs->platform.write(nmbs->msg.buf, count, nmbs->byte_timeout_ms, nmbs->platform.arg);
|
||||
|
||||
for (int i = 0; i < nmbs->msg.buf_idx; i++) {
|
||||
if (spacing_ms != 0)
|
||||
nmbs->platform.sleep(spacing_ms, nmbs->platform.arg);
|
||||
if (ret == (int) count)
|
||||
return NMBS_ERROR_NONE;
|
||||
|
||||
int ret = nmbs->platform.write_byte(nmbs->msg.buf[i], nmbs->read_timeout_ms, nmbs->platform.arg);
|
||||
if (ret == 0) {
|
||||
return NMBS_ERROR_TIMEOUT;
|
||||
}
|
||||
else if (ret != 1) {
|
||||
if (ret < (int) count) {
|
||||
if (ret < 0)
|
||||
return NMBS_ERROR_TRANSPORT;
|
||||
}
|
||||
|
||||
return NMBS_ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
return NMBS_ERROR_NONE;
|
||||
return NMBS_ERROR_TRANSPORT;
|
||||
}
|
||||
|
||||
|
||||
@ -412,7 +399,7 @@ static nmbs_error send_msg_footer(nmbs_t* nmbs) {
|
||||
put_2(nmbs, crc);
|
||||
}
|
||||
|
||||
nmbs_error err = send(nmbs);
|
||||
nmbs_error err = send(nmbs, nmbs->msg.buf_idx);
|
||||
|
||||
DEBUG("\n");
|
||||
|
||||
|
||||
20
nanomodbus.h
20
nanomodbus.h
@ -130,11 +130,12 @@ typedef enum nmbs_transport {
|
||||
* After the creation of an instance it can be changed with nmbs_set_platform_arg().
|
||||
*/
|
||||
typedef struct nmbs_platform_conf {
|
||||
nmbs_transport transport; /*!< Transport type */
|
||||
int (*read_byte)(uint8_t* b, int32_t timeout_ms, void* arg); /*!< Byte read transport function pointer */
|
||||
int (*write_byte)(uint8_t b, int32_t timeout_ms, void* arg); /*!< Byte write transport function pointer */
|
||||
void (*sleep)(uint32_t milliseconds, void* arg); /*!< Sleep function pointer */
|
||||
void* arg; /*!< User data, will be passed to functions above */
|
||||
nmbs_transport transport; /*!< Transport type */
|
||||
int32_t (*read)(uint8_t* buf, uint32_t count, int32_t byte_timeout_ms,
|
||||
void* arg); /*!< Bytes read transport function pointer */
|
||||
int32_t (*write)(const uint8_t* buf, uint32_t count, int32_t byte_timeout_ms,
|
||||
void* arg); /*!< Bytes write transport function pointer */
|
||||
void* arg; /*!< User data, will be passed to functions above */
|
||||
} nmbs_platform_conf;
|
||||
|
||||
|
||||
@ -172,7 +173,6 @@ typedef struct nmbs_t {
|
||||
|
||||
int32_t byte_timeout_ms;
|
||||
int32_t read_timeout_ms;
|
||||
uint32_t byte_spacing_ms;
|
||||
|
||||
nmbs_platform_conf platform;
|
||||
|
||||
@ -217,18 +217,12 @@ nmbs_error nmbs_server_create(nmbs_t* nmbs, uint8_t address_rtu, const nmbs_plat
|
||||
*/
|
||||
void nmbs_set_read_timeout(nmbs_t* nmbs, int32_t timeout_ms);
|
||||
|
||||
/** Set the timeout between the reception of two consecutive bytes.
|
||||
/** Set the timeout between the reception/transmission of two consecutive bytes.
|
||||
* @param nmbs pointer to the nmbs_t instance
|
||||
* @param timeout_ms timeout in milliseconds. If < 0, the timeout is disabled.
|
||||
*/
|
||||
void nmbs_set_byte_timeout(nmbs_t* nmbs, int32_t timeout_ms);
|
||||
|
||||
/** Set the spacing between two sent bytes. This value is ignored when transport is not RTU.
|
||||
* @param nmbs pointer to the nmbs_t instance
|
||||
* @param timeout_ms time spacing in milliseconds.
|
||||
*/
|
||||
void nmbs_set_byte_spacing(nmbs_t* nmbs, uint32_t spacing_ms);
|
||||
|
||||
/** Set the pointer to user data argument passed to platform functions.
|
||||
* @param nmbs pointer to the nmbs_t instance
|
||||
* @param arg user data argument
|
||||
|
||||
Loading…
Reference in New Issue
Block a user