Added NMBS_ERROR_CRC and NMBS_ERROR_INVALID_TCP_MBAP nmbs_error
This commit is contained in:
parent
3904a6c52f
commit
271f689771
@ -130,9 +130,8 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
err = nmbs_server_poll(&nmbs);
|
||||
if (err != NMBS_ERROR_NONE) {
|
||||
fprintf(stderr, "Error polling modbus connection - %s\n", nmbs_strerror(err));
|
||||
// In a more complete example, we should handle this error by closing the connection from our side
|
||||
break;
|
||||
fprintf(stderr, "Error on modbus connection - %s\n", nmbs_strerror(err));
|
||||
// In a more complete example, we would handle this error by checking its nmbs_error value
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
12
nanomodbus.c
12
nanomodbus.c
@ -249,7 +249,7 @@ static nmbs_error recv_msg_footer(nmbs_t* nmbs) {
|
||||
uint16_t recv_crc = get_2(nmbs);
|
||||
|
||||
if (recv_crc != crc)
|
||||
return NMBS_ERROR_TRANSPORT;
|
||||
return NMBS_ERROR_CRC;
|
||||
}
|
||||
|
||||
DEBUG("\n");
|
||||
@ -315,10 +315,10 @@ static nmbs_error recv_msg_header(nmbs_t* nmbs, bool* first_byte_received) {
|
||||
nmbs->msg.fc = get_1(nmbs);
|
||||
|
||||
if (protocol_id != 0)
|
||||
return NMBS_ERROR_TRANSPORT;
|
||||
return NMBS_ERROR_INVALID_TCP_MBAP;
|
||||
|
||||
if (length > 255)
|
||||
return NMBS_ERROR_TRANSPORT;
|
||||
return NMBS_ERROR_INVALID_TCP_MBAP;
|
||||
}
|
||||
|
||||
return NMBS_ERROR_NONE;
|
||||
@ -1288,6 +1288,12 @@ nmbs_error nmbs_receive_raw_pdu_response(nmbs_t* nmbs, void* data_out, uint32_t
|
||||
#ifndef NMBS_STRERROR_DISABLED
|
||||
const char* nmbs_strerror(nmbs_error error) {
|
||||
switch (error) {
|
||||
case NMBS_ERROR_INVALID_TCP_MBAP:
|
||||
return "invalid TCP MBAP received";
|
||||
|
||||
case NMBS_ERROR_CRC:
|
||||
return "invalid CRC received";
|
||||
|
||||
case NMBS_ERROR_TRANSPORT:
|
||||
return "transport error";
|
||||
|
||||
|
||||
@ -54,6 +54,8 @@
|
||||
*/
|
||||
typedef enum nmbs_error {
|
||||
// Library errors
|
||||
NMBS_ERROR_INVALID_TCP_MBAP = -6, /**< Received invalid TCP MBAP */
|
||||
NMBS_ERROR_CRC = -5, /**< Received invalid CRC */
|
||||
NMBS_ERROR_TRANSPORT = -4, /**< Transport error */
|
||||
NMBS_ERROR_TIMEOUT = -3, /**< Read/write timeout occurred */
|
||||
NMBS_ERROR_INVALID_RESPONSE = -2, /**< Received invalid response from server */
|
||||
|
||||
Loading…
Reference in New Issue
Block a user