add new parameter to specify retries for mbus-serial-scan and

mbus-tcp-scan
make primary scan to retransmit SND_NKE in case of timeout so it's more
reliable but also slowlier
keep default behaviour, no retransmission
equalize mbus-serial-scan and mbus-tcp-scan
update man page
This commit is contained in:
Stefan Wahren 2013-04-25 21:19:41 +02:00
parent 65c036f0d2
commit ea0c6c3455
3 changed files with 133 additions and 48 deletions

View File

@ -10,9 +10,9 @@ the communication with M-Bus devices.
B<mbus-serial-switch-baudrate> [-b BAUDRATE] device address target-baudrate B<mbus-serial-switch-baudrate> [-b BAUDRATE] device address target-baudrate
B<mbus-serial-scan> [-d] [-b BAUDRATE] device B<mbus-serial-scan> [-d] [-b BAUDRATE] [-r RETRIES] device
B<mbus-tcp-scan> [-d] host port B<mbus-tcp-scan> [-d] [-r RETRIES] host port
B<mbus-serial-scan-secondary> [-d] [-b BAUDRATE] device [address-mask] B<mbus-serial-scan-secondary> [-d] [-b BAUDRATE] device [address-mask]
@ -72,6 +72,13 @@ Note that your MBus gateway and/or MBus device will most likely support only
a subset of these. The most commonlu used/supported rates are probably 9600, 2400 a subset of these. The most commonlu used/supported rates are probably 9600, 2400
and 300. and 300.
=item B<-r> I<RETRIES>
Maximum retransmissions. In case a MBus device doesn't reply to a request or
the response is erroneous, the MBus master can send a retransmissions.
libmbus supports the following range of retransmission: 0 until 9
=item B<-d> =item B<-d>
Enable debugging messages. Enable debugging messages.

View File

@ -15,6 +15,37 @@
static int debug = 0; static int debug = 0;
int ping_address(mbus_handle *handle, mbus_frame *reply, int address)
{
int i, ret = MBUS_RECV_RESULT_ERROR;
memset((void *)reply, 0, sizeof(mbus_frame));
for (i = 0; i <= handle->max_retry; i++)
{
if (debug)
{
printf("%d ", address);
fflush(stdout);
}
if (mbus_send_ping_frame(handle, address, 0) == -1)
{
printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str());
return MBUS_RECV_RESULT_ERROR;
}
ret = mbus_recv_frame(handle, reply);
if (ret != MBUS_RECV_RESULT_TIMEOUT)
{
return ret;
}
}
return ret;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Primary addressing scanning of mbus devices. // Primary addressing scanning of mbus devices.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -23,7 +54,7 @@ main(int argc, char **argv)
{ {
mbus_handle *handle; mbus_handle *handle;
char *device; char *device;
int address, baudrate = 9600; int address, baudrate = 9600, retries = 0;
int ret; int ret;
if (argc == 2) if (argc == 2)
@ -40,15 +71,39 @@ main(int argc, char **argv)
baudrate = atoi(argv[2]); baudrate = atoi(argv[2]);
device = argv[3]; device = argv[3];
} }
else if (argc == 4 && strcmp(argv[1], "-r") == 0)
{
retries = atoi(argv[2]);
device = argv[3];
}
else if (argc == 5 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-b") == 0) else if (argc == 5 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-b") == 0)
{ {
debug = 1; debug = 1;
baudrate = atoi(argv[3]); baudrate = atoi(argv[3]);
device = argv[4]; device = argv[4];
} }
else if (argc == 5 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-r") == 0)
{
debug = 1;
retries = atoi(argv[3]);
device = argv[4];
}
else if (argc == 6 && strcmp(argv[1], "-b") == 0 && strcmp(argv[3], "-r") == 0)
{
baudrate = atoi(argv[2]);
retries = atoi(argv[4]);
device = argv[5];
}
else if (argc == 7 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-b") == 0 && strcmp(argv[4], "-r") == 0)
{
debug = 1;
baudrate = atoi(argv[3]);
retries = atoi(argv[5]);
device = argv[6];
}
else else
{ {
fprintf(stderr, "usage: %s [-d] [-b BAUDRATE] device\n", argv[0]); printf("usage: %s [-d] [-b BAUDRATE] [-r RETRIES] device\n", argv[0]);
return 0; return 0;
} }
@ -60,13 +115,19 @@ main(int argc, char **argv)
if ((handle = mbus_context_serial(device)) == NULL) if ((handle = mbus_context_serial(device)) == NULL)
{ {
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); printf("Scan failed: Could not initialize M-Bus context: %s\n", mbus_error_str());
return 1; return 1;
} }
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
printf("Failed to setup connection to M-bus gateway\n"); printf("Scan failed: Could not setup connection to M-bus gateway: %s\n", mbus_error_str());
return 1;
}
if (mbus_context_set_option(handle, MBUS_OPTION_MAX_RETRY, retries) == -1)
{
printf("Failed to set retry count\n");
return 1; return 1;
} }
@ -83,30 +144,13 @@ main(int argc, char **argv)
{ {
mbus_frame reply; mbus_frame reply;
memset((void *)&reply, 0, sizeof(mbus_frame)); ret = ping_address(handle, &reply, address);
if (debug)
{
printf("%d ", address);
fflush(stdout);
}
if (mbus_send_ping_frame(handle, address, 0) == -1)
{
printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str());
return 1;
}
ret = mbus_recv_frame(handle, &reply);
if (ret == MBUS_RECV_RESULT_TIMEOUT) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
continue; continue;
} }
if (debug)
printf("\n");
if (ret == MBUS_RECV_RESULT_INVALID) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
/* check for more data (collision) */ /* check for more data (collision) */
@ -121,7 +165,6 @@ main(int argc, char **argv)
if (mbus_purge_frames(handle)) if (mbus_purge_frames(handle))
{ {
printf("Collision at address %d\n", address); printf("Collision at address %d\n", address);
continue; continue;
} }

View File

@ -13,15 +13,48 @@
#include <stdio.h> #include <stdio.h>
#include <mbus/mbus.h> #include <mbus/mbus.h>
static int debug = 0;
int ping_address(mbus_handle *handle, mbus_frame *reply, int address)
{
int i, ret = MBUS_RECV_RESULT_ERROR;
memset((void *)reply, 0, sizeof(mbus_frame));
for (i = 0; i <= handle->max_retry; i++)
{
if (debug)
{
printf("%d ", address);
fflush(stdout);
}
if (mbus_send_ping_frame(handle, address, 0) == -1)
{
printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str());
return MBUS_RECV_RESULT_ERROR;
}
ret = mbus_recv_frame(handle, reply);
if (ret != MBUS_RECV_RESULT_TIMEOUT)
{
return ret;
}
}
return ret;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Execution starts here: // Primary addressing scanning of mbus devices.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
mbus_handle *handle; mbus_handle *handle;
char *host; char *host;
int port, address, debug = 0; int port, address, retries = 0;
int ret; int ret;
if (argc == 3) if (argc == 3)
@ -35,9 +68,22 @@ main(int argc, char **argv)
host = argv[2]; host = argv[2];
port = atoi(argv[3]); port = atoi(argv[3]);
} }
else if (argc == 5 && strcmp(argv[1], "-r") == 0)
{
retries = atoi(argv[2]);
host = argv[3];
port = atoi(argv[4]);
}
else if (argc == 6 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-r") == 0)
{
debug = 1;
retries = atoi(argv[3]);
host = argv[4];
port = atoi(argv[5]);
}
else else
{ {
printf("usage: %s [-d] host port\n", argv[0]); printf("usage: %s [-d] [-r RETRIES] host port\n", argv[0]);
return 0; return 0;
} }
@ -59,6 +105,12 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (mbus_context_set_option(handle, MBUS_OPTION_MAX_RETRY, retries) == -1)
{
printf("Failed to set retry count\n");
return 1;
}
if (debug) if (debug)
printf("Scanning primary addresses:\n"); printf("Scanning primary addresses:\n");
@ -66,30 +118,13 @@ main(int argc, char **argv)
{ {
mbus_frame reply; mbus_frame reply;
memset((void *)&reply, 0, sizeof(mbus_frame)); ret = ping_address(handle, &reply, address);
if (debug)
{
printf("%d ", address);
fflush(stdout);
}
if (mbus_send_ping_frame(handle, address, 0) == -1)
{
printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str());
return 1;
}
ret = mbus_recv_frame(handle, &reply);
if (ret == MBUS_RECV_RESULT_TIMEOUT) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
continue; continue;
} }
if (debug)
printf("\n");
if (ret == MBUS_RECV_RESULT_INVALID) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
/* check for more data (collision) */ /* check for more data (collision) */