diff --git a/bin/mbus-serial-request-data-multi-reply.c b/bin/mbus-serial-request-data-multi-reply.c index dcdf390..7d9b04b 100755 --- a/bin/mbus-serial-request-data-multi-reply.c +++ b/bin/mbus-serial-request-data-multi-reply.c @@ -19,6 +19,35 @@ static int debug = 0; +// +// init slave to get really the beginning of the records +// +int +init_slaves(mbus_handle *handle) +{ + if (debug) + printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__); + + if (mbus_send_ping_frame(handle, MBUS_ADDRESS_NETWORK_LAYER, 1) == -1) + { + return 0; + } + + // + // resend SND_NKE, maybe the first get lost + // + + if (debug) + printf("%s: debug: sending init frame #2\n", __PRETTY_FUNCTION__); + + if (mbus_send_ping_frame(handle, MBUS_ADDRESS_NETWORK_LAYER, 1) == -1) + { + return 0; + } + + return 1; +} + //------------------------------------------------------------------------------ // Scan for devices using secondary addressing. //------------------------------------------------------------------------------ @@ -91,51 +120,10 @@ main(int argc, char **argv) return 1; } - // - // init slave to get really the beginning of the records - // - - frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT); - - if (frame == NULL) + if (init_slaves(handle) == 0) { - fprintf(stderr, "Failed to allocate mbus frame.\n"); return 1; } - - 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 #1\n", __PRETTY_FUNCTION__); - - if (mbus_send_frame(handle, frame) == -1) - { - fprintf(stderr, "Failed to send mbus frame.\n"); - mbus_frame_free(frame); - return 1; - } - - mbus_purge_frames(handle); - - // - // 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; - } - - mbus_purge_frames(handle); if (strlen(addr_str) == 16) { diff --git a/bin/mbus-serial-scan-secondary.c b/bin/mbus-serial-scan-secondary.c index 0fcc544..a5b6ed2 100755 --- a/bin/mbus-serial-scan-secondary.c +++ b/bin/mbus-serial-scan-secondary.c @@ -19,6 +19,36 @@ static int debug = 0; +// +// init slave to get really the beginning of the records +// +int +init_slaves(mbus_handle *handle) +{ + if (debug) + printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__); + + if (mbus_send_ping_frame(handle, MBUS_ADDRESS_NETWORK_LAYER, 1) == -1) + { + return 0; + } + + // + // resend SND_NKE, maybe the first get lost + // + + if (debug) + printf("%s: debug: sending init frame #2\n", __PRETTY_FUNCTION__); + + if (mbus_send_ping_frame(handle, MBUS_ADDRESS_BROADCAST_NOREPLY, 1) == -1) + { + return 0; + } + + return 1; +} + + //------------------------------------------------------------------------------ // Scan for devices using secondary addressing. //------------------------------------------------------------------------------ @@ -140,33 +170,12 @@ main(int argc, char **argv) return 1; } - // - // init slaves - // - frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = MBUS_ADDRESS_NETWORK_LAYER; - - if (mbus_send_frame(handle, frame) == -1) + if (init_slaves(handle) == 0) { - fprintf(stderr, "Failed to send SND_NKE #1.\n"); - mbus_frame_free(frame); + free(addr_mask); return 1; } - (void) mbus_recv_frame(handle, &reply); - - frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY; - - if (mbus_send_frame(handle, frame) == -1) - { - fprintf(stderr, "Failed to send SND_NKE #2.\n"); - mbus_frame_free(frame); - return 1; - } - - (void) mbus_recv_frame(handle, &reply); - mbus_scan_2nd_address_range(handle, 0, addr_mask); mbus_disconnect(handle); diff --git a/bin/mbus-tcp-request-data-multi-reply.c b/bin/mbus-tcp-request-data-multi-reply.c index 7d421f0..c9e227e 100755 --- a/bin/mbus-tcp-request-data-multi-reply.c +++ b/bin/mbus-tcp-request-data-multi-reply.c @@ -77,29 +77,13 @@ main(int argc, char **argv) // // init slave to get really the beginning of the records // - - frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT); - - if (frame == NULL) - { - fprintf(stderr, "Failed to allocate mbus frame.\n"); - return 1; - } - - frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY; - if (debug) printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__); - - if (mbus_send_frame(handle, frame) == -1) + + if (mbus_send_ping_frame(handle, MBUS_ADDRESS_BROADCAST_NOREPLY, 1) == -1) { - fprintf(stderr, "Failed to send mbus frame.\n"); - mbus_frame_free(frame); return 1; } - - mbus_recv_frame(handle, &reply); if (strlen(addr_str) == 16) { diff --git a/bin/mbus-tcp-scan-secondary.c b/bin/mbus-tcp-scan-secondary.c index 71f532b..55b731f 100755 --- a/bin/mbus-tcp-scan-secondary.c +++ b/bin/mbus-tcp-scan-secondary.c @@ -74,40 +74,30 @@ main(int argc, char **argv) fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); return 1; } - - frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT); - - if (frame == NULL) - { - fprintf(stderr, "Failed to allocate mbus frame.\n"); - return 1; - } // // init slaves // - frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = MBUS_ADDRESS_NETWORK_LAYER; - - if (mbus_send_frame(handle, frame) == -1) - { - fprintf(stderr, "Failed to send SND_NKE #1.\n"); - mbus_frame_free(frame); - return 1; - } - - (void) mbus_recv_frame(handle, &reply); - - frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY; - - if (mbus_send_frame(handle, frame) == -1) + if (debug) + printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__); + + if (mbus_send_ping_frame(handle, MBUS_ADDRESS_NETWORK_LAYER, 1) == -1) { free(addr_mask); return 1; } - (void) mbus_recv_frame(handle, &reply); + // + // resend SND_NKE, maybe the first get lost + // + if (debug) + printf("%s: debug: sending init frame #2\n", __PRETTY_FUNCTION__); + + if (mbus_send_ping_frame(handle, MBUS_ADDRESS_BROADCAST_NOREPLY, 1) == -1) + { + free(addr_mask); + return 1; + } mbus_scan_2nd_address_range(handle, 0, addr_mask);