2012-04-23 20:56:26 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Copyright (C) 2011, Robert Johansson, Raditex AB
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// rSCADA
|
|
|
|
// http://www.rSCADA.se
|
|
|
|
// info@rscada.se
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <mbus/mbus.h>
|
|
|
|
|
|
|
|
static int debug = 0;
|
|
|
|
|
2012-09-01 22:52:55 +02:00
|
|
|
//
|
|
|
|
// 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;
|
2012-10-04 22:43:06 +02:00
|
|
|
}
|
2012-09-01 22:52:55 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2012-04-23 20:56:26 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Scan for devices using secondary addressing.
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2012-05-20 03:44:06 +02:00
|
|
|
mbus_frame *frame, reply;
|
2012-04-23 20:56:26 +02:00
|
|
|
mbus_frame_data reply_data;
|
|
|
|
mbus_handle *handle = NULL;
|
|
|
|
|
2012-05-20 03:55:36 +02:00
|
|
|
char *device, *addr_str, *xml_result;
|
2012-04-23 20:56:26 +02:00
|
|
|
int address, baudrate = 9600;
|
|
|
|
|
|
|
|
memset((void *)&reply, 0, sizeof(mbus_frame));
|
|
|
|
memset((void *)&reply_data, 0, sizeof(mbus_frame_data));
|
|
|
|
|
|
|
|
if (argc == 3)
|
|
|
|
{
|
|
|
|
device = argv[1];
|
|
|
|
addr_str = argv[2];
|
|
|
|
}
|
|
|
|
else if (argc == 4 && strcmp(argv[1], "-d") == 0)
|
|
|
|
{
|
|
|
|
device = argv[2];
|
|
|
|
addr_str = argv[3];
|
|
|
|
debug = 1;
|
|
|
|
}
|
|
|
|
else if (argc == 5 && strcmp(argv[1], "-b") == 0)
|
|
|
|
{
|
|
|
|
baudrate = atoi(argv[2]);
|
|
|
|
device = argv[3];
|
|
|
|
addr_str = argv[4];
|
|
|
|
}
|
|
|
|
else if (argc == 6 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-b") == 0)
|
|
|
|
{
|
|
|
|
baudrate = atoi(argv[3]);
|
|
|
|
device = argv[4];
|
|
|
|
addr_str = argv[5];
|
|
|
|
debug = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: %s [-d] [-b BAUDRATE] device mbus-address\n", argv[0]);
|
|
|
|
fprintf(stderr, " optional flag -d for debug printout\n");
|
|
|
|
fprintf(stderr, " optional flag -b for selecting baudrate\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-05-15 22:57:03 +02:00
|
|
|
|
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
mbus_register_send_event(&mbus_dump_send_event);
|
|
|
|
mbus_register_recv_event(&mbus_dump_recv_event);
|
|
|
|
}
|
2012-04-23 20:56:26 +02:00
|
|
|
|
2012-07-04 19:49:54 +02:00
|
|
|
if ((handle = mbus_context_serial(device)) == NULL)
|
2012-04-23 20:56:26 +02:00
|
|
|
{
|
2012-07-04 19:49:54 +02:00
|
|
|
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
2012-04-23 20:56:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-13 23:01:29 +02:00
|
|
|
if (mbus_connect(handle) == -1)
|
2012-07-04 19:49:54 +02:00
|
|
|
{
|
|
|
|
printf("Failed to setup connection to M-bus gateway\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbus_serial_set_baudrate(handle, baudrate) == -1)
|
2012-04-23 20:56:26 +02:00
|
|
|
{
|
|
|
|
printf("Failed to set baud rate.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2012-05-20 03:44:06 +02:00
|
|
|
|
2012-09-01 22:52:55 +02:00
|
|
|
if (init_slaves(handle) == 0)
|
2012-05-20 03:44:06 +02:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2012-07-04 21:21:01 +02:00
|
|
|
|
2012-04-23 20:56:26 +02:00
|
|
|
if (strlen(addr_str) == 16)
|
|
|
|
{
|
|
|
|
// secondary addressing
|
|
|
|
|
2012-05-20 03:55:36 +02:00
|
|
|
int ret;
|
2012-04-23 20:56:26 +02:00
|
|
|
|
2012-05-20 03:55:36 +02:00
|
|
|
ret = mbus_select_secondary_address(handle, addr_str);
|
2012-04-23 20:56:26 +02:00
|
|
|
|
2012-05-20 03:55:36 +02:00
|
|
|
if (ret == MBUS_PROBE_COLLISION)
|
2012-04-23 20:56:26 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error: The address mask [%s] matches more than one device.\n", __PRETTY_FUNCTION__, addr_str);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-05-20 03:55:36 +02:00
|
|
|
else if (ret == MBUS_PROBE_NOTHING)
|
2012-04-23 20:56:26 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error: The selected secondary address does not match any device [%s].\n", __PRETTY_FUNCTION__, addr_str);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-05-20 03:55:36 +02:00
|
|
|
else if (ret == MBUS_PROBE_ERROR)
|
2012-04-23 20:56:26 +02:00
|
|
|
{
|
2012-05-20 03:55:36 +02:00
|
|
|
fprintf(stderr, "%s: Error: Failed to select secondary address [%s].\n", __PRETTY_FUNCTION__, addr_str);
|
2012-04-23 20:56:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// else MBUS_PROBE_SINGLE
|
|
|
|
|
|
|
|
address = 253;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// primary addressing
|
|
|
|
address = atoi(addr_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
// instead of the send and recv, use this sendrecv function that
|
2012-05-13 16:39:48 +09:00
|
|
|
// takes care of the possibility of multi-telegram replies (limit = 16 frames)
|
2012-06-20 00:23:25 +02:00
|
|
|
if (mbus_sendrecv_request(handle, address, &reply, 16) != 0)
|
2012-04-23 20:56:26 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to send/receive M-Bus request.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// dump hex data if debug is true
|
|
|
|
//
|
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
mbus_frame_print(&reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// generate XML and print to standard output
|
|
|
|
//
|
2012-05-13 16:34:12 +09:00
|
|
|
if ((xml_result = mbus_frame_xml(&reply)) == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to generate XML representation of MBUS frames: %s\n", mbus_error_str());
|
|
|
|
return 1;
|
|
|
|
}
|
2012-05-29 21:24:30 +02:00
|
|
|
|
2012-05-13 16:34:12 +09:00
|
|
|
printf("%s", xml_result);
|
2012-05-29 21:24:30 +02:00
|
|
|
free(xml_result);
|
2012-04-23 20:56:26 +02:00
|
|
|
|
|
|
|
mbus_disconnect(handle);
|
2012-07-04 19:49:54 +02:00
|
|
|
mbus_context_free(handle);
|
2012-04-23 20:56:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|