aboutsummaryrefslogtreecommitdiff
path: root/mycan.c
blob: 3eccce72c39006cb830b9cf21884212da49728cc (plain)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// references:
// https://www.waveshare.com/wiki/RS485_CAN_HAT
// https://github.com/howerj/dbcc MIT

/* build:
cc -Wall -c all_SRETest.c
cc -Wall -c mycan.c
cc -Wall mycan.o all_SRETest.o -o mycan
*/

/* test:
sudo ip link add type vcan
sudo ip link set dev vcan0 type vcan
sudo ip link set dev vcan0 up
cansend vcan0 630#02.00.02.00.02.00.02.00
cansend vcan0 680#02.00.02.00.02.00.02.00
# if cell temperature is signed (can be negative), 00.FF sends -256
cansend vcan0 680#00.FF.02.00.02.00.02.00
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

// (GPL-2.0-only WITH Linux-syscall-note) OR BSD-3-Clause
#include <linux/can.h>
#include <linux/can/raw.h>

// time()
#include <time.h>

#include "all_SRE_edited.h"

#define CAN_VOLT_LEN 24
#define CAN_TEMP_LEN 16
// max wire unsigned number is 65535
// max wire signed number maybe is -32768 to 32767
// voltage max format 7 bytes ,6.5535
// temp max format 4 bytes ,255
// timestamp max 10 bytes 1745742182
// 1 timestamp, 96 voltage, 48 temp, 1 \n
// 10+96*7+96*4+1=1067 bytes
#define ENTRY_SIZE 1067

// Convert can message to uint64_t
// https://github.com/howerj/dbcc MIT
static uint64_t u64_from_can_msg(const uint8_t m[8]) {
	return ((uint64_t)m[7] << 56) | ((uint64_t)m[6] << 48) | ((uint64_t)m[5] << 40) | ((uint64_t)m[4] << 32) 
		| ((uint64_t)m[3] << 24) | ((uint64_t)m[2] << 16) | ((uint64_t)m[1] << 8) | ((uint64_t)m[0] << 0);
}

int main(int argc, char **argv)
{
	int ret;
	int s, nbytes;
	struct sockaddr_can addr;
	struct ifreq ifr;
	struct can_frame frame;
	char *filename;
	// Initialize filename to $XDG_DATA_HOME/mycan.csv. If no XDG_DATA_HOME
	// environment variable, then initialize to $HOME/.local/share/mycan.csv
	{
		// usually ~/.local/share
		const char *dir=getenv("XDG_DATA_HOME");
		const char *str;
		if(dir != NULL)
			str="/mycan.csv";
		else
		{
			str="/.local/share/mycan.csv";
			dir=getenv("HOME");
		}
		filename=malloc((strlen(dir)+strlen(str)+1)*sizeof(char));
		sprintf(filename,"%s%s",dir,str);
	}

	can_obj_all_sre_edited_h_t obj;
	dbcc_time_stamp_t t, t_before;
	uint64_t can_message_u64;
	//memset(&obj, 0, sizeof(can_obj_all_sretest_h_t));

	memset(&frame, 0, sizeof(struct can_frame));

	// No need any more because I use systemd unit to autostart configure CAN network interface
	//system("sudo ip link set can0 type can bitrate 500000");
	//system("sudo ifconfig can0 up");

	// Check and fix corrupted .csv file due to power failure.
	for(FILE *fp;(fp=fopen(filename,"r"))!=NULL;)
	{
		char ch;
		uint32_t offset;
		int remain;
		fseek(fp,-1L,SEEK_END);
		fscanf(fp,"%c",&ch);
		fseek(fp,0L,SEEK_END);
		offset=ftell(fp);
		//printf("csv file end offset: %d\n",offset);
		fclose(fp);
		remain=offset%ENTRY_SIZE;
		if(remain)
		{
			printf("Corrupted .csv file not a multiple of %d, removing last line\n",ENTRY_SIZE);
			truncate(filename,offset-remain);
		}
		else if(ch != '\n')
		{
			printf("Corrupted .csv file last character not \\n, removing last %d bytes of data\n",ENTRY_SIZE);
			truncate(filename,offset-ENTRY_SIZE);
		}
		else
			break;
	}
	// need fflush for systemd to log my stdout
	// without fflush, fprintf stderr works, printf not work, fprintf stdout not work
	// maybe other ways to flush buffers or disable buffering (with setbuf and setvbuf) also works
	// https://unix.stackexchange.com/questions/725331#comment1376185_725331
	// https://stackoverflow.com/a/7876743
	// https://unix.stackexchange.com/questions/785686#comment1505589_785686
	fflush(stdout);

	// https://www.waveshare.com/wiki/RS485_CAN_HAT
	// Using Linux kernel's SocketCAN
	// https://docs.kernel.org/networking/can.html
	//1.Create socket
	s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
	if (s < 0) {
		perror("socket PF_CAN failed");
		return 1;
	}

	//2.Specify can0 device
	if(argc>1)
		strcpy(ifr.ifr_name, argv[1]);
	else
		strcpy(ifr.ifr_name, "can0");
	ret = ioctl(s, SIOCGIFINDEX, &ifr);
	if (ret < 0) {
		perror("ioctl failed");
		return 1;
	}

	//3.Bind the socket to can0
	addr.can_family = PF_CAN;
	addr.can_ifindex = ifr.ifr_ifindex;
	ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
	if (ret < 0) {
		perror("bind failed");
		return 1;
	}

	//4.Define receive rules
	// Adding masks to the socket, to only read CAN ids I want.
	// 24 (voltage) + 16 (temp) can ids
	struct can_filter rfilter[CAN_VOLT_LEN+CAN_TEMP_LEN];
	rfilter[0].can_id = 0x630;
	rfilter[0].can_mask = CAN_SFF_MASK;
	rfilter[CAN_VOLT_LEN].can_id = 0x680;
	rfilter[CAN_VOLT_LEN].can_mask = CAN_SFF_MASK;
	for(int i=1;i<CAN_VOLT_LEN;i++)
	{
		rfilter[i].can_id = rfilter[i-1].can_id+1;
		rfilter[i].can_mask = CAN_SFF_MASK;
	}
	for(int i=CAN_VOLT_LEN+1;i<CAN_VOLT_LEN+CAN_TEMP_LEN;i++)
	{
		rfilter[i].can_id = rfilter[i-1].can_id+1;
		rfilter[i].can_mask = CAN_SFF_MASK;
	}
	setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));

	//5.Receive data
	for(t_before=time(NULL);;)
	{
		nbytes = read(s, &frame, sizeof(frame));
		if(nbytes > 0) {
			double out;
			uint8_t iout;
			FILE * fp;

			if((fp=fopen(filename,"a"))==NULL)
			{
				fprintf(stderr,"Can't open file \"%s\".\n",filename);
				exit(1);
			}

			//printf("can_id = 0x%X\r\ncan_dlc = %d \r\n", frame.can_id, frame.can_dlc);
			//for(int i = 0; i < 8; i++)
			//	printf("data[%d] = %d\r\n", i, frame.data[i]);
			can_message_u64 = u64_from_can_msg(frame.data);
			t=time(NULL);
			// Unpack CAN message and save to obj. This function is automatically generated using dbcc.
			unpack_message(&obj,frame.can_id,can_message_u64,frame.can_dlc,t);
			//printf("my prints:\n");
			//print_message(&obj, frame.can_id, stdout);
			//printf("my timestamp: %u\n", t);

			// Decode CAN message and write to file, using decode functions generated automatically using dbcc.
			if(t>t_before)
			{
				t_before=t;
				// Maybe better if concat strings and print all at once, but it would be more complicated, I tried, see commit a4dcf5ba5a
				fprintf(fp,"%u",t);
				decode_can_0x630_BMS_M1_Cell_1_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x630_BMS_M1_Cell_2_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x630_BMS_M1_Cell_3_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x630_BMS_M1_Cell_4_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x631_BMS_M1_Cell_5_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x631_BMS_M1_Cell_6_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x631_BMS_M1_Cell_7_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x631_BMS_M1_Cell_8_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x632_BMS_M1_Cell_9_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x632_BMS_M1_Cell_10_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x632_BMS_M1_Cell_11_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x632_BMS_M1_Cell_12_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x633_BMS_M2_Cell_1_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x633_BMS_M2_Cell_2_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x633_BMS_M2_Cell_3_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x633_BMS_M2_Cell_4_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x634_BMS_M2_Cell_5_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x634_BMS_M2_Cell_6_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x634_BMS_M2_Cell_7_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x634_BMS_M2_Cell_8_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x635_BMS_M2_Cell_9_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x635_BMS_M2_Cell_10_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x635_BMS_M2_Cell_11_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x635_BMS_M2_Cell_12_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x636_BMS_M3_Cell_1_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x636_BMS_M3_Cell_2_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x636_BMS_M3_Cell_3_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x636_BMS_M3_Cell_4_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x637_BMS_M3_Cell_5_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x637_BMS_M3_Cell_6_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x637_BMS_M3_Cell_7_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x637_BMS_M3_Cell_8_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x638_BMS_M3_Cell_9_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x638_BMS_M3_Cell_10_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x638_BMS_M3_Cell_11_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x638_BMS_M3_Cell_12_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x639_BMS_M4_Cell_1_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x639_BMS_M4_Cell_2_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x639_BMS_M4_Cell_3_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x639_BMS_M4_Cell_4_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63a_BMS_M4_Cell_5_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63a_BMS_M4_Cell_6_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63a_BMS_M4_Cell_7_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63a_BMS_M4_Cell_8_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63b_BMS_M4_Cell_9_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63b_BMS_M4_Cell_10_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63b_BMS_M4_Cell_11_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63b_BMS_M4_Cell_12_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63c_BMS_M5_Cell_1_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63c_BMS_M5_Cell_2_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63c_BMS_M5_Cell_3_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63c_BMS_M5_Cell_4_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63d_BMS_M5_Cell_5_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63d_BMS_M5_Cell_6_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63d_BMS_M5_Cell_7_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63d_BMS_M5_Cell_8_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63e_BMS_M5_Cell_9_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63e_BMS_M5_Cell_10_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63e_BMS_M5_Cell_11_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63e_BMS_M5_Cell_12_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63f_BMS_M6_Cell_1_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63f_BMS_M6_Cell_2_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63f_BMS_M6_Cell_3_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x63f_BMS_M6_Cell_4_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x640_BMS_M6_Cell_5_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x640_BMS_M6_Cell_6_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x640_BMS_M6_Cell_7_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x640_BMS_M6_Cell_8_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x641_BMS_M6_Cell_9_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x641_BMS_M6_Cell_10_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x641_BMS_M6_Cell_11_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x641_BMS_M6_Cell_12_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x642_BMS_M7_Cell_1_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x642_BMS_M7_Cell_2_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x642_BMS_M7_Cell_3_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x642_BMS_M7_Cell_4_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x643_BMS_M7_Cell_5_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x643_BMS_M7_Cell_6_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x643_BMS_M7_Cell_7_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x643_BMS_M7_Cell_8_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x644_BMS_M7_Cell_9_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x644_BMS_M7_Cell_10_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x644_BMS_M7_Cell_11_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x644_BMS_M7_Cell_12_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x645_BMS_M8_Cell_1_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x645_BMS_M8_Cell_2_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x645_BMS_M8_Cell_3_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x645_BMS_M8_Cell_4_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x646_BMS_M8_Cell_5_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x646_BMS_M8_Cell_6_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x646_BMS_M8_Cell_7_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x646_BMS_M8_Cell_8_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x647_BMS_M8_Cell_9_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x647_BMS_M8_Cell_10_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x647_BMS_M8_Cell_11_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x647_BMS_M8_Cell_12_Voltage(&obj,&out);
				fprintf(fp,",%6.4f",out);
				decode_can_0x680_BMS_M1_Cell_1_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x680_BMS_M1_Cell_2_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x680_BMS_M1_Cell_3_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x680_BMS_M1_Cell_4_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x680_BMS_M1_Cell_5_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x680_BMS_M1_Cell_6_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x680_BMS_M1_Cell_7_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x680_BMS_M1_Cell_8_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x681_BMS_M1_Cell_9_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x681_BMS_M1_Cell_10_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x681_BMS_M1_Cell_11_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x681_BMS_M1_Cell_12_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x682_BMS_M2_Cell_1_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x682_BMS_M2_Cell_2_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x682_BMS_M2_Cell_3_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x682_BMS_M2_Cell_4_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x682_BMS_M2_Cell_5_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x682_BMS_M2_Cell_6_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x682_BMS_M2_Cell_7_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x682_BMS_M2_Cell_8_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x683_BMS_M2_Cell_9_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x683_BMS_M2_Cell_10_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x683_BMS_M2_Cell_11_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x683_BMS_M2_Cell_12_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x684_BMS_M3_Cell_1_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x684_BMS_M3_Cell_2_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x684_BMS_M3_Cell_3_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x684_BMS_M3_Cell_4_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x684_BMS_M3_Cell_5_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x684_BMS_M3_Cell_6_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x684_BMS_M3_Cell_7_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x684_BMS_M3_Cell_8_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x685_BMS_M3_Cell_9_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x685_BMS_M3_Cell_10_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x685_BMS_M3_Cell_11_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x685_BMS_M3_Cell_12_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x686_BMS_M4_Cell_1_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x686_BMS_M4_Cell_2_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x686_BMS_M4_Cell_3_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x686_BMS_M4_Cell_4_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x686_BMS_M4_Cell_5_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x686_BMS_M4_Cell_6_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x686_BMS_M4_Cell_7_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x686_BMS_M4_Cell_8_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x687_BMS_M4_Cell_9_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x687_BMS_M4_Cell_10_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x687_BMS_M4_Cell_11_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x687_BMS_M4_Cell_12_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x688_BMS_M5_Cell_1_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x688_BMS_M5_Cell_2_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x688_BMS_M5_Cell_3_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x688_BMS_M5_Cell_4_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x688_BMS_M5_Cell_5_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x688_BMS_M5_Cell_6_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x688_BMS_M5_Cell_7_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x688_BMS_M5_Cell_8_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x689_BMS_M5_Cell_9_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x689_BMS_M5_Cell_10_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x689_BMS_M5_Cell_11_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x689_BMS_M5_Cell_12_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68a_BMS_M6_Cell_1_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68a_BMS_M6_Cell_2_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68a_BMS_M6_Cell_3_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68a_BMS_M6_Cell_4_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68a_BMS_M6_Cell_5_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68a_BMS_M6_Cell_6_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68a_BMS_M6_Cell_7_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68a_BMS_M6_Cell_8_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68b_BMS_M6_Cell_9_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68b_BMS_M6_Cell_10_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68b_BMS_M6_Cell_11_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68b_BMS_M6_Cell_12_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68c_BMS_M7_Cell_1_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68c_BMS_M7_Cell_2_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68c_BMS_M7_Cell_3_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68c_BMS_M7_Cell_4_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68c_BMS_M7_Cell_5_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68c_BMS_M7_Cell_6_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68c_BMS_M7_Cell_7_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68c_BMS_M7_Cell_8_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68d_BMS_M7_Cell_9_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68d_BMS_M7_Cell_10_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68d_BMS_M7_Cell_11_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68d_BMS_M7_Cell_12_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68e_BMS_M8_Cell_1_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68e_BMS_M8_Cell_2_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68e_BMS_M8_Cell_3_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68e_BMS_M8_Cell_4_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68e_BMS_M8_Cell_5_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68e_BMS_M8_Cell_6_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68e_BMS_M8_Cell_7_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68e_BMS_M8_Cell_8_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68f_BMS_M8_Cell_9_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68f_BMS_M8_Cell_10_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68f_BMS_M8_Cell_11_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu",iout);
				decode_can_0x68f_BMS_M8_Cell_12_Thermistor(&obj,&iout);
				fprintf(fp,",%3hhu\n",iout);
			}

			fclose(fp);
		}
	}

	//6.Close the socket and can0
	close(s);
	//system("sudo ifconfig can0 down");

	free(filename);

	return 0;
}