forked from machinekoder/mbus-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbus-serial-windows.c
338 lines (275 loc) · 9.69 KB
/
mbus-serial-windows.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//------------------------------------------------------------------------------
// Copyright (C) 2012, Alexander Rössler
// All rights reserved.
//
//
//------------------------------------------------------------------------------
#include <windows.h>
#include <commctrl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <string.h>
#include <mbus/mbus.h>
#include <mbus/mbus-serial-windows.h>
#define PACKET_BUFF_SIZE 2048
//------------------------------------------------------------------------------
/// Set up a serial connection handle.
//------------------------------------------------------------------------------
mbus_serial_handle *
mbus_serial_connect(char *device)
{
mbus_serial_handle *handle;
char buffer[100];
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 = buffer;
//map unix serial device names to windows serial device names
if (strcmp(device, "/dev/ttyS0") == 0)
strcpy(handle->device, "COM1");
else if (strcmp(device, "/dev/ttyS1") == 0)
strcpy(handle->device, "COM2");
else if (strcmp(device, "/dev/ttyS2") == 0)
strcpy(handle->device, "COM3");
else if (strcmp(device, "/dev/ttyS3") == 0)
strcpy(handle->device, "COM4");
else
strcpy(handle->device, device);
fprintf(stderr, "%s: Going to use device %s\n", __PRETTY_FUNCTION__, handle->device);
//
// create a SERIAL connection
//
handle->hSerial = CreateFile(handle->device,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (handle->hSerial == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
fprintf(stderr, "%s: serial port not found.\n", __PRETTY_FUNCTION__);
return NULL;
}
fprintf(stderr, "%s: failed to open serial device.\n", __PRETTY_FUNCTION__);
return NULL;
}
else {
fprintf(stderr, "%s: device opened %s\n", __PRETTY_FUNCTION__, handle->device);
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(handle->hSerial, &dcbSerialParams))
{
fprintf(stderr, "%s: error getting state.\n", __PRETTY_FUNCTION__);
return NULL;
}
dcbSerialParams.BaudRate = CBR_2400;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = EVENPARITY;
if (!SetCommState(handle->hSerial, &dcbSerialParams))
{
fprintf(stderr, "%s: error setting serial port state.\n", __PRETTY_FUNCTION__);
return NULL;
}
mbus_serial_set_timeout(handle, 200); //200ms default
return handle;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_set_baudrate(mbus_serial_handle *handle, int baudrate)
{
if (handle == NULL)
return -1;
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(handle->hSerial, &dcbSerialParams))
{
fprintf(stderr, "%s: error getting state.\n", __PRETTY_FUNCTION__);
return -1;
}
switch (baudrate)
{
case 300:
dcbSerialParams.BaudRate = CBR_300;
break;
case 1200:
dcbSerialParams.BaudRate = CBR_1200;
break;
case 2400:
dcbSerialParams.BaudRate = CBR_2400;
break;
case 9600:
dcbSerialParams.BaudRate = CBR_9600;
break;
default:
return -1; // unsupported baudrate
}
if (!SetCommState(handle->hSerial, &dcbSerialParams))
{
fprintf(stderr, "%s: error setting serial port state.\n", __PRETTY_FUNCTION__);
return -1;
}
return 0;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_set_timeout(mbus_serial_handle *handle, int timeout)
{
// 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.
COMMTIMEOUTS commTimeouts = {0};
commTimeouts.ReadIntervalTimeout = 0;
commTimeouts.ReadTotalTimeoutMultiplier = 0;
commTimeouts.ReadTotalTimeoutConstant = timeout;
commTimeouts.WriteTotalTimeoutMultiplier = 0;
commTimeouts.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts(handle->hSerial, &commTimeouts))
{
fprintf(stderr, "%s: error setting serial port timeouts.\n", __PRETTY_FUNCTION__);
return -1;
}
else
{
fprintf(stderr, "%s: timeout set to %d ms.\n", __PRETTY_FUNCTION__, timeout);
}
return 0;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_disconnect(mbus_serial_handle *handle)
{
if (handle == NULL)
{
return -1;
}
CloseHandle(handle->hSerial);
free(handle);
return 0;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_send_frame(mbus_serial_handle *handle, mbus_frame *frame)
{
u_char buff[PACKET_BUFF_SIZE];
DWORD dwBytesWritten = 0;
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;
}
fprintf(stderr, "mbus_serial_send_frame handle %x: len:%d\n", handle->hSerial, len);
printf("SEND :\n");
mbus_hexdump((const char * )&buff, len);
///
if (!WriteFile(handle->hSerial, buff, len, &dwBytesWritten, NULL))
{
fprintf(stderr, "%s: bytes not written\n", __PRETTY_FUNCTION__);
return -1;
}
else if (dwBytesWritten != len)
{
char lastError[1024];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lastError,
1024,
NULL);
fprintf(stderr, "%s: Failed to write frame to socket: \n", __PRETTY_FUNCTION__, lastError);
return -1;
}
else {
fprintf(stderr, "%s: bytes written %d\n", __PRETTY_FUNCTION__, len);
}
return 0;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_recv_frame(mbus_serial_handle *handle, mbus_frame *frame)
{
char buff[PACKET_BUFF_SIZE];
DWORD dwBytesRead;
int len, remaining, nread;
memset((void *)buff, '\0', sizeof(buff));
//
// read data until a packet is received
//
remaining = 1; // start by reading 1 byte
len = 0;
fprintf(stderr, "%s: Read Started.\n", __PRETTY_FUNCTION__);
do {
// fprintf(stderr, "%s: Read Loop.\n", __PRETTY_FUNCTION__);
if (ReadFile(handle->hSerial, &buff[len], remaining, &dwBytesRead, NULL) == 0)
{
//fprintf(stderr, "%s: ReadFile Failed.\n", __PRETTY_FUNCTION__);
fprintf(stderr, "%s: aborting recv frame (remaining = %d, len = %d, nread = %d)\n",
__PRETTY_FUNCTION__, remaining, len, dwBytesRead);
return -1;
}
else
{
fprintf(stderr, "%s: Read OK.\n", __PRETTY_FUNCTION__);
}
printf("%s: READ %d bytes [remaining %d, len %d]\n",__PRETTY_FUNCTION__, dwBytesRead, remaining, len);
if (dwBytesRead > 0)
mbus_hexdump((const char * )&buff[len], dwBytesRead);
len += dwBytesRead;
} 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: An error has occured %d but we continue\n",__PRETTY_FUNCTION__, remaining);
//return remaining;
return -1;
}
//else
// {
// printf("%s: Remaining %d :\n",__PRETTY_FUNCTION__,remaining);
// }
//printf("Final READ %d :\n",len);
//mbus_hexdump((const char * )&buff, len);
if (len == -1)
{
fprintf(stderr, "%s: M-Bus layer failed to parse data.\n", __PRETTY_FUNCTION__);
return -1;
}
return 0;
}