forked from machinekoder/mbus-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbus-serial.c
239 lines (188 loc) · 6.7 KB
/
mbus-serial.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//------------------------------------------------------------------------------
// Copyright (C) 2011, Robert Johansson, Raditex AB
// All rights reserved.
//
// FreeSCADA
// http://www.FreeSCADA.com
//
//------------------------------------------------------------------------------
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdio.h>
#include <strings.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <mbus/mbus.h>
#include <mbus/mbus-serial.h>
#define PACKET_BUFF_SIZE 2048
//------------------------------------------------------------------------------
/// Set up a serial connection handle.
//------------------------------------------------------------------------------
mbus_serial_handle *
mbus_serial_connect(char *device)
{
mbus_serial_handle *handle;
if (device == NULL)
{
return NULL;
}
if ((handle = (mbus_serial_handle *)malloc(sizeof(mbus_serial_handle))) == NULL)
{
fprintf(stderr, "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__);
return NULL;
}
handle->device = device; // strdup?
//
// create the SERIAL connection
//
// Use blocking read and handle it by serial port VMIN/VTIME setting
if ((handle->fd = open(handle->device, O_RDWR | O_NOCTTY)) < 0)
{
fprintf(stderr, "%s: failed to open tty.", __PRETTY_FUNCTION__);
return NULL;
}
bzero(&(handle->t), sizeof(handle->t));
handle->t.c_cflag |= (CS8|CREAD|CLOCAL);
handle->t.c_cflag |= PARENB;
// No received data still OK
handle->t.c_cc[VMIN] = 0;
// Wait at most 0.2 sec.Note that it starts after first received byte!!
// I.e. if CMIN>0 and there are no data we would still wait forever...
//
// The specification mentions link layer response timeout this way:
// The time structure of various link layer communication types is described in EN60870-5-1. The answer time
// between the end of a master send telegram and the beginning of the response telegram of the slave shall be
// between 11 bit times and (330 bit times + 50ms).
//
// For 2400Bd this means (330 + 11) / 2400 + 0.05 = 188.75 ms (added 11 bit periods to receive first byte).
// I.e. timeout of 0.2s seems appropriate for 2400Bd.
handle->t.c_cc[VTIME] = 2;
cfsetispeed(&(handle->t), B2400);
cfsetospeed(&(handle->t), B2400);
#ifdef MBUS_SERIAL_DEBUG
printf("%s: t.c_cflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_cflag);
printf("%s: t.c_oflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_oflag);
printf("%s: t.c_iflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_iflag);
printf("%s: t.c_lflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_lflag);
#endif
tcsetattr(handle->fd, TCSANOW, &(handle->t));
return handle;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_set_baudrate(mbus_serial_handle *handle, int baudrate)
{
if (handle == NULL)
return -1;
switch (baudrate)
{
case 300:
cfsetispeed(&(handle->t), B300);
cfsetospeed(&(handle->t), B300);
return 0;
case 1200:
cfsetispeed(&(handle->t), B1200);
cfsetospeed(&(handle->t), B1200);
return 0;
case 2400:
cfsetispeed(&(handle->t), B2400);
cfsetospeed(&(handle->t), B2400);
return 0;
case 9600:
cfsetispeed(&(handle->t), B9600);
cfsetospeed(&(handle->t), B9600);
return 0;
default:
return -1; // unsupported baudrate
}
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_set_timeout(mbus_serial_handle *handle, int timeout)
{
handle->t.c_cc[VTIME] = timeout / 100;
return 0;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_disconnect(mbus_serial_handle *handle)
{
if (handle == NULL)
{
return -1;
}
close(handle->fd);
free(handle);
return 0;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_send_frame(mbus_serial_handle *handle, mbus_frame *frame)
{
u_char buff[PACKET_BUFF_SIZE];
int len, ret;
if (handle == NULL || frame == NULL)
{
return -1;
}
if ((len = mbus_frame_pack(frame, buff, sizeof(buff))) == -1)
{
fprintf(stderr, "%s: mbus_frame_pack failed\n", __PRETTY_FUNCTION__);
return -1;
}
if ((ret = write(handle->fd, buff, len)) != len)
{
fprintf(stderr, "%s: Failed to write frame to socket (ret = %d: %s)\n", __PRETTY_FUNCTION__, ret, strerror(errno));
return -1;
}
return 0;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_recv_frame(mbus_serial_handle *handle, mbus_frame *frame)
{
char buff[PACKET_BUFF_SIZE];
int len, remaining, nread;
bzero((void *)buff, sizeof(buff));
//
// read data until a packet is received
//
remaining = 1; // start by reading 1 byte
len = 0;
do {
//printf("%s: Attempt to read %d bytes [len = %d]\n", __PRETTY_FUNCTION__, remaining, len);
if ((nread = read(handle->fd, &buff[len], remaining)) == -1)
{
// fprintf(stderr, "%s: aborting recv frame (remaining = %d, len = %d, nread = %d)\n",
// __PRETTY_FUNCTION__, remaining, len, nread);
return -1;
}
// printf("%s: Got %d byte [remaining %d, len %d]\n", __PRETTY_FUNCTION__, nread, remaining, len);
len += nread;
} while ((remaining = mbus_parse(frame, buff, len)) > 0);
if(remaining < 0)
{
// Would be OK when e.g. scanning the bus, otherwise it is a failure.
// printf("%s: M-Bus layer failed to receive complete data.\n", __PRETTY_FUNCTION__);
return -1;
}
if (len == -1)
{
fprintf(stderr, "%s: M-Bus layer failed to parse data.\n", __PRETTY_FUNCTION__);
return -1;
}
return 0;
}