-
Notifications
You must be signed in to change notification settings - Fork 3
/
pimaster2.c
417 lines (367 loc) · 11.8 KB
/
pimaster2.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// gcc pimaster2.c -o pimaster2
#include <unistd.h> //Needed for I2C port
#include <fcntl.h> //Needed for I2C port
#include <sys/ioctl.h> //Needed for I2C port
#include <linux/i2c-dev.h> //Needed for I2C port
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define I2C_ADDRESS_82 0x41 // 0x82 with 7 most significant bits
#define I2C_ADDRESS_60 0x30 // 0x60 with 7 most significant buts
const char MESSAGE_GETREGELAAR82[6] = {0x80, 0x90, 0xE0, 0x04, 0x00, 0x8A};
const int MESSAGE_GETREGELAAR82_LENGTH = 6;
const char MESSAGE_OPHALENSERIENUMMER[6] = {0x80, 0x90, 0xE1, 0x04, 0x00, 0x89};
const int MESSAGE_OPHALENSERIENUMMER_LENGTH = 6;
const char MESSAGE_OPHALENSETTING0[25] = {0x80,0xA4,0x10,0x04,0x13,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33};
const int MESSAGE_OPHALENSETTING0_LENGTH = 25;
const char MESSAGE_OPHALENSETTING50[25] = {0x80, 0xA4, 0x10, 0x04, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x01};
const int MESSAGE_OPHALENSETTING50_LENGTH = 25;
const char MESSAGE_OPHALENSETTING69[25] = {0x80,0xA4, 0x10, 0x04, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x00, 0xEE};
const int MESSAGE_OPHALENSETTING69_LENGTH = 25;
const char MESSAGE_OPHALENDATATYPE[6] = {0x80,0xA4,0x00,0x04,0x00,0x56};
const int MESSAGE_OPHALENDATATYPE_LENGTH = 6;
const char MESSAGE_DATALOG6[6] = {0x80, 0xA4, 0x01, 0x04, 0x00, 0x55};
const int MESSAGE_DATALOG6_LENGTH = 6;
const char MESSAGE_VRAAGCONFIG[10] = {0x80, 0xC0, 0x30, 0x04, 0x04, 0x00, 0x00, 0x00, 0x0C, 0xFA};
const int MESSAGE_VRAAGCONFIG_LENGTH = 10;
const char MESSAGE_VRAAGVENTIELAANWEZIG[10] = {0x80, 0xC0, 0x30, 0x04, 0x04, 0x01, 0x00, 0x00, 0x03, 0x02};
const int MESSAGE_VRAAGVENTIELAANWEZIG_LENGTH = 10;
const char MESSAGE_VRAAGCOUNTERS[6] = {0x80, 0xC2, 0x10, 0x04, 0x00, 0x28};
const int MESSAGE_VRAAGCOUNTERS_LENGTH = 6;
const int MESSAGE_SENDSETTING_LENGTH = 25;
const char MESSAGE_SENDSETTING[25] = {0x80, 0xA4, 0x10, 0x06, 0x13, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87, 0x00, 0xA9};
const char MESSAGE_C220[6] = {0x80, 0xC2, 0x20, 0x04, 0x00, 0x18};
const int MESSAGE_C220_LENGTH = 6;
char ophalenSettingMessage[26] = { 0x82, 0x80, 0xA4, 0x10, 0x04, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33 };
char ophalenConfigMessage[11] = { 0x82,0x80, 0xC0, 0x30, 0x04, 0x04, 0x01, 0x00, 0x00, 0x01, 0x04 };
int SendMessage(int fd, const char *message, int length);
int ReadBytes(int fd, int length);
void PrintHelp();
char *ConstructOphalensetting(int settingNr);
char CalculateChecksum(int length, char *buffer);
char *ConstructOphalenConfig(int settingNr, int counterNr);
int OpenBus(int addr);
void SendMessageForInput(int file_i2c, int c);
void HandleKeyBoardInput(int file_i2c);
int ReadInputFile(int file_i2c, char *filename);
void ConvertHexToChar(char *hex, char *buffer);
#define KEY_QUESTION 63
#define KEY_R 114
#define KEY_S 115
#define KEY_0 48
#define KEY_2 50
#define KEY_5 53
#define KEY_6 54
#define KEY_X 120
#define KEY_D 100 // ophalen datatype
#define KEY_UP_O 79 // ophalen autotemp settings
#define KEY_A 97 // ophalen datalog
#define KEY_C 99 // vraag config
#define KEY_I 105 // vraag ventielaanwezig
#define KEY_V 118 // vraag counters
#define KEY_O 111 // ophalen alle settings
#define KEY_N 110 // ophalen alle configs
#define KEY_Z 122 // zenden setting 135
int main(int argc, char *argv[])
{
int file_i2c_82 = OpenBus(I2C_ADDRESS_82);
if (file_i2c_82<0)
return file_i2c_82;
if (argc<2) // No arguments means keyboard input
{
HandleKeyBoardInput(file_i2c_82);
}
else if (argc==2) // One argument, means the keyboard input has been placed on the command line
{
int c = atoi(argv[1]);
SendMessageForInput(file_i2c_82, c);
// c = getchar();
}
else if (argc==3)
{
// This could be -f input.txt
// Read the input message from the given input file, and then send it
if (strcmp(argv[1],"-f")!=0)
{
printf("option is not supported; only -f supported\n");
return 1;
}
return ReadInputFile(file_i2c_82, argv[2]);
}
return 0;
}
int ReadInputFile(int file_i2c, char *filename)
{
int ch;
FILE *fp;
char byteBuffer[2];
char output[1024];
printf("Reading input file %s\n", filename);
fp = fopen(filename, "r"); // read mode.
if (fp == NULL)
{
printf("failed to open file\n");
return 2;
}
int count = 0;
int offset = 0;
do
{
ch = fgetc(fp);
if ((ch == '[') || (ch==']') || (ch==' '))
continue;
if (ch == EOF)
break ;
byteBuffer[count] = ch;
count++;
if (count==2)
{
count=0;
// check contents; if 0x then ignore
if ((byteBuffer[0]=='0') && (byteBuffer[1]=='x'))
continue;
output[offset++] = byteBuffer[0];
output[offset++] = byteBuffer[1];
}
} while (1);
for (int i=0;i<offset;i++)
printf("%c", output[i]);
printf("\n");
char buffer[offset];
ConvertHexToChar(output, buffer);
int startpos=0;
if (buffer[0]==0x82)
startpos = 1;
int length = offset/2;
if (SendMessage(file_i2c, buffer+startpos, length-startpos)==0)
printf("Send message, %d bytes\n", length-startpos);
fclose(fp);
return 0;
}
void ConvertHexToChar(char *hex, char *buffer)
{
char *pos = hex;
int length = strlen(hex)/2;
/* WARNING: no sanitization or error-checking whatsoever */
for (size_t count = 0; count < length; count++) {
sscanf(pos, "%2hhx", &buffer[count]);
pos += 2;
}
}
void HandleKeyBoardInput(int file_i2c)
{
int c;
do
{
if (c!=10)
printf("Type a command (? = help)\n");
c = getchar();
SendMessageForInput(file_i2c,c);
} while (c!=KEY_X);
}
void SendMessageForInput(int file_i2c, int c)
{
switch (c)
{
case KEY_QUESTION:
PrintHelp();
break;
case KEY_R:
if (SendMessage(file_i2c, MESSAGE_GETREGELAAR82, MESSAGE_GETREGELAAR82_LENGTH)==0)
printf("Send message GetRegelaar\n");
break;
case KEY_S:
if (SendMessage(file_i2c, MESSAGE_OPHALENSERIENUMMER, MESSAGE_OPHALENSERIENUMMER_LENGTH)==0)
printf("Send message Ophalen Serienummer\n");
break;
case KEY_0:
if (SendMessage(file_i2c, MESSAGE_OPHALENSETTING0, MESSAGE_OPHALENSETTING0_LENGTH)==0)
printf("Send message Ophalen Setting 0\n");
break;
case KEY_5:
if (SendMessage(file_i2c, MESSAGE_OPHALENSETTING50, MESSAGE_OPHALENSETTING50_LENGTH)==0)
printf("Send message Ophalen Setting 50\n");
break;
case KEY_2:
ConstructOphalensetting(62);
if (SendMessage(file_i2c, ophalenSettingMessage+1, 25)==0)
printf("Send message Ophalen Setting 62\n");
break;
case KEY_6:
if (SendMessage(file_i2c, MESSAGE_OPHALENSETTING69, MESSAGE_OPHALENSETTING69_LENGTH)==0)
printf("Send message Ophalen Setting 69\n");
break;
case KEY_D:
if (SendMessage(file_i2c, MESSAGE_OPHALENDATATYPE, MESSAGE_OPHALENDATATYPE_LENGTH)==0)
printf("Send message ophalen datatype\n");
break;
case KEY_A: // ophalen datalog
if (SendMessage(file_i2c, MESSAGE_DATALOG6, MESSAGE_DATALOG6_LENGTH)==0)
printf("Send message ophalen datalog\n");
break;
case KEY_C: // vraag config
if (SendMessage(file_i2c, MESSAGE_VRAAGCONFIG, MESSAGE_VRAAGCONFIG_LENGTH)==0)
printf("Send message vraag config\n");
break;
case KEY_I: // vraag ventielaanwezig
if (SendMessage(file_i2c, MESSAGE_VRAAGVENTIELAANWEZIG, MESSAGE_VRAAGVENTIELAANWEZIG_LENGTH)==0)
printf("Send message vraag ventiel aanwezig\n");
break;
case KEY_V: // vraag counters
if (SendMessage(file_i2c, MESSAGE_VRAAGCOUNTERS, MESSAGE_VRAAGCOUNTERS_LENGTH)==0)
printf("Send message vraag counters\n");
break;
case KEY_O:
for (int i=0;i<=0x94;i++)
{
ConstructOphalensetting(i);
if (SendMessage(file_i2c, ophalenSettingMessage+1, 25)==0)
printf("Send message Ophalensetting(%d)\n", i);
else
break;
sleep(5); // 1 second
}
break;
case KEY_UP_O:
for (int i=0;i<=95;i++)
{
ConstructOphalensetting(i);
if (SendMessage(file_i2c, ophalenSettingMessage+1, 25)==0)
printf("Send message Ophalensetting(%d)\n", i);
else
break;
sleep(5); // 1 second
}
break;
case KEY_N:
for (int i=0;i<=0x26;i++)
{
ConstructOphalenConfig(1, i);
if (SendMessage(file_i2c, ophalenConfigMessage+1, 10)==0)
printf("Send message OphalenConfig(1, %d)\n", i);
else
break;
sleep(5); // 1 second
}
for (int i=0;i<=0x29;i++)
{
ConstructOphalenConfig(0, i);
if (SendMessage(file_i2c, ophalenConfigMessage+1, 10)==0)
printf("Send message OphalenConfig(0, %d)\n", i);
else
break;
sleep(5); // 1 second
}
break;
case KEY_Z: // zenden setting
if (SendMessage(file_i2c, MESSAGE_SENDSETTING, MESSAGE_SENDSETTING_LENGTH)==0)
printf("Send message to set setting 134\n");
break;
case (int) 'q':
if (SendMessage(file_i2c, MESSAGE_C220, MESSAGE_C220_LENGTH)==0)
printf("Send message C2 20\n");
break;
case KEY_X:
printf("Stopping\n");
break;
case 10: // ignore the return
break;
default:
printf("Unknown command (%d)\n", c);
break;
}
}
int OpenBus(int addr)
{
//----- OPEN THE I2C BUS for specified slave -----
int file_i2c;
char *filename = (char*)"/dev/i2c-1";
if ((file_i2c = open(filename, O_RDWR)) < 0)
{
//ERROR HANDLING: you can check errno to see what went wrong
printf("Failed to open the i2c bus");
return -1;
}
if (ioctl(file_i2c, I2C_SLAVE, addr) < 0)
{
printf("Failed to acquire bus access and/or talk to slave.\n");
//ERROR HANDLING; you can check errno to see what went wrong
return -1;
}
return file_i2c;
}
void PrintHelp()
{
printf("? = Help\n");
printf("q = Versturen C2 20\n");
printf("r = GetRegelaar\n");
printf("s = Ophalen Serienummer\n");
printf("o = Ophalen alle WPU settings (0x00 tot 0x94 - 148)\n");
printf("O = Ophalen alle Autotemp settings (0 t/m 95)\n");
printf("0 = Ophalen Setting 0\n");
printf("2 = Ophalen Setting 62\n");
printf("5 = Ophalen Setting 50\n");
printf("6 = Ophalen Setting 69\n");
printf("d = Ophalen DataType\n");
printf("a = Ophalen datalog\n");
printf("c = Vraag config\n");
printf("n = Vraag alle configs (warmtepomp)\n");
printf("i = Ventiel aanwezig\n");
printf("v = Vraag counters\n");
printf("z = Zending setting 135 - 0.1\n");
printf("x = Exit\n");
}
int SendMessage(int fd, const char *message, int length)
{
//----- WRITE BYTES -----
// The first byte is the destination address, so can be skipped.
int written = write(fd, message, length);
if (written != length) //write() returns the number of bytes actually written, if it doesn't match then an error occurred (e.g. no response from the device)
{
/* ERROR HANDLING: i2c transaction failed */
printf("Failed to write message to the i2c bus. Written bytes:%d\n", written);
return -1;
}
return 0;
}
int ReadBytes(int fd, int length)
{
char buffer[60];
//----- READ BYTES -----
if (read(fd, buffer, length) != length) //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device)
{
//ERROR HANDLING: i2c transaction failed
printf("Failed to read from the i2c bus.\n");
return -1;
}
else
{
printf("Data read: %s\n", buffer);
return 0;
}
}
char *ConstructOphalenConfig(int settingNr, int counterNr)
{
// data byte 0 is called settingNr
// data byte 2 is called counterNr
ophalenConfigMessage[6+0] = (char) settingNr;
ophalenConfigMessage[6+2] = (char) counterNr;
ophalenConfigMessage[10] = CalculateChecksum(10, ophalenConfigMessage);
}
char *ConstructOphalensetting(int settingNr)
{
ophalenSettingMessage[17+6] = (char) settingNr;
ophalenSettingMessage[25] = CalculateChecksum(25, ophalenSettingMessage);
}
char CalculateChecksum(int length, char *buffer)
{
int total = 0;
for (int i = 0; i<length; i++)
{
total += (int) buffer[i];
}
int checksum = 256 - (total % 256);
if (checksum == 256)
checksum = 0;
return (char) checksum;
}