forked from machinekoder/mbus-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbus-serial-request-data.c
150 lines (128 loc) · 4.03 KB
/
mbus-serial-request-data.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//------------------------------------------------------------------------------
// Copyright (C) 2011, Robert Johansson, Raditex AB
// All rights reserved.
//
// FreeSCADA
// http://www.FreeSCADA.com
//
//------------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <mbus/mbus.h>
static int debug = 0;
//------------------------------------------------------------------------------
// Scan for devices using secondary addressing.
//------------------------------------------------------------------------------
int
main(int argc, char **argv)
{
mbus_frame reply;
mbus_frame_data reply_data;
mbus_handle *handle = NULL;
char *device, *addr_str, matching_addr[16];
int address, baudrate = 9600;
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;
}
if ((handle = mbus_connect_serial(device)) == NULL)
{
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
return -1;
}
if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1)
{
printf("Failed to set baud rate.\n");
return -1;
}
if (strlen(addr_str) == 16)
{
// secondary addressing
int probe_ret;
probe_ret = mbus_probe_secondary_address(handle, addr_str, matching_addr);
if (probe_ret == MBUS_PROBE_COLLISION)
{
fprintf(stderr, "%s: Error: The address mask [%s] matches more than one device.\n", __PRETTY_FUNCTION__, addr_str);
return -1;
}
else if (probe_ret == MBUS_PROBE_NOTHING)
{
fprintf(stderr, "%s: Error: The selected secondary address does not match any device [%s].\n", __PRETTY_FUNCTION__, addr_str);
return -1;
}
else if (probe_ret == MBUS_PROBE_ERROR)
{
fprintf(stderr, "%s: Error: Failed to probe secondary address [%s].\n", __PRETTY_FUNCTION__, addr_str);
return -1;
}
// else MBUS_PROBE_SINGLE
if (mbus_send_request_frame(handle, 253) == -1)
{
fprintf(stderr, "Failed to send M-Bus request frame.\n");
return -1;
}
}
else
{
// primary addressing
address = atoi(addr_str);
if (mbus_send_request_frame(handle, address) == -1)
{
fprintf(stderr, "Failed to send M-Bus request frame.\n");
return -1;
}
}
if (mbus_recv_frame(handle, &reply) == -1)
{
fprintf(stderr, "Failed to receive M-Bus response frame.\n");
return -1;
}
//
// parse data and print in XML format
//
if (debug)
{
mbus_frame_print(&reply);
}
mbus_frame_data_parse(&reply, &reply_data);
printf("%s", mbus_frame_data_xml(&reply_data));
// manual free
if (reply_data.data_var.record)
{
mbus_data_record_free(reply_data.data_var.record); // free's up the whole list
}
mbus_disconnect(handle);
return 0;
}