2012-04-11 10:46:09 +09:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Copyright (C) 2010, Raditex AB
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// rSCADA
|
|
|
|
// http://www.rSCADA.se
|
|
|
|
// info@rscada.se
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <mbus/mbus-protocol.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2012-04-12 00:38:00 +02:00
|
|
|
int fd, len, i, result;
|
2012-04-11 10:46:09 +09:00
|
|
|
u_char raw_buff[4096], buff[4096], *ptr, *endptr;
|
|
|
|
mbus_frame reply;
|
|
|
|
mbus_frame_data frame_data;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
2012-04-16 21:00:24 +02:00
|
|
|
fprintf(stderr, "usage: %s hex-file\n", argv[0]);
|
2012-04-16 00:33:03 +02:00
|
|
|
return 1;
|
2012-04-11 10:46:09 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((fd = open(argv[1], O_RDONLY, 0)) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: failed to open '%s'", argv[0], argv[1]);
|
2012-04-16 00:33:03 +02:00
|
|
|
return 1;
|
2012-04-11 10:46:09 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
bzero(raw_buff, sizeof(raw_buff));
|
|
|
|
len = read(fd, raw_buff, sizeof(raw_buff));
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
ptr = 0;
|
|
|
|
endptr = raw_buff;
|
|
|
|
while (i < sizeof(buff)-1)
|
|
|
|
{
|
|
|
|
ptr = endptr;
|
|
|
|
buff[i] = (u_char)strtol(ptr, (char **)&endptr, 16);
|
|
|
|
|
|
|
|
// abort at non hex value
|
|
|
|
if (ptr == endptr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bzero(&reply, sizeof(reply));
|
|
|
|
bzero(&frame_data, sizeof(frame_data));
|
2012-04-12 00:38:00 +02:00
|
|
|
|
|
|
|
//mbus_parse_set_debug(1);
|
|
|
|
|
|
|
|
result = mbus_parse(&reply, buff, i);
|
2012-04-11 10:46:09 +09:00
|
|
|
|
2012-04-12 00:38:00 +02:00
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "mbus_parse: %s\n", mbus_error_str());
|
2012-04-16 00:33:03 +02:00
|
|
|
return 1;
|
2012-04-12 00:38:00 +02:00
|
|
|
}
|
|
|
|
else if (result > 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "mbus_parse: need %d more bytes\n", result);
|
2012-04-16 00:33:03 +02:00
|
|
|
return 1;
|
2012-04-12 00:38:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result = mbus_frame_data_parse(&reply, &frame_data);
|
2012-04-11 10:46:09 +09:00
|
|
|
|
2012-04-12 00:38:00 +02:00
|
|
|
if (result != 0)
|
|
|
|
{
|
|
|
|
mbus_frame_print(&reply);
|
|
|
|
fprintf(stderr, "mbus_frame_data_parse: %s\n", mbus_error_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//mbus_frame_print(&reply);
|
2012-04-11 10:46:09 +09:00
|
|
|
//mbus_frame_data_print(&frame_data);
|
2012-05-15 23:35:13 +02:00
|
|
|
printf("%s", mbus_frame_data_xml(&frame_data));
|
2012-04-16 00:33:03 +02:00
|
|
|
return 0;
|
2012-04-11 10:46:09 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|