Code: Select all
// Transmit a list of CAN bus messages through PEAK USB.
// The list of CAN bus messages is stored in a file specified as command line argument to this program.
// Two file (message) formats are supported:
// 1. PcanView trace (received) files with extension *.trc
// 2. PcanView transmit files with extension *.xmt
#include "stdafx.h"
#include <windows.h>
#include "PCANBasic.h"
int _tmain(int argc, _TCHAR* argv[])
{
TPCANStatus result;
char line[256];
TPCANMsg msg;
FILE *fp;
unsigned char messageType;
bool xmtFileFormat = false;
if (argc != 2) {
printf("Input file not specified.\n");
exit(1);
}
if (strstr(argv[1], ".xmt") != NULL)
xmtFileFormat = true;
else if (strstr(argv[1], ".trc") == NULL) {
printf("Specified file %s does not have extension *.trc or *.xmt\n", argv[1]);
exit(1);
}
if ((fp = fopen(argv[1], "r")) == NULL) {
printf("Cannot open file %s - %s\n", argv[1], strerror(int errno));
exit(1);
}
result = CAN_Initialize(PCAN_USBBUS1, (TPCANBaudrate)0x1D14); // 33.333 kBits
if(result != PCAN_ERROR_OK) {
CAN_GetErrorText(result, 0, line);
printf("%s\n", line);
exit(1);
}
while (fgets(line, sizeof line, fp) != NULL ) { /* read a line */
if (line[0] == ';') // skip comment lines starting with ;
continue;
// extract the CAN message fields from the line
if (xmtFileFormat) {
// PcanView transmit file with extension *.xmt
// message example format:
// 1001C040h 0 8 D 07h 65h 14h 80h 00h 00h 00h 00h
result = sscanf(line, "%X%*c %*s %hd %c %hX%*c %hX%*c %hX%*c %hX%*c %hX%*c %hX%*c %hX%*c %hX%*c",
&msg.ID, &msg.LEN, &messageType, &msg.DATA[0], &msg.DATA[1], &msg.DATA[2], &msg.DATA[3], &msg.DATA[4], &msg.DATA[5], &msg.DATA[6], &msg.DATA[7]);
if (messageType != 'D')
continue; // skip no Data messages
if (line[3] == 'h')
msg.MSGTYPE = PCAN_MESSAGE_STANDARD; // standard message ID is three digits followed by 'h'
else if (line[8] == 'h')
msg.MSGTYPE = PCAN_MESSAGE_EXTENDED; // extended message ID is eight digits followed by 'h'
else
continue; // skip lines with unexpected ID format
} else {
// PcanView trace (received) file with extension *.trc
// message example format:
// 120) 24551.8 Rx 0621 8 01 12 00 00 00 00 00 00
result = sscanf(line, "%*s %*s %*s %X %hd %hx %hx %hx %hx %hx %hx %hx %hx",
&msg.ID, &msg.LEN, &msg.DATA[0], &msg.DATA[1], &msg.DATA[2], &msg.DATA[3], &msg.DATA[4], &msg.DATA[5], &msg.DATA[6], &msg.DATA[7]);
if (line[28] == ' ')
msg.MSGTYPE = PCAN_MESSAGE_STANDARD; // standard message ID is three 4 followed prefixed with 4 spaces e.g. " 0621"
else
msg.MSGTYPE = PCAN_MESSAGE_EXTENDED; // extended message ID is eight digits e.g. "0FFFE099"
}
// finally send the CAN message
while ((result = CAN_Write(PCAN_USBBUS1, &msg)) == PCAN_ERROR_QXMTFULL);
if (result != PCAN_ERROR_OK) {
CAN_GetErrorText(result, 0, line);
printf("%s\n", line);
break;
}
}
CAN_Uninitialize(PCAN_USBBUS1);
return 0;
}
Code: Select all
1. Sellect File->New->Project.
2. In the “Name:” field specify the name of the program (e.g. can_tx).
3. Specify where the project should be located in the “Location:” field.
4. Leave the “Solution name:” field the same as the name selected at step 2.
5. Select “Win32 Console Application".
6. Click “OK” button.
7. Click “Finish” button in the following screen.
8. Copy and replace the auto generated code with the example source code above.
9. Right click on the “can_tx” in the “Solution Explorer” pan on the left side of the screen and select “Properties”. Click on the “+” sign of the “Configuration Properties“ in the left pan of the dialog and select “VC++ Directories”. Next select “Include Directories” and click on the down arrow (on the right end of “Include Directories”) and select “<edit>”. Click on the yellow directory button “New Line (Ctrl-Insert)” and browse to the directory where “pcan-basic” zip file has been extracted. Then select “pcan-basic\PCAN-Basic API\Include” directory and click “OK”. Click “Apply” and “OK” buttons in the parent window.
10. Once again Right click on the “can_tx” in the “Solution Explorer” pan on the left side of the screen and select “Properties”. Click on the “+” sign of the “Configuration Properties“ in the left pan of the dialog, select “General”. Next select “Character Set” and click on the down arrow (on the right end of “Character Set”) and select “Use Multi-Byte Character Set”. Click “Apply” and “OK” buttons.
11. Once again Right click on the “can_tx” in the “Solution Explorer” pan on the left side of the screen and select “Add existing Item…”. Browse to the directory where “pcan-basic” zip file has been extracted. Then select “pcan-basic\PCAN-Basic API\Win32\VC_LIB\PCANBasic.lib” and click the “Add” button.
12. Click on “Build” and then on “Build Solution” (short cut for this operation is F7).
13. Open DOS command prompt and change directory to the “VC++ Express” project location chosen at step 3. The built executable “can_tx.exe” will be in “can_tx\Debug” subdirectory. Change the directory to where the “can_tx.exe” executable have been generated.
14. Copy “PCANBasic.dll” from the “pcan-basic\PCAN-Basic API\Win32” directory to “can_tx\Debug” where the “can_tx.exe” executable is.
15. Run “can_tx.exe my_can_tx_list.xmt”, where “my_can_tx_list.xmt” is the file containing the CAN messages to be sent. “my_can_tx_list.xmt” can be manually generated or by “PCAN-View”. Here is an example of two CAN commands in “my_can_tx_list.xmt” generated by “PCAN-View”:
; C:\temp\my_can_tx_list.xmt
16. ; CAN messages saved by PCAN-View 3.0.8
17. ;
18. ; Columns descriptions:
19. ; ~~~~~~~~~~~~~~~~~~~~~
20. ; +Message ID
21. ; | +Cycle time in ms (0=manual)
22. ; | | +Length of message
23. ; | | | +Frame type: D)ata or R)emote request
24. ; | | | | +Message data
25. ; | | | | |
26. 621h 0 8 D 00h 40h 00h 00h 00h 00h 00h 00h
27. 10026040h 0 8 D 37h 4Ch 38h 37h 31h 37h 35h 38h