diff --git a/bin/mbus-serial-request-data-multi-reply.c b/bin/mbus-serial-request-data-multi-reply.c index 9dd1648..36831bc 100644 --- a/bin/mbus-serial-request-data-multi-reply.c +++ b/bin/mbus-serial-request-data-multi-reply.c @@ -98,10 +98,10 @@ main(int argc, char **argv) } frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY; + frame->address = MBUS_ADDRESS_NETWORK_LAYER; if (debug) - printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__); + printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__); if (mbus_send_frame(handle, frame) == -1) { @@ -110,8 +110,27 @@ main(int argc, char **argv) return 1; } - mbus_recv_frame(handle, &reply); + while (mbus_recv_frame(handle, &reply) != -1); + // + // resend SND_NKE, maybe the first get lost + // + + frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; + frame->address = MBUS_ADDRESS_NETWORK_LAYER; + + if (debug) + printf("%s: debug: sending init frame #2\n", __PRETTY_FUNCTION__); + + if (mbus_send_frame(handle, frame) == -1) + { + fprintf(stderr, "Failed to send mbus frame.\n"); + mbus_frame_free(frame); + return 1; + } + + while (mbus_recv_frame(handle, &reply) != -1); + if (strlen(addr_str) == 16) { // secondary addressing @@ -147,7 +166,7 @@ main(int argc, char **argv) // instead of the send and recv, use this sendrecv function that // takes care of the possibility of multi-telegram replies (limit = 16 frames) - if (mbus_sendrecv_request(handle, address, &reply, 16) == -1) + if (mbus_sendrecv_request(handle, address, &reply, 16) != 0) { fprintf(stderr, "Failed to send/receive M-Bus request.\n"); return 1; diff --git a/bin/mbus-serial-request-data.c b/bin/mbus-serial-request-data.c index d60a777..219a49d 100644 --- a/bin/mbus-serial-request-data.c +++ b/bin/mbus-serial-request-data.c @@ -126,7 +126,7 @@ main(int argc, char **argv) } } - if (mbus_recv_frame(handle, &reply) == -1) + if (mbus_recv_frame(handle, &reply) != 0) { fprintf(stderr, "Failed to receive M-Bus response frame.\n"); return 1; diff --git a/bin/mbus-serial-scan-secondary.c b/bin/mbus-serial-scan-secondary.c index 92dc3d3..960c066 100644 --- a/bin/mbus-serial-scan-secondary.c +++ b/bin/mbus-serial-scan-secondary.c @@ -127,7 +127,7 @@ main(int argc, char **argv) // init slaves // frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = 0xFD; + frame->address = MBUS_ADDRESS_NETWORK_LAYER; if (mbus_send_frame(handle, frame) == -1) { @@ -139,7 +139,7 @@ main(int argc, char **argv) (void) mbus_recv_frame(handle, &reply); frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = 0xFF; + frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY; if (mbus_send_frame(handle, frame) == -1) { diff --git a/bin/mbus-tcp-request-data-multi-reply.c b/bin/mbus-tcp-request-data-multi-reply.c index e06c022..7c165c0 100644 --- a/bin/mbus-tcp-request-data-multi-reply.c +++ b/bin/mbus-tcp-request-data-multi-reply.c @@ -130,7 +130,7 @@ main(int argc, char **argv) // instead of the send and recv, use this sendrecv function that // takes care of the possibility of multi-telegram replies (limit = 16 frames) - if (mbus_sendrecv_request(handle, address, &reply, 16) == -1) + if (mbus_sendrecv_request(handle, address, &reply, 16) != 0) { fprintf(stderr, "Failed to send/receive M-Bus request.\n"); return 1; diff --git a/bin/mbus-tcp-request-data.c b/bin/mbus-tcp-request-data.c index 7b40f28..fce7b7b 100644 --- a/bin/mbus-tcp-request-data.c +++ b/bin/mbus-tcp-request-data.c @@ -111,7 +111,7 @@ main(int argc, char **argv) } } - if (mbus_recv_frame(handle, &reply) == -1) + if (mbus_recv_frame(handle, &reply) != 0) { fprintf(stderr, "Failed to receive M-Bus response frame.\n"); return 1; diff --git a/bin/mbus-tcp-scan-secondary.c b/bin/mbus-tcp-scan-secondary.c index e2cca2a..6a97484 100644 --- a/bin/mbus-tcp-scan-secondary.c +++ b/bin/mbus-tcp-scan-secondary.c @@ -75,7 +75,7 @@ main(int argc, char **argv) // init slaves // frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = 0xFD; + frame->address = MBUS_ADDRESS_NETWORK_LAYER; if (mbus_send_frame(handle, frame) == -1) { @@ -88,7 +88,7 @@ main(int argc, char **argv) sleep(1); frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = 0xFF; + frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY; if (mbus_send_frame(handle, frame) == -1) { diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index 2323bec..bba0cbc 100644 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1389,6 +1389,12 @@ mbus_recv_frame(mbus_handle * handle, mbus_frame *frame) return 0; } + if (frame == NULL) + { + MBUS_ERROR("%s: Invalid frame.\n", __PRETTY_FUNCTION__); + return 0; + } + if (handle->is_serial) { result = mbus_serial_recv_frame(handle->m_serial_handle, frame); @@ -1594,6 +1600,8 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m // next_frame = reply; + memset((void *)&reply_data, 0, sizeof(mbus_frame_data)); + while (more_frames) { frame_count++; @@ -1601,7 +1609,7 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m if (debug) printf("%s: debug: receiving response frame #%d\n", __PRETTY_FUNCTION__, frame_count); - if (mbus_recv_frame(handle, next_frame) == -1) + if (mbus_recv_frame(handle, next_frame) != 0) { MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__); retval = 1; @@ -1905,7 +1913,7 @@ int mbus_read_slave(mbus_handle * handle, mbus_address *address, mbus_frame * re } } - if (mbus_recv_frame(handle, reply) == -1) + if (mbus_recv_frame(handle, reply) != 0) { MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__);