This repository has been archived by the owner on Oct 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
CiA301CommPort.cpp
907 lines (688 loc) · 22.1 KB
/
CiA301CommPort.cpp
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
#include "CiA301CommPort.h"
CiA301CommPort::CiA301CommPort(int newPortFileDescriptor, uint8_t new_id)
{
portFileDescriptor = newPortFileDescriptor;
usesockets=0;
id=new_id;
}
CiA301CommPort::CiA301CommPort(PortBase* new_port, uint8_t new_id)
{
port=new_port;
usesockets=1;
id=new_id;
//set the filter to port so commport will only accept messages to his id
//must decide to make not constant. Till then, moved up to main
port->SetFilter(id,0x07F);
//port->SetFilter(0x000,0x7FF);
}
///
/// \brief CiA301CommPort::ReadSDO Waits until expected
/// \param can SDO address
/// \return
///
ulong CiA301CommPort::ReadSDO(const vector<uint8_t> &address)
{
co_msg output;
//cout << "id" << id << endl;
//Ask an sdo read from address
vector<uint8_t> data={};
// data.insert(data.end(), address.begin(), address.end());
// SendMessage(SetCanOpenMsg(sdo::rx0+id, 0 ,data) );
//Reading is got from an empty write response
ulong ret=(ulong)WriteSDO(address, data);
// cout << "ReadSDO ret: " << ret << endl;
return ret;
// //Wait for the answer
// output = SetCanOpenMsg(sdo::tx0+id, 0 ,address);
// ReadCobId(sdo::tx0+id,output);
//Get the data from output
//Use the data from output
//TODO: control response with read table (table 8 man 301)
// if (output.data_co[0]!=0x60)
// {
// err(1,"Can not receive answer from node");
// }
//fixed return four last bytes from data.
// long retvalue = output.data_co[7];
// retvalue = (retvalue << 8) + output.data_co[6];
// retvalue = (retvalue << 8) + output.data_co[5];
// retvalue = (retvalue << 8) + output.data_co[4];
// return output.data_co[4]+(16^2)*output.data_co[5]+
// (16^4)*output.data_co[6]+(16^6)*output.data_co[7];
// return retvalue;
}
long CiA301CommPort::WriteSDO(const vector<uint8_t> &address, const vector<uint8_t> &value )
{
//Set the first byte of data frame
//it holds the size descriptor for the data bytes
//like in CiA 301
vector<uint8_t> data(1);
switch(value.size()){
case 0 :
data[0]=0x40;
break;
case 1 :
data[0]=0x2F;
break;
case 2 :
data[0]=0x2B;
break;
default:
data[0]=0x23;
break;
}
//Insert SDO Address
data.insert(data.end(), address.begin(), address.end());
//Insert SDO data field
data.insert(data.end(), value.begin(), value.end());
//cout << "id" << id << endl;
//Write the data in the default rx port
//like in CiA 301
int size=value.size();
SendMessage( SetCanOpenMsg(sdo::rx0+id, 0 ,data) );
// //and wait for write response
// co_msg output;
//every write operation returns an empty answer if correct
//wait the answer in TPDO1 (tx0(580)+id)
co_msg output;
ReadCobId(sdo::tx0+id, output);
//alternative: use PDO, but no check is available.
// if (ReadPDO(0) != 0) return -1;
for (long reps=0 ; reps<FIND_RETRY ;reps++)
{
//cout << output.data_co[1]<< " " << address[0]<< " " << output.data_co[2]<< " " << address[1] <<endl;
//Check that received data comes from right address.
if ( (output.data_co[1] == address[0]) && (output.data_co[2] == address[1]) )
{
//cout << "break";
break;
}
else //Wrong address.
{
//TODO: Return message to bus??
//SendMessage(output);
//Read again and check (later)
ReadCobId(sdo::tx0+id,output);
}
}
//fixed return four last bytes from data.
// cout<<"OUTPUT DATA 0 "<<(int)output.data_co[0]<<endl;
// cout<<"OUTPUT DATA 1 "<<(int)output.data_co[1]<<endl;
// cout<<"OUTPUT DATA 2 "<<(int)output.data_co[2]<<endl;
// cout<<"OUTPUT DATA 3 "<<(int)output.data_co[3]<<endl;
// cout<<"OUTPUT DATA 4 "<<(bitset<8>)output.data_co[4]<<endl;
// cout<<"OUTPUT DATA 5 "<<(bitset<8>)output.data_co[5]<<endl;
// cout<<"OUTPUT DATA 6 "<<(bitset<8>)output.data_co[6]<<endl;
// cout<<"OUTPUT DATA 7 "<<(bitset<8>)output.data_co[7]<<endl;
// long retvalue = output.data_co[7];
// retvalue = (retvalue << 8) + output.data_co[6];
// retvalue = (retvalue << 8) + output.data_co[5];
// retvalue = (retvalue << 8) + output.data_co[4];
// return retvalue;
// cout << "dlc " << output.dlc_co << endl;
// long retvalue=DataToInt(&output.data_co[4], output.dlc_co);
long ret=DataToInt(&output.data_co[4], output.dlc_co-4);
//cout << "WriteSDO ret: " << ret << endl;
return ret;
}
///
/// \brief CiA301CommPort::ReadPDO
/// \param number: Use numbers between 0(TPDO1) and 3 (TPDO4).
/// \return Data field response converted to long.
///
long CiA301CommPort::ReadPDO(long number)
{
co_msg output;
ReadCobId((number*0x100)+pdo::tx0+id,output);
//TODO: check response
// for (long reps=0 ; reps>FIND_RETRY ;reps++)
// {
// //Check that received data comes from right address.
// if ( (output.data_co[1] == address[0]) && (output.data_co[2] == address[1]) )
// {
// continue;
// }
// else //Wrong address.
// {
// cout << output.data_co[1]<< " " << address[0]<< " " << output.data_co[2]<< " " << address[1] <<endl;
// //TODO: Return message to bus??
// //SendMessage(output);
// //Read again and check (later)
// ReadCobId(sdo::tx0+id,output);
// }
// }
//Get the data from output
//Use the data from output
//TODO: control response with read table (table 8 man 301)
// if (output.data_co[0]!=0x60)
// {
// err(1,"Can not receive answer from node");
// }
return DataToInt(output.data_co,output.dlc_co);
}
long CiA301CommPort::DisablePDOs()
{
//Can Open iPos manual page 194
//Send the following message:
//(SDO write access to object 1A00 h sub-index 0 the 8-bit value 00 h ):
vector<uint8_t> data={0x00};
cout << WriteSDO(pdo::tx0enable ,data);
//Can Open iPos manual page 268
//Send the following message:
//(SDO write access to object 1A01 h sub-index 0 the 8-bit value 00 h ):
cout << WriteSDO(pdo::tx1enable ,data);
cout << WriteSDO(pdo::tx2enable ,data);
cout << WriteSDO(pdo::tx3enable ,data);
return 0;
}
long CiA301CommPort::EnablePDOs()
{
//Can Open iPos manual page 194
//Send the following message:
//(SDO write access to object 1A00 h sub-index 0 the 8-bit value 00 h ):
vector<uint8_t> data={0x10, 0x00, 0x41, 0x60};
cout << WriteSDO(pdo::tx0sub1 ,data);
data={0x02};
cout << WriteSDO(pdo::tx0enable ,data);
//Can Open iPos manual page 268
//Send the following message:
//(SDO write access to object 1A01 h sub-index 0 the 8-bit value 00 h ):
cout << WriteSDO(pdo::tx1enable ,data);
cout << WriteSDO(pdo::tx2enable ,data);
cout << WriteSDO(pdo::tx3enable ,data);
return 0;
}
long CiA301CommPort::WritePDO( long number,const vector<uint8_t> &command)
{
//co_msg output;
//cout << "id" << id << endl;
//Ask an sdo read from address
SendMessage(SetCanOpenMsg(pdo::rx0+id, 0 ,command) );
//Wait for the answer
//output = SetCanOpenMsg(sdo::tx0+id, 0 ,address);
//Get the data from output
//ReadCobId(pdo::tx0+id,output);
return 0;
}
long CiA301CommPort::WritePDO1(const vector<uint8_t> &command)
{
//co_msg output;
//cout << "id" << id << endl;
//Ask an sdo read from address
SendMessage(SetCanOpenMsg(pdo::rx0+id, 0 ,command) );
//Wait for the answer
//output = SetCanOpenMsg(sdo::tx0+id, 0 ,address);
//Get the data from output
//ReadCobId(pdo::tx0+id,output);
return 0;
}
long CiA301CommPort::WritePDO4(const vector<uint8_t> &command)
{
//co_msg output;
//cout << "id" << id << endl;
//Ask an sdo read from address
SendMessage(SetCanOpenMsg(pdo::rx3+id, 0 ,command) );
//Wait for the answer
//output = SetCanOpenMsg(sdo::tx0+id, 0 ,address);
//Get the data from output
//ReadCobId(pdo::tx0+id,output);
return 0;
}
long CiA301CommPort::FlushBuffer()
{
// cout << "Flushing can buffer" << endl;
co_msg m1;
port->FlushMsg();
//maybe flush nmt will be needed sometime
// if (usesockets)
// {
// cout << "Cant Flush can buffer. Using blocking read!!!!" << endl;
// return 1;
//// for (int i=0; i<5; i++)
//// {
//// uint32_t id; uint8_t data[8];int size;
//// port->GetMsg(id,data,size);
//// }
// }
// while (WaitForReadMessage(m1,0)==0)
// {
// }
return 0;
}
long CiA301CommPort::FlushBuffer(int msgs)
{
for (int i=0; i<msgs; i++)
{
GetMsg(input);
cout << "cleared : " << hex << input.id << dec << endl;
}
return 0;
}
long CiA301CommPort::WriteNMT(const vector<uint8_t> &nmtCommand)
{
co_msg output;
long response;
vector<uint8_t> data(nmtCommand);
data.push_back(id);
SendMessage(SetCanOpenMsg(0, 0 ,data) );
return 0;
}
long CiA301CommPort::ReadNMT(const vector<uint8_t> & nmtCode)
{
can_frame output;
vector<uint8_t> nmtBoot(nmtCode);
nmtBoot.push_back(id);
long code=DataToInt(nmtBoot.data(),nmtBoot.size());
cout<<" --> ReadNMT. Expecting: " << std::hex << code << std::dec << endl;
for(long reps=0; reps<FIND_RETRY; reps++)
{
port->GetNMT(output.data, output.can_dlc);
//cout << " Received: " << std::hex << input.id << std::dec << endl;
//First check for the input.
//If the response is the answer expected, continue
if ( code == DataToInt(output.data,output.can_dlc) )
{
//cout << " comp: " << (input.id == expected_cobid) << endl;
break;
}
else
{
cout << " ReadNMT still not received. Received: " << std::hex << DataToInt(output.data,output.can_dlc) << std::dec << endl;
}
if (reps==FIND_RETRY-1)
{
//fprintf(stderr, "Max retry reached: %u", reps);
cerr << " ReadNMT Max retry reached: " << reps << endl;
cout << " ReadNMT Max retry reached: " << reps << endl;
return -1;
}
}
//NMT
cout<<" <-- ReadNMT. Received: " << std::hex << code << std::dec << endl;
cout << "ID: " << std::hex<< (int)output.data[output.can_dlc-1] << std::dec<< endl;
return 0;
}
long CiA301CommPort::ReadErrorNMT()
{
co_msg output;
//NMT
return ReadCobId(0x700+id,output);
}
long CiA301CommPort::CanOpenToCanBus(const co_msg & input, can_msg & output)
{
output.id=input.id_co;
//output.fi=input.id_co;
//output.fi<<=4;
//output.fi+=input.dlc_co;
output.dlc=input.dlc_co;
output.rtr=input.rtr;
output.ff=FF_NORMAL; //normal frame
output.ts=input.ts;
// creo que en vez de 8 poner dlc
for( int i=0; i < 8; i++)
{
output.data[i] = input.data_co[i];
}
return 0;
}
/* Función para convertir de can a canopen */
long CiA301CommPort::CanBusToCanOpen(const can_msg & input, co_msg & output)
{
//verificar el bit de start to frame de can
// cout<<"input.dlc"<<input.dlc<<endl;
output.dlc_co=input.dlc;
output.id_co=input.id;
output.rtr=input.rtr;
output.ts=input.ts;
//
for( int i=0; i < 8; i++)
{
output.data_co[i] = input.data[i];
}
return 0;
}
co_msg CiA301CommPort::SetCanOpenMsg(unsigned short id_co, unsigned short rtr, vector<uint8_t> coDataFrame){
co_msg msg_co;
msg_co.id_co=id_co;
msg_co.dlc_co=coDataFrame.size();
memcpy(msg_co.data_co, coDataFrame.data(), (msg_co.dlc_co)*sizeof(uint8_t));
//msg_co.nodeID=nodeID;
msg_co.rtr=rtr;
//msg_co.fun_code=fund_code;
// cout<<"cob id: " << std::hex << msg_co.id_co << std::dec << " rtr: " << msg_co.rtr << endl;
// cout<<"data: ";
// for (int i = 0; i < msg_co.dlc_co; i++) {
// printf("%02x ",msg_co.data_co[i]);
// }
// cout<<endl;
return msg_co;
}
/* Transforma mensaje de canopen a can y lo envía al puerto */
int CiA301CommPort::SendMessage(co_msg input)
{
// cout<<"SendMessage " << endl;
// cout<<"sent cob id " << std::hex << input.id_co << std::dec <<" rtr: " << input.rtr << endl;
// cout<<"sent data: ";
// for (int i = 0; i < input.dlc_co; i++)
// {
// printf("%02x ",input.data_co[i]);
// }
// cout<<endl;
if (CanOpenToCanBus(input,send_msg) < 0)
{
cout<<"Error al transformar el mensaje"<<endl;
}
else
{
//print can frame information
// cout<<"sent can id " << (bitset<16>)send_msg.id << " rtr: " << send_msg.rtr << endl;
// cout<<"sent data: ";
// for (int i = 0; i < send_msg.dlc; i++)
// {
// printf("%02x ",send_msg.data[i]);
// }
// cout<<endl;
/* Write the message to the Port */
if(usesockets)
{
uint32_t tmpid = send_msg.id;
uint16_t tmpdlc = send_msg.dlc;
port->PutMsg(tmpid, send_msg.data, tmpdlc);
}
else
{
if(write(portFileDescriptor,&send_msg,sizeof(struct can_msg))!=sizeof(struct can_msg))
{
err(1, "Failed to send message");
}
}
}
return 0;
}
int CiA301CommPort::SendCanMessage(can_msg &input)
{
//cout<<"SendCanMessage " << endl;
//print can frame information
//cout<<"sent can id " << (bitset<16>)send_msg.id << " rtr: " << send_msg.rtr << endl;
// cout<<"sent data: ";
// for (int i = 0; i < send_msg.dlc; i++)
// {
// printf("%02x ",send_msg.data[i]);
// }
// cout<<endl;
/* Write the message to the Port */
if(usesockets)
{
uint32_t tmpid = send_msg.id;
port->PutMsg(tmpid, send_msg.data, send_msg.dlc);
}
else
{
if(write(portFileDescriptor,&send_msg,sizeof(struct can_msg))!=sizeof(struct can_msg))
{
err(1, "Failed to send message");
}
}
return 0;
}
int CiA301CommPort::WaitForReadMessage(co_msg & output, unsigned int canIndex){
can_msg input;
// cout<<"WaitForReadMessage -----------" << endl;
#if USE_TIMEOUT
if(read_timeout(portFileDescriptor,&input,USE_TIMEOUT)==0)
{
//err(1,"Could not read the message.");
return -1;
}
#else //(NOT)USE_TIMEOUT
if( read(portFileDescriptor,&input,sizeof(struct can_msg) !=sizeof(input)) )
{
err(1,"read");
}
#endif //USE_TIMEOUT
//convert received data to canopen
if(CanBusToCanOpen(input, output)!=0)
{
err(1,"error al convertir el mensaje");
return -1;
}
else
{
// //print can frame information
// cout<<"received can id " << (bitset<16>)input.id << " rtr: " << input.rtr << endl;
// cout<<"received data: ";
// for (int i = 0; i < input.dlc; i++)
// {
// printf("%02x ",input.data[i]);
// }
// cout<<endl;
// cout<<"received cob id " << std::hex << output.id_co << std::dec << " rtr: " << output.rtr << endl;
// cout<<"received canopen data: ";
// for (int i = 0; i < output.dlc_co; i++)
// {
// printf("%02x ",output.data_co[i]);
// }
// cout<<endl;
// cout<<"WaitForReadMessage End-----------" << endl;
}
return 0;
}
///
/// \brief CiA301CommPort::WaitForAnswer Read the port until expected canopen answer shows
/// up in the port reads.
/// \param output Expected canopen answer.
/// \param canIndex: Index of the caller.
/// \return
///
int CiA301CommPort::ReadCobId(uint16_t expected_cobid, co_msg & output )
{
vector<can_msg> otherMsgs(0);
//cout << " Canid Received: " << std::hex << input.id << std::dec << endl;
// cout<<" --> ReadCobId. Expecting: " << std::hex << expected_cobid << std::dec << endl;
for(long reps=0 ; reps<FIND_RETRY ;reps++)
{
GetMsg(input);
// cout << "Expecting :"<< std::hex << expected_cobid << " Received: " << std::hex << input.id << std::dec << endl;
//First check for the input.
//If the response is the answer expected, continue
if (input.id == expected_cobid)
{
// cout << " comp: " << (input.id == expected_cobid) << endl;
// FlushBuffer();
break;
}
//if not cobid but node id:
if (GET_NODE_ID(input.id) == GET_NODE_ID(expected_cobid) )
{
// cout << " Cobid still not received. Received: " << std::hex << input.id << std::dec << endl;
//if id, check if error
//return -1;
}
else
{
// cout << " Cobid still not received. Received: " << std::hex << input.id << std::dec << endl;
otherMsgs.push_back(input);
}
if (reps==FIND_RETRY-1)
{
cerr<<" ReadCobId Max retry reached " << endl;
cout<<" ReadCobId Max retry reached " << endl;
return -1;
}
}
//GetMsg(input);
// for (int i=0; i<otherMsgs.size(); i++)
// {
// cout << "resend number : " << i << " with bytes: " << otherMsgs[i].dlc; ;
// cout<<" message with cob id: " << std::hex << otherMsgs[i].id << std::dec <<" rtr: " << otherMsgs[i].rtr << endl;
// for (int j = 0; j < otherMsgs[i].dlc; j++)
// {
// printf("%02x ",otherMsgs[i].data[j]);
// }
//TODO: find another way for this without sending again
//SendCanMessage(otherMsgs[i]);
// }
//convert received data to canopen
if(CanBusToCanOpen(input, output)!=0)
{
err(1,"error al convertir el mensaje");
return -1;
}
else
{
// cout<<" <-- ReadCobId. Received: " << std::hex << output.id_co << std::dec << endl;
// cout << "ID: " << std::hex<< GET_NODE_ID(output.id_co) << std::dec<< endl;
//cout << " rtr: " << output.rtr << endl;
// cout<<"received canopen data: ";
// for (int i = 0; i < output.dlc_co; i++)
// {
// printf("%02x ",output.data_co[i]);
// }
// cout<<endl;
//TODO: use dlc here!!!
return data4x8to32(&output.data_co[4],output.dlc_co);
}
return 0;
}
int CiA301CommPort::read_timeout(int fd, struct can_msg *buf, unsigned int timeout)
{
struct timeval tv;
int sec,ret;
sec=timeout/1000;
tv.tv_sec=sec;
tv.tv_usec=(timeout-(sec*1000))*1000;
//use select to check readyness
fd_set fds; // file descriptor set
FD_ZERO(&fds);// clear the set
FD_SET(fd,&fds);// put the fd in the set
assert(FD_ISSET(fd,&fds));
ret=select(fd+1,&fds,0,0,&tv);
//cout<< "Number of ready ports: " << ret <<endl;
if(ret==0)/* timeout */
{
//err(1,"Port connection timed out");
return 0;
}
else if (ret<0)
{
err(ret,"Numbered Error. See exit code");
return ret;
}
else
{
//TODO *** Check if pread works with canbus ports at 0 offset
return pread(fd,buf,sizeof(struct can_msg),0);
}
}
long CiA301CommPort::GetMsg(can_msg &msg)
{
if (usesockets)
{
uint32_t tmpid;
uint8_t size;
//doing it trough port object
port->GetMsg(tmpid,msg.data,size);
msg.dlc=size;
msg.id= tmpid;
return 0;
}
#if USE_TIMEOUT
timeval tv;
//int readyMsg;
//can_msg msg;
//As USE_TIMEOUT are miliseconds, get seconds, then miliseconds
tv.tv_sec=(int)USE_TIMEOUT/1000;
tv.tv_usec=USE_TIMEOUT-(tv.tv_sec*1000);
//use select to check readyness
fd_set fds; // file descriptor set
FD_ZERO(&fds);// clear the set
FD_SET(portFileDescriptor,&fds);// put the fd in the set
assert(FD_ISSET(portFileDescriptor,&fds));
int readyMsg=select(portFileDescriptor+1,&fds,0,0,&tv);
//cout<< "Number of ready ports: " << ret <<endl;
if(readyMsg==0)/* timeout */
{
//err(1,"Port connection timed out");
//cout<< "Bus empty. Messages in buffer: " << readyMsg <<endl;
return 1;
}
else if (readyMsg<0)
{
err(readyMsg,"Numbered Error. See exit code");
return readyMsg;
}
else
{
if(pread(portFileDescriptor,&msg,sizeof(struct can_msg), 0) != sizeof(struct can_msg))
{
//err(1,"Could not read the message.");
cout << "Could not read the message." << endl;
return -1;
}
//TODO *** Check if pread works with canbus ports at 0 offset
//CanBusToCanOpen( msg , msg );
return 0;
}
#else //(NOT)USE_TIMEOUT
if( pread(portFileDescriptor,&buf,sizeof(struct can_msg), 0) != sizeof(struct can_msg) )
{
err(1,"read");
}
else
{
//CanBusToCanOpen( buf , msg );
return 0;
}
#endif //USE_TIMEOUT
}
uint32_t CiA301CommPort::data4x8to32(const uint8_t* in,int dlc)
{
//TODO check for Sizes!!!!!
//TODO: use dlc here!!!
uint32_t retvalue;
const vector<uint8_t> data_out={};
for (int i=0;i<dlc;i++){
//memcpy((uint8_t *)&(data_out+i),(uint8_t *)in+i,1);
if(dlc>0){
retvalue = in[i];
retvalue = (retvalue << 8);
}
else{
retvalue = data_out[i];
}
// uint32_t retvalue;
// for(int i=0;i<dlc;i++){
// if(dlc>0){
// retvalue = data_out[i];
// retvalue = (retvalue << 8);
// }
// else{
// retvalue = data_out[i];
// }
}
return retvalue;
//return data_out;
}
ulong CiA301CommPort::DataToInt(const uint8_t* in, unsigned short size)
{
// cout<<"SIZE "<<size<<endl;
if ( (size<=0) | (size>4) )
{
return 0;
}
long retvalue = in[size-1];
// cout<<"--- RETVALUE "<<retvalue<<endl;
for (int i=size-2; i>=0; i--)
{
retvalue = (retvalue << 8) + in[i];
// cout<<"----RETVALUE "<<retvalue<<endl;
//cout <<"i" << i <<"v"<< retvalue <<endl;
}
// if (in[3]==0xFF && in[2]==0xFF)
// {
// // cout<<"in[3]==0xFF"<<in[3]<<endl;
// // cout<<"in[2]==0xFF"<<in[2]<<endl;
// return -(4294967296-retvalue);
// }
return retvalue;
}