2012-04-11 10:46:09 +09: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>
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Execution starts here:
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
mbus_handle *handle;
|
|
|
|
mbus_frame reply;
|
2012-09-01 22:14:21 +02:00
|
|
|
char *host, *addr = NULL;
|
2012-04-12 00:38:00 +02:00
|
|
|
int port, ret;
|
2012-04-11 10:46:09 +09:00
|
|
|
|
|
|
|
if (argc != 4)
|
|
|
|
{
|
2013-04-26 08:18:17 +02:00
|
|
|
fprintf(stderr,"usage: %s host port secondary-mbus-address\n", argv[0]);
|
2012-04-11 10:46:09 +09:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
host = argv[1];
|
|
|
|
port = atoi(argv[2]);
|
2012-09-01 22:14:21 +02:00
|
|
|
|
|
|
|
if ((addr = strdup(argv[3])) == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to allocate address.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2012-04-11 10:46:09 +09:00
|
|
|
|
2012-12-31 13:44:03 +01:00
|
|
|
if (mbus_is_secondary_address(addr) == 0)
|
2012-04-11 10:46:09 +09:00
|
|
|
{
|
2013-04-26 08:18:17 +02:00
|
|
|
fprintf(stderr,"Misformatted secondary address. Must be 16 character HEX number.\n");
|
2012-04-11 10:46:09 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-04 19:49:54 +02:00
|
|
|
if ((handle = mbus_context_tcp(host, port)) == NULL)
|
2012-04-11 10:46:09 +09:00
|
|
|
{
|
2012-07-04 19:49:54 +02:00
|
|
|
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-09 12:33:32 +02:00
|
|
|
if (mbus_connect(handle) == -1)
|
2012-07-04 19:49:54 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
|
2012-04-11 10:46:09 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbus_send_select_frame(handle, addr) == -1)
|
|
|
|
{
|
2013-04-26 08:18:17 +02:00
|
|
|
fprintf(stderr,"Failed to send selection frame: %s\n", mbus_error_str());
|
2012-04-11 10:46:09 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbus_recv_frame(handle, &reply);
|
|
|
|
|
2012-07-11 23:33:52 +02:00
|
|
|
if (ret == MBUS_RECV_RESULT_TIMEOUT)
|
2012-04-11 10:46:09 +09:00
|
|
|
{
|
2013-04-26 08:18:17 +02:00
|
|
|
fprintf(stderr,"No reply from device with secondary address %s: %s\n", argv[3], mbus_error_str());
|
2012-04-11 10:46:09 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-11 23:33:52 +02:00
|
|
|
if (ret == MBUS_RECV_RESULT_INVALID)
|
2012-04-11 10:46:09 +09:00
|
|
|
{
|
2013-04-26 08:18:17 +02:00
|
|
|
fprintf(stderr,"Invalid reply from %s: The address address probably match more than one device: %s\n", argv[3], mbus_error_str());
|
2012-04-11 10:46:09 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK)
|
|
|
|
{
|
2012-12-20 20:42:10 +01:00
|
|
|
if (mbus_send_request_frame(handle, MBUS_ADDRESS_NETWORK_LAYER) == -1)
|
2012-04-11 10:46:09 +09:00
|
|
|
{
|
2013-04-26 08:18:17 +02:00
|
|
|
fprintf(stderr,"Failed to send request to selected secondary device: %s\n", mbus_error_str());
|
2012-04-11 10:46:09 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-11 23:33:52 +02:00
|
|
|
if (mbus_recv_frame(handle, &reply) != MBUS_RECV_RESULT_OK)
|
2012-04-11 10:46:09 +09:00
|
|
|
{
|
2013-04-26 08:18:17 +02:00
|
|
|
fprintf(stderr,"Failed to recieve reply from selected secondary device: %s\n", mbus_error_str());
|
2012-04-11 10:46:09 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbus_frame_type(&reply) != MBUS_FRAME_TYPE_ACK)
|
|
|
|
{
|
|
|
|
printf("Recieved a reply from secondarily addressed device: Searched for [%s] and found [%s].\n",
|
|
|
|
argv[3], mbus_frame_get_secondary_address(&reply));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-26 08:18:17 +02:00
|
|
|
fprintf(stderr,"Unknown reply:\n");
|
2012-04-11 10:46:09 +09:00
|
|
|
mbus_frame_print(&reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(addr);
|
|
|
|
mbus_disconnect(handle);
|
2012-07-04 19:49:54 +02:00
|
|
|
mbus_context_free(handle);
|
2012-04-11 10:46:09 +09:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|