forked from machinekoder/mbus-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbus-serial-scan.c
123 lines (99 loc) · 2.99 KB
/
mbus-serial-scan.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
//------------------------------------------------------------------------------
// 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>
//------------------------------------------------------------------------------
// Primary addressing scanning of mbus devices.
//------------------------------------------------------------------------------
int
main(int argc, char **argv)
{
mbus_handle *handle;
char *device;
int address, baudrate = 2400, timeout = 200;
fprintf(stderr, "Hello, welcome to mbus\n");
if (argc == 2)
{
device = argv[1];
}
else if (argc == 4 && strcmp(argv[1], "-b") == 0)
{
baudrate = atoi(argv[2]);
device = argv[3];
}
else if (argc == 4 && strcmp(argv[1], "-t") == 0)
{
timeout = atoi(argv[2]);
device = argv[3];
}
else if (argc == 6 && strcmp(argv[1], "-b") == 0 && strcmp(argv[3], "-t") == 0)
{
baudrate = atoi(argv[2]);
timeout = atoi(argv[4]);
device = argv[5];
}
else
{
fprintf(stderr, "usage: %s [-b BAUDRATE] [-t TIMEOUT] device\n", argv[0]);
return 0;
}
fprintf(stderr, "Going to use device %s at baud %d and timeout %d\n", device, baudrate, timeout);
if ((handle = mbus_connect_serial(device)) == NULL)
{
printf("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 (mbus_serial_set_timeout(handle->m_serial_handle, timeout) == -1)
{
printf("Failed to set timeout.\n");
return -1;
}
for (address = 0; address < 254; address++)
{
mbus_frame reply;
memset((void *)&reply, 0, sizeof(mbus_frame));
if (mbus_send_ping_frame(handle, address) == -1)
{
printf("Scan failed. Could not send ping frame to handle %d and address %d: %s\n", handle, address, mbus_error_str());
continue;
// return -1;
}
if (mbus_recv_frame(handle, &reply) == -1)
{
printf("No reply address %d\n", address);
continue;
}
else {
printf("Got a reply\n");
mbus_hexdump((const char * )&reply, reply.length1);
}
int frame_type= mbus_frame_type(&reply);
if (frame_type == MBUS_FRAME_TYPE_ACK)
{
printf("Found a M-Bus device at address %d \n", address);
}
else
{
printf("not found addr:%d got frame type %d\n", address, frame_type);
}
}
mbus_disconnect(handle);
return 0;
}