From 1fb68d1e05d8ea6d65243c6166b116ec181d4644 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Sun, 20 May 2012 03:35:04 +0200 Subject: [PATCH] Fixed primary address scan - scan only normal addresses (0-250) - show collisions in scan result --- bin/mbus-serial-scan.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/bin/mbus-serial-scan.c b/bin/mbus-serial-scan.c index f0d1776..3d7c8bf 100644 --- a/bin/mbus-serial-scan.c +++ b/bin/mbus-serial-scan.c @@ -28,6 +28,7 @@ main(int argc, char **argv) mbus_handle *handle; char *device; int address, baudrate = 9600; + int ret; if (argc == 2) { @@ -76,7 +77,7 @@ main(int argc, char **argv) if (debug) printf("Scanning primary addresses:\n"); - for (address = 0; address < 254; address++) + for (address = 0; address <= 250; address++) { mbus_frame reply; @@ -93,16 +94,41 @@ main(int argc, char **argv) printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str()); return 1; } + + ret = mbus_recv_frame(handle, &reply); - if (mbus_recv_frame(handle, &reply) == -1) + if (ret == -1) { continue; - } + } + + if (debug) + printf("\n"); + + if (ret == -2) + { + /* check for more data (collision) */ + while (mbus_recv_frame(handle, &reply) != -1); + + printf("Collision at address %d\n", address); + + continue; + } if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK) { - if (debug) - printf("\n"); + /* check for more data (collision) */ + while (mbus_recv_frame(handle, &reply) != -1) + { + ret = -2; + } + + if (ret == -2) + { + printf("Collision at address %d\n", address); + + continue; + } printf("Found a M-Bus device at address %d\n", address); }