64
9.6.9
Example Code for C
#include <stdio.h>
#include <stdlib.h>
#include "CAN_API.h"
int
main
()
{
TCAN_HANDLE Handle
;
TCAN_STATUS Status
;
CHAR
*
ComPort
=
"COM23"
;
CHAR
*
szBitrate
=
"800"
;
CHAR
*
acceptance_code
=
"1FFFFFFF"
;
CHAR
*
acceptance_mask
=
"00000000"
;
VOID
*
flags
=
CAN_TIMESTAMP_OFF
;
DWORD Mode
=
LoopBack
;
char
version
[
10
];
CAN_MSG SendMSG
;
CAN_MSG RecvMSG
;
Handle
=
-
1
;
Status
=
0
;
SendMSG
.
Flags
=
CAN_FLAGS_EXTENDED
;
SendMSG
.
Id
=
0x12345678
;
SendMSG
.
Size
=
8
;
SendMSG
.
Data
[
0
]
=
0x11
;
SendMSG
.
Data
[
1
]
=
0x22
;
SendMSG
.
Data
[
2
]
=
0x33
;
SendMSG
.
Data
[
3
]
=
0x44
;
SendMSG
.
Data
[
4
]
=
0x55
;
SendMSG
.
Data
[
5
]
=
0x66
;
SendMSG
.
Data
[
6
]
=
0x77
;
SendMSG
.
Data
[
7
]
=
0x88
;
Handle
=
CAN_Open
(
ComPort
,
szBitrate
,
acceptance_code
,
acceptance_mask
,
flags
,
Mode
);
printf
(
"handle= %d\n"
,
Handle
);
if
(
Handle
<
0
)
{
return
0
;
}
memset
(
version
,
0
,
sizeof
(
char
)
*
10
);
Status
=
CAN_Flush
(
Handle
);
Status
=
CAN_Version
(
Handle
,
version
);
if
(
Status
==
CAN_ERR_OK
)
{
printf
(
"Version : %s\n"
,
version
);
}
Status
=
CAN_Write
(
Handle
,
&
SendMSG
);
if
(
Status
==
CAN_ERR_OK
)
{
printf
(
"Write Success\n"
);
}
while
(
1
)
{
Status
=
CAN_Read
(
Handle
,
&
RecvMSG
);
if
(
Status
==
CAN_ERR_OK
)
{
printf
(
"Read ID=0x%X, Type=%s, DLC=%d, FrameType=%s, Data="
,
RecvMSG
.
Id
,(
RecvMSG
.
Flags
&
CAN_FLAGS_STANDARD
)
?
"STD"
:
"EXT"
,
RecvMSG
.
Size
,(
RecvMSG
.
Flags
&
CAN_FLAGS_REMOTE
)
?
"REMOTE"
:
"DATA"
);
for
(
int
i
=
0
;
i
<
RecvMSG
.
Size
;
i
++
)
{
printf
(
"%X,"
,
RecvMSG
.
Data
[
i
]
);
}