forked from mikebailey61/J1939_stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJ1939_stack_test.c
857 lines (705 loc) · 27.2 KB
/
J1939_stack_test.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
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
/////////////////////////////////////////////////////////////////////////////////////////
// File: J1939_stack_test.c
// Module: TEST only
// Description: Test the J1939 stack
// Originator: MB
// Derived from: Vector Gmbh xlCANdemo.c
// Date created: 11 June 2014
//---------------------------------------------------------------------------------------
// Revision Log:
// $Log: $
//
//---------------------------------------------------------------------------------------
// Notes:
//
//---------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////
// Copyright (C)2014 by Timespace Technology Ltd. All rights reserved.
//
// This software is confidential. It is not to be copied or distributed in any form
// to any third party.
//
// This software is provided by Timespace Technology on an 'as is' basis, and Timespace
// Technology expressly disclaims any and all warranties, expressed or implied including,
// without limitation, warranties of merchantability and fitness for a particular
// purpose. In no event shall Timespace Technology be liable for any direct, indirect,
// incidental, punitive or consequential damages - of any kind whatsoever - with respect
// to the software.
/////////////////////////////////////////////////////////////////////////////////////////
/*------------------------------------------------------------------------------
| File:
| xlCANdemo.C
| Project:
| Sample for XL - Driver Library
| Example application using 'vxlapi.dll'
|-------------------------------------------------------------------------------
| $Author: vismwf $ $Locker: $ $Revision: 13365 $
| $Header: /VCANDRV/XLAPI/samples/xlCANdemo/xlCANdemo.c 17 20.11.07 14:43 Jörg $
|-------------------------------------------------------------------------------
| Copyright (c) 2004 by Vector Informatik GmbH. All rights reserved.
-----------------------------------------------------------------------------*/
#if defined(_Windows) || defined(_MSC_VER) || defined (__GNUC__)
#define STRICT
#include <windows.h>
#endif
#include <stdio.h>
#define UNUSED_PARAM(a) { a=a; }
#define RECEIVE_EVENT_SIZE 1 // DO NOT EDIT! Currently 1 is supported only
#define RX_QUEUE_SIZE 4096 // internal driver queue size in CAN events
#include "vxlapi.h"
#include "J1939_includes.h"
#include "FMS.h"
/////////////////////////////////////////////////////////////////////////////
// globals
char g_AppName[XL_MAX_LENGTH+1] = "J1939_stack_test"; // Application name which is displayed in VHWconf
XLportHandle g_xlPortHandle = XL_INVALID_PORTHANDLE; // Global porthandle (we use only one!)
XLdriverConfig g_xlDrvConfig; // Contains the actual hardware configuration
XLaccess g_xlChannelMask = 0; // Global channelmask (includes all founded channels)
XLaccess g_xlPermissionMask = 0; // Global permissionmask (includes all founded channels)
unsigned int g_BaudRate = 500000; // Default baudrate
int g_silent = 0; // flag to visualize the message events (on/off)
unsigned int g_TimerRate = 0; // Global timerrate (to toggel)
XLaccess g_xlChanMaskTx = 0; // Global transmit channel mask
/////////////////////////////////////////////////////////////////////////////
// thread variables
XLhandle g_hMsgEvent; // notification handle for the receive queue
HANDLE g_hRXThread; // thread handle (RX)
HANDLE g_hTXThread; // thread handle (TX)
int g_RXThreadRun; // flag to start/stop the RX thread
int g_TXThreadRun; // flag to start/stop the TX thread (for the transmission burst)
////////////////////////////////////////////////////////////////////////////
// functions (Threads)
DWORD WINAPI RxThread( PVOID par );
DWORD WINAPI TxThread( LPVOID par );
////////////////////////////////////////////////////////////////////////////
// functions (prototypes)
void demoHelp(void);
void demoPrintConfig(void);
XLstatus demoCreateRxThread(void);
////////////////////////////////////////////////////////////////////////////
// demoHelp()
// shows the program functionality
//
////////////////////////////////////////////////////////////////////////////
void demoHelp(void)
{
printf("\n");
printf("----------------------------------------------------------\n");
printf("- J1939_test_stack - HELP -\n");
printf("----------------------------------------------------------\n");
printf("- Keyboard commands: -\n");
printf("- 't' Transmit a message -\n");
printf("- 'b' Transmit a message burst (toggle) -\n");
printf("- 'm' Transmit a remote message -\n");
printf("- 'g' Request chipstate -\n");
printf("- 's' Start/Stop -\n");
printf("- 'r' Reset clock -\n");
printf("- '+' Select channel (up) -\n");
printf("- '-' Select channel (down) -\n");
printf("- 'i' Select transmit Id (up) -\n");
printf("- 'I' Select transmit Id (down) -\n");
printf("- 'x' Toggle extended/standard Id -\n");
printf("- 'o' Toggle output mode -\n");
printf("- 'a' Toggle timer -\n");
printf("- 'v' Toggle logging to screen -\n");
printf("- 'p' Show hardware configuration -\n");
printf("- 'h' Help -\n");
printf("- 'ESC' Exit -\n");
printf("----------------------------------------------------------\n");
printf("- 'PH'->PortHandle; 'CM'->ChannelMask; 'PM'->Permission -\n");
printf("----------------------------------------------------------\n");
printf("\n");
}
////////////////////////////////////////////////////////////////////////////
// demoPrintConfig()
// shows the actual hardware configuration
//
////////////////////////////////////////////////////////////////////////////
void demoPrintConfig(void)
{
unsigned int i;
char str[XL_MAX_LENGTH + 1]="";
printf("----------------------------------------------------------\n");
printf("- %02d channels Hardware Configuration -\n", g_xlDrvConfig.channelCount);
printf("----------------------------------------------------------\n");
for (i=0; i < g_xlDrvConfig.channelCount; i++)
{
printf("- Ch:%02d, CM:0x%03I64x,",
g_xlDrvConfig.channel[i].channelIndex, g_xlDrvConfig.channel[i].channelMask);
strncpy( str, g_xlDrvConfig.channel[i].name, 23);
printf(" %23s,", str);
memset(str, 0, sizeof(str));
if (g_xlDrvConfig.channel[i].transceiverType != XL_TRANSCEIVER_TYPE_NONE)
{
strncpy( str, g_xlDrvConfig.channel[i].transceiverName, 13);
printf("%13s -\n", str);
}
else printf(" no Cab! -\n", str);
}
printf("----------------------------------------------------------\n\n");
}
////////////////////////////////////////////////////////////////////////////
// demoTransmit
// transmit a CAN message (depending on an ID, channel)
//
////////////////////////////////////////////////////////////////////////////
XLstatus demoTransmit(unsigned int txID, XLaccess xlChanMaskTx)
{
static unsigned char loop;
#if 1
J1939_TX_MESSAGE_T msg;
memset(&msg, 0, sizeof(msg));
msg.byte_count = 8;
msg.PGN = FMS_DD;
msg.priority = 3;
for (unsigned char i = 0; i < 8; i++)
msg.data[i] = i + loop;
loop++;
// set global channel mask for HAL layer
g_xlChanMaskTx = xlChanMaskTx;
Transmit_J1939msg(&msg);
#else
static XLevent xlEvent;
XLstatus xlStatus;
unsigned int messageCount = 1;
memset(&xlEvent, 0, sizeof(xlEvent));
xlEvent.tag = XL_TRANSMIT_MSG;
xlEvent.tagData.msg.id = txID;
xlEvent.tagData.msg.dlc = 8;
xlEvent.tagData.msg.flags = 0;
for (unsigned char i = 0; i < 8; i++)
xlEvent.tagData.msg.data[i] = i + loop;
loop++;
xlStatus = xlCanTransmit(g_xlPortHandle, xlChanMaskTx, &messageCount, &xlEvent);
printf("- Transmit : CM(0x%I64x), %s\n", xlChanMaskTx, xlGetErrorString(xlStatus));
return xlStatus;
#endif
}
////////////////////////////////////////////////////////////////////////////
// demoTransmitBurst
// transmit a message burst (also depending on an IC, channel).
//
////////////////////////////////////////////////////////////////////////////
XLstatus demoTransmitBurst(unsigned int txID)
{
DWORD ThreadId=0;
static unsigned int TXID;
TXID = txID;
if (!g_TXThreadRun)
{
printf("- print txID: %d\n", txID);
g_hTXThread = CreateThread(0, 0x1000, TxThread, (LPVOID)&TXID, 0, &ThreadId);
}
else
{
g_TXThreadRun = 0;
Sleep(10);
WaitForSingleObject(g_hTXThread, 10);
}
return XL_SUCCESS;
}
////////////////////////////////////////////////////////////////////////////
// demoTransmitRemote
// transmit a remote frame
//
////////////////////////////////////////////////////////////////////////////
XLstatus demoTransmitRemote(unsigned int txID, XLaccess xlChanMaskTx)
{
XLstatus xlStatus;
XLevent xlEvent;
unsigned int messageCount = 1;
memset(&xlEvent, 0, sizeof(xlEvent));
xlEvent.tag = XL_TRANSMIT_MSG;
xlEvent.tagData.msg.id = txID;
xlEvent.tagData.msg.flags = XL_CAN_MSG_FLAG_REMOTE_FRAME;
xlEvent.tagData.msg.dlc = 8;
xlStatus = xlCanTransmit(g_xlPortHandle, xlChanMaskTx, &messageCount, &xlEvent);
printf("- Transmit REMOTE : CM(0x%I64x), %s\n", g_xlChannelMask, xlGetErrorString(xlStatus));
return XL_SUCCESS;
}
////////////////////////////////////////////////////////////////////////////
// demoStartStop
// toggle the channel activate/deactivate
//
////////////////////////////////////////////////////////////////////////////
XLstatus demoStartStop(int activated)
{
XLstatus xlStatus;
if (activated)
{
xlStatus = xlActivateChannel(g_xlPortHandle, g_xlChannelMask, XL_BUS_TYPE_CAN, XL_ACTIVATE_RESET_CLOCK);
printf("- ActivateChannel : CM(0x%I64x), %s\n", g_xlChannelMask, xlGetErrorString(xlStatus));
}
else
{
xlStatus = xlDeactivateChannel(g_xlPortHandle, g_xlChannelMask);
printf("- DeactivateChannel: CM(0x%I64x), %s\n", g_xlChannelMask, xlGetErrorString(xlStatus));
}
return XL_SUCCESS;
}
////////////////////////////////////////////////////////////////////////////
// demoSetOutput
// toggle NORMAL/SILENT mode of a CAN channel
//
////////////////////////////////////////////////////////////////////////////
XLstatus demoSetOutput(int outputMode, XLaccess xlChanMaskTx)
{
XLstatus xlStatus;
char *sMode = "NORMAL";
switch (outputMode)
{
case XL_OUTPUT_MODE_NORMAL:
sMode = "NORMAL";
break;
case XL_OUTPUT_MODE_SILENT:
sMode = "SILENT";
break;
case XL_OUTPUT_MODE_TX_OFF:
sMode = "SILENT-TXOFF";
break;
}
// to get an effect we deactivate the channel first.
xlStatus = xlDeactivateChannel(g_xlPortHandle, g_xlChannelMask);
xlStatus = xlCanSetChannelOutput(g_xlPortHandle, xlChanMaskTx, outputMode);
printf("- SetChannelOutput: CM(0x%I64x), %s, %s, %d\n", xlChanMaskTx, sMode, xlGetErrorString(xlStatus), outputMode);
// and activate the channel again.
xlStatus = xlActivateChannel(g_xlPortHandle, g_xlChannelMask, XL_BUS_TYPE_CAN, XL_ACTIVATE_RESET_CLOCK);
return xlStatus;
}
////////////////////////////////////////////////////////////////////////////
// demoCreateRxThread
// set the notification and creates the thread.
//
////////////////////////////////////////////////////////////////////////////
XLstatus demoCreateRxThread(void)
{
XLstatus xlStatus = XL_ERROR;
DWORD ThreadId=0;
if (g_xlPortHandle != XL_INVALID_PORTHANDLE)
{
// Send an event for each Msg!!!
xlStatus = xlSetNotification (g_xlPortHandle, &g_hMsgEvent, 1);
g_hRXThread = CreateThread(0, 0x1000, RxThread, (LPVOID) 0, 0, &ThreadId);
}
return xlStatus;
}
////////////////////////////////////////////////////////////////////////////
// demoInitDriver
// initializes the driver with one port and all founded channels which
// have a connected CAN cab/piggy.
//
////////////////////////////////////////////////////////////////////////////
XLstatus demoInitDriver(XLaccess *pxlChannelMaskTx, unsigned char *pxlChannelIndex)
{
XLstatus xlStatus;
XLaccess xlChannelMaskTx = 0;
unsigned int i;
// ------------------------------------
// open the driver
// ------------------------------------
xlStatus = xlOpenDriver ();
// ------------------------------------
// get/print the hardware configuration
// ------------------------------------
if (XL_SUCCESS == xlStatus)
{
xlStatus = xlGetDriverConfig(&g_xlDrvConfig);
}
if (XL_SUCCESS == xlStatus)
{
demoPrintConfig();
printf("Usage: J1939_stack_test <BaudRate> <ApplicationName> <Identifier>\n\n");
// ------------------------------------
// select the wanted channels
// ------------------------------------
g_xlChannelMask = 0;
for (i=0; i < g_xlDrvConfig.channelCount; i++)
{
// we take all hardware we found and
// check that we have only CAN cabs/piggy's
// at the moment there is no VN8910 XLAPI support!
if (g_xlDrvConfig.channel[i].channelBusCapabilities & XL_BUS_ACTIVE_CAP_CAN)
{
if (!*pxlChannelMaskTx)
{
*pxlChannelMaskTx = g_xlDrvConfig.channel[i].channelMask;
*pxlChannelIndex = g_xlDrvConfig.channel[i].channelIndex;
}
g_xlChannelMask |= g_xlDrvConfig.channel[i].channelMask;
}
}
if (!g_xlChannelMask)
{
printf("ERROR: no available channels found! (e.g. no CANcabs...)\n\n");
xlStatus = XL_ERROR;
}
}
g_xlPermissionMask = g_xlChannelMask;
// ------------------------------------
// open ONE port including all channels
// ------------------------------------
if (XL_SUCCESS == xlStatus)
{
xlStatus = xlOpenPort(&g_xlPortHandle, g_AppName, g_xlChannelMask, &g_xlPermissionMask, RX_QUEUE_SIZE, XL_INTERFACE_VERSION, XL_BUS_TYPE_CAN);
printf("- OpenPort : CM=0x%I64x, PH=0x%02X, PM=0x%I64x, %s\n",
g_xlChannelMask, g_xlPortHandle, g_xlPermissionMask, xlGetErrorString(xlStatus));
}
if ((XL_SUCCESS == xlStatus) && (XL_INVALID_PORTHANDLE != g_xlPortHandle))
{
// ------------------------------------
// if we have permission we set the
// bus parameters (baudrate)
// ------------------------------------
if (g_xlChannelMask == g_xlPermissionMask)
{
xlStatus = xlCanSetChannelBitrate(g_xlPortHandle, g_xlChannelMask, g_BaudRate);
printf("- SetChannelBitrate: baudr.=%u, %s\n",g_BaudRate, xlGetErrorString(xlStatus));
}
else
{
printf("- : we have NO init access!\n");
}
}
else
{
xlClosePort(g_xlPortHandle);
g_xlPortHandle = XL_INVALID_PORTHANDLE;
xlStatus = XL_ERROR;
}
return xlStatus;
}
////////////////////////////////////////////////////////////////////////////
// demoCleanUp()
// close the port and the driver
//
////////////////////////////////////////////////////////////////////////////
static XLstatus demoCleanUp(void)
{
XLstatus xlStatus;
if (g_xlPortHandle != XL_INVALID_PORTHANDLE)
{
xlStatus = xlClosePort(g_xlPortHandle);
printf("- ClosePort : PH(0x%x), %s\n", g_xlPortHandle, xlGetErrorString(xlStatus));
}
g_xlPortHandle = XL_INVALID_PORTHANDLE;
xlCloseDriver();
return XL_SUCCESS; // No error handling
}
////////////////////////////////////////////////////////////////////////////
// main
//
//
////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
XLstatus xlStatus;
XLaccess xlChanMaskTx = 0;
int stop = 0;
int activated = 0;
int c;
unsigned char xlChanIndex = 0;
unsigned int txID = 0x01;
int outputMode = XL_OUTPUT_MODE_NORMAL;
printf("----------------------------------------------------------\n");
printf("- J1939_stack_test - Test Application for J1939 stack -\n");
printf("- using XL Family Driver API -\n");
printf("- Timespace Technology, " __DATE__" -\n");
#ifdef WIN64
printf("- - 64bit Version - -\n");
#endif
printf("----------------------------------------------------------\n");
// ------------------------------------
// commandline may specify application
// name and baudrate
// ------------------------------------
if (argc > 1)
{
g_BaudRate = atoi(argv[1]);
if (g_BaudRate)
{
printf("Baudrate = %u\n", g_BaudRate);
argc--;
argv++;
}
}
if (argc > 1)
{
strncpy(g_AppName, argv[1], XL_MAX_APPNAME);
g_AppName[XL_MAX_APPNAME] = 0;
printf("AppName = %s\n", g_AppName);
argc--;
argv++;
}
if (argc > 1)
{
sscanf (argv[1], "%lx", &txID ) ;
if (txID)
{
printf("TX ID = %lx\n", txID);
}
}
// ------------------------------------
// initialize the driver structures
// for the application
// ------------------------------------
xlStatus = demoInitDriver(&xlChanMaskTx, &xlChanIndex);
printf("- Init : %s\n", xlGetErrorString(xlStatus));
#if 0
if (XL_SUCCESS == xlStatus)
{
// ------------------------------------
// create the RX thread to read the
// messages
// ------------------------------------
xlStatus = demoCreateRxThread();
printf("- Create RX thread : %s\n", xlGetErrorString(xlStatus));
}
#endif
if (XL_SUCCESS == xlStatus)
{
// ------------------------------------
// go with all selected channels on bus
// ------------------------------------
xlStatus = xlActivateChannel(g_xlPortHandle, g_xlChannelMask, XL_BUS_TYPE_CAN, XL_ACTIVATE_RESET_CLOCK);
printf("- ActivateChannel : CM=0x%I64x, %s\n", g_xlChannelMask, xlGetErrorString(xlStatus));
if (xlStatus == XL_SUCCESS)
{
activated = 1;
}
}
printf("\n: Press <h> for help - actual channel Ch=%d, CM=0x%02I64x\n", xlChanIndex, xlChanMaskTx);
// initialise the J1939 stack
J1939_stk_init();
// ------------------------------------
// parse the key - commands
// ------------------------------------
while (stop == 0)
{
unsigned long n;
INPUT_RECORD ir;
// run the J1939 stack
J1939_stk_periodic();
ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &ir, 1, &n);
if ((n == 1) && (ir.EventType == KEY_EVENT))
{
if (ir.Event.KeyEvent.bKeyDown)
{
c = ir.Event.KeyEvent.uChar.AsciiChar;
switch (c)
{
case 'v':
if (g_silent)
{
g_silent = 0;
printf("- screen on\n");
}
else
{
g_silent = 1;
printf("- screen off\n");
}
break;
case 't': // transmit a message
demoTransmit(txID, xlChanMaskTx);
break;
case 'b': // transmit message burst
demoTransmitBurst(txID);
break;
case 'm': // transmit a remote message
demoTransmitRemote(txID, xlChanMaskTx);
break;
case '-': // channel selection
if (xlChanIndex == 0)
{
xlChanIndex = g_xlDrvConfig.channelCount;
}
xlChanIndex--;
xlChanMaskTx = g_xlDrvConfig.channel[xlChanIndex].channelMask;
printf("- TX Channel set to channel: %02d, %s CM(0x%I64x)\n",
g_xlDrvConfig.channel[xlChanIndex].channelIndex, g_xlDrvConfig.channel[xlChanIndex].name, xlChanMaskTx);
break;
case '+': // channel selection
xlChanIndex++;
if (xlChanIndex >= g_xlDrvConfig.channelCount)
{
xlChanIndex = 0;
}
xlChanMaskTx = g_xlDrvConfig.channel[xlChanIndex].channelMask;
printf("- TX Channel set to channel: %02d, %s CM(0x%I64x)\n",
g_xlDrvConfig.channel[xlChanIndex].channelIndex, g_xlDrvConfig.channel[xlChanIndex].name, xlChanMaskTx);
break;
case 'x':
txID ^= XL_CAN_EXT_MSG_ID; // toggle ext/std
printf("- Id set to 0x%08X\n", txID);
break;
case 'I': // id selection
if (txID & XL_CAN_EXT_MSG_ID)
{
txID = (txID-1) | XL_CAN_EXT_MSG_ID;
}
else if (txID == 0)
{
txID = 0x7FF;
}
else
{
txID--;
}
printf("- Id set to 0x%08X\n", txID);
break;
case 'i':
if (txID & XL_CAN_EXT_MSG_ID)
{
txID = (txID+1) | XL_CAN_EXT_MSG_ID;
}
else if (txID == 0x7FF)
{
txID = 0;
}
else
{
txID++;
}
printf("- Id set to 0x%08X\n", txID);
break;
case 'g':
xlStatus = xlCanRequestChipState(g_xlPortHandle, g_xlChannelMask);
printf("- RequestChipState : CM(0x%I64x), %s\n", g_xlChannelMask, xlGetErrorString(xlStatus));
break;
case 'a':
if (g_TimerRate)
{
g_TimerRate = 0;
}
else
{
g_TimerRate = 20000;
}
xlStatus = xlSetTimerRate(g_xlPortHandle, g_TimerRate);
printf("- SetTimerRate : %d, %s\n", g_TimerRate, xlGetErrorString(xlStatus));
break;
case 'o':
switch(outputMode)
{
case XL_OUTPUT_MODE_NORMAL:
outputMode = XL_OUTPUT_MODE_SILENT;
break;
case XL_OUTPUT_MODE_SILENT:
outputMode = XL_OUTPUT_MODE_TX_OFF;
break;
case XL_OUTPUT_MODE_TX_OFF:
default:
outputMode = XL_OUTPUT_MODE_NORMAL;
break;
}
demoSetOutput(outputMode, xlChanMaskTx);
break;
case 'r':
xlStatus = xlResetClock(g_xlPortHandle);
printf("- ResetClock : %s\n", xlGetErrorString(xlStatus));
break;
case 's':
if (activated)
{
activated = 0;
}
else
{
activated = 1;
}
demoStartStop(activated);
break;
case 'p':
demoPrintConfig();
break;
case 27: // end application
stop = 1;
break;
case 'h':
demoHelp();
break;
default:
break;
// end switch
}
}
}
} // end while
if ((XL_SUCCESS != xlStatus) && activated)
{
xlStatus = xlDeactivateChannel(g_xlPortHandle, g_xlChannelMask);
printf("- DeactivateChannel: CM(0x%I64x), %s\n", g_xlChannelMask, xlGetErrorString(xlStatus));
}
demoCleanUp();
return(0);
} // end main()
////////////////////////////////////////////////////////////////////////////
// TxThread
//
//
////////////////////////////////////////////////////////////////////////////
DWORD WINAPI TxThread( LPVOID par )
{
XLstatus xlStatus = XL_SUCCESS;
XLevent xlEvent;
unsigned int n = 1;
unsigned int TxID = *(unsigned int*) par;
static unsigned char loop = 0;
memset(&xlEvent, 0, sizeof(xlEvent));
xlEvent.tag = XL_TRANSMIT_MSG;
xlEvent.tagData.msg.id = TxID;
xlEvent.tagData.msg.dlc = 8;
xlEvent.tagData.msg.flags = 0;
xlEvent.tagData.msg.data[0] = 1;
xlEvent.tagData.msg.data[1] = 2;
xlEvent.tagData.msg.data[2] = 3;
xlEvent.tagData.msg.data[3] = 4;
xlEvent.tagData.msg.data[4] = 5;
xlEvent.tagData.msg.data[5] = 6;
xlEvent.tagData.msg.data[6] = 7;
xlEvent.tagData.msg.data[7] = 8;
g_TXThreadRun = 1;
while (g_TXThreadRun && XL_SUCCESS == xlStatus)
{
++xlEvent.tagData.msg.data[0];
xlStatus = xlCanTransmit(g_xlPortHandle, g_xlChannelMask, &n, &xlEvent);
Sleep(10);
}
if (XL_SUCCESS != xlStatus)
{
printf("Error xlCanTransmit:%s\n", xlGetErrorString(xlStatus));
}
return NO_ERROR;
}
///////////////////////////////////////////////////////////////////////////
// RxThread
// thread to readout the message queue and parse the incoming messages
//
////////////////////////////////////////////////////////////////////////////
DWORD WINAPI RxThread(LPVOID par)
{
XLstatus xlStatus;
unsigned int msgsrx = RECEIVE_EVENT_SIZE;
XLevent xlEvent;
UNUSED_PARAM(par);
g_RXThreadRun = 1;
while (g_RXThreadRun)
{
WaitForSingleObject(g_hMsgEvent,10);
xlStatus = XL_SUCCESS;
while (!xlStatus)
{
msgsrx = RECEIVE_EVENT_SIZE;
xlStatus = xlReceive(g_xlPortHandle, &msgsrx, &xlEvent);
if (xlStatus != XL_ERR_QUEUE_IS_EMPTY)
{
if (!g_silent)
{
printf("%s\n", xlGetEventString(&xlEvent));
}
//ResetEvent(g_hMsgEvent);
}
}
}
return NO_ERROR;
}