⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 samplesource.c

📁 基于J1939的汽车CAN通讯测试,简单上位机软件源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
// ----------------------------------------------------------------------------------------------------
//  Dearborn Group Technology - RP1210 Example Source Code
// ----------------------------------------------------------------------------------------------------
// 
//  Name:           SampleSourceCode.c  Version 2.0
// 
//  Date:           October 20, 2008
// 
//  Written By:     Kenneth L. DeGrant II
//                  Field Applications Engineering Manager
//                  Dearborn Group Technology, Inc.
//                  TMC S.12 RP1210 Task Force Chairman
//                  kdegrant@dgtech.com
//                  859-358-1485 (cell)
// 
//  Description:    This code is provided by Dearborn Group to assist application developers in 
//                  developing RP1210A and RP1210B applications for the Dearborn Protocol Adapter
//                  (DPA) family of products.  It allows connecting to a DG DPA and the J1708/J1587,
//                  CAN@250k, CAN@500k, J1939 and J1850 VPW  protocols (the most common heavy-duty protocols).  
//                  It displays all message traffic seen/sent and also sends out request messages
//                  every 5 seconds.  
//
// CAN Notes:       The code also sends a 29-bit CANID of 0x18EA00F9 with three bytes
//                  of data [C5][FD][00]; which correlates to the J1939 PGN of 59904 (the request PGN) 
//                  requesting the PGN 0x00FDC5 (64965 - ECU Identification Information - ECUID).
//                  The receive routine for the CAN protocols handles both standard CAN (11-bit) 
//                  as well as extended (29-bit) CAN traffic.  GMLAN (protocol name = IESCAN) which 
//                  is 500k CAN (J2284) is also included, but no specific GMLAN messages are presented.
//                  GMLAN is being treated as simply "CAN" in this example code.
//
// J1850 Notes:     The code sends out a request for MODE=1, PID=0 OBDII request as the periodic
//                  message.  This code was a port from a J2534 sample application and has not been tested.  
//                  No code is written to decode any specific J1850 messages.
//
// Code Notes:      All necessary code for this project is in one file (this file).  Because this
//                  program runs on a DOS command prompt, it can be readily adapted to do any basic 
//                  functionality that a developer unfamiliar with RP1210 might want to do without
//                  having to look at complex MFC or Visual Basic code or dialog boxes, etc.  
//
// Logging Notes:   This code also presents the user the option to write all data that appears
//                  on the screen (messages, errors, etc) to a disk file.  This makes this code
//                  a very simple data logger.  You will see where two file pointers are passed to 
//                  most every function.  Passing "stdout" as the first FILE * argument to these
//                  functions causes the data to get printed to the screen, and the second FILE *
//                  is for logging to the file.  Note that some functions do not use the second
//                  argument, as you may not want "translated" or just information data writtent to a file.
//
// Compile Notes:   This code was successfully compiled and executed using Microsoft Visual C++ 6.0.
//
//                  It will take modification to compile easily under the "free" Microsoft 8.0
//                  Enterprise Edition compiler, but has been done.  The main problem with going
//                  from C++ 6.0 to 8.0 was the call to LoadLibrary.  It seems that a wide character
//                  string was needed.  Here is the code that was donated by Jesse Stanley of the US
//                  Army Corps of Engineers to solve that problem.  Note that you will get many 
//                  warnings where Microsoft wants you to use their "safe" string functions instead
//                  of the standard ANSI C like sprintf, etc.  These warnings can safely be ignored.
//
//            [//   Quick fix for char * to LPSWSTR issue.
//            [//   
//            [//   // Added variables at start of main ( void )
//            [//   int a;
//            [//   BSTR unicodestr;
//            [//   
//            [//   // After assigning szDLLName
//            [//   
//            [//   a = lstrlenA(szDLLName);
//            [//   unicodestr = SysAllocStringLen(NULL, a);
//            [//   MultiByteToWideChar(CP_ACP, 0, szDLLName, a, unicodestr, a);
//            [//   
//            [//   LoadLibraryAndFunctions( unicodestr );
//            [//   
//            [//   // Now should free the BSTR
//            [//   SysFreeString(unicodestr);
//                  
//
//  Requirements:   This program requires that you have downloaded and installed the latest DPA
//                  drivers for either the DPA 4+ (and prior), or the DPA 5.  These drivers are 
//                  readily available from the Dearborn Group Technology website as follows:
//
//                               http://www.dgtech.com/download.php
//
//  No Liability:   This code is example code and is NOT warranted to be fit for any particular use 
//                  or purpose.  Dearborn Group Technology will not be held liable for any use or 
//                  misuse of this code "under any circumstances whatsoever".  
// 
//  Notes:          Be sure to have your DOS command prompt set to a width of at least 150, as the 
//                  program displays about 140 contiguous characters from a J1939 message.
// 
//                  It is possible to develop a completely RP1210A/B-compliant application that
//                  will not connect, send or read messages to or from a particular databus.  
//                  To ensure success, please read the documentation for the protocol you want to use 
//                  thoroughly along with reading the "complete" RP1210 document before trying to 
//                  adapt this code to a particular purpose.
//
//  Copyright:      This code is Copyrighted (c) 2008 by Dearborn Group Technology, and is intended
//                  solely for the use of our DPA customers or potential DPA customers.  No portion,
//                  including "snippets" of this code are to be used, investigated, or copied in any 
//                  shape, form, or fashion, by anyone who is, or may in the future be, in competition 
//                  with Dearborn Group Technology in any way.  The code shall also not be modified in
//                  a way that it is capable of being used with any adapter outside of one from Dearborn 
//                  Group Technology.
//
// ----------------------------------------------------------------------------------------------------
//  Revision  Date       Notes
//     1.0    04-16-08   Original Version.
//     2.1    08-04-08   Added CAN and IESCAN protocols, added write to file option.
//     2.1    10-20-08   Added J1850 protocol and logging capability.
// 
// ----------------------------------------------------------------------------------------------------

#define PROGRAM_ID "Dearborn Group Technology - RP1210 Example Souce Code - Version 2.0 - 10/20/2008"

//-----------------------------------------------------------------------------------------------------
// C Include Files
//-----------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<windows.h>
#include<winuser.h>
#include<signal.h>
#include<conio.h>
#include<time.h>

//-----------------------------------------------------------------------------------------------------
// RP1210B   RP1210_SendCommand Defines (From RP1210B Document)
//-----------------------------------------------------------------------------------------------------

typedef unsigned char U8;

#define RP1210_Reset_Device                            0
#define RP1210_Set_All_Filters_States_to_Pass          3
#define RP1210_Set_Message_Filtering_For_J1939         4
#define RP1210_Set_Message_Filtering_For_CAN           5
#define RP1210_Set_Message_Filtering_For_J1708         7
#define RP1210_Set_Message_Filtering_For_J1850         8
#define RP1210_Set_Message_Filtering_For_ISO15765      9
#define RP1210_Generic_Driver_Command                 14
#define RP1210_Set_J1708_Mode                         15
#define RP1210_Echo_Transmitted_Messages              16
#define RP1210_Set_All_Filters_States_to_Discard      17
#define RP1210_Set_Message_Receive                    18
#define RP1210_Protect_J1939_Address                  19
#define RP1210_Set_Broadcast_For_J1708                20
#define RP1210_Set_Broadcast_For_CAN                  21
#define RP1210_Set_Broadcast_For_J1939                22
#define RP1210_Set_Broadcast_For_J1850                23
#define RP1210_Set_J1708_Filter_Type                  24
#define RP1210_Set_J1939_Filter_Type                  25
#define RP1210_Set_CAN_Filter_Type                    26
#define RP1210_Set_J1939_Interpacket_Time             27
#define RP1210_SetMaxErrorMsgSize                     28
#define RP1210_Disallow_Further_Connections           29
#define RP1210_Set_J1850_Filter_Type                  30
#define RP1210_Release_J1939_Address                  31
#define RP1210_Set_ISO15765_Filter_Type               32
#define RP1210_Set_Broadcast_For_ISO15765             33
#define RP1210_Set_ISO15765_Flow_Control              34
#define RP1210_Clear_ISO15765_Flow_Control            35
#define RP1210_Set_ISO15765_Link_Type                 36
#define RP1210_Set_J1939_Baud                         37
#define RP1210_Set_ISO15765_Baud                      38
#define RP1210_Set_BlockTimeout                      215
#define RP1210_Set_J1708_Baud                        305

//-----------------------------------------------------------------------------------------------------
// RP1210B Constants - Check RP1210B document for any updates.
//-----------------------------------------------------------------------------------------------------

#define BIT0                                           1  // Bit 0 - Used for masking bits
#define BIT1                                           2  // Bit 1 - Used for masking bits
#define BIT2                                           4  // Bit 2 - Used for masking bits
#define BIT3                                           8  // Bit 3 - Used for masking bits
#define BIT4                                          16  // Bit 4 - Used for masking bits
#define BIT5                                          32  // Bit 5 - Used for masking bits
#define BIT6                                          64  // Bit 6 - Used for masking bits
#define BIT7                                         128  // Bit 7 - Used for masking bits

#define CONNECTED                                      1  // Connection state = Connected 
#define NOT_CONNECTED                                 -1  // Connection state = Disconnected
 
#define FILTER_PASS_NONE                               0  // Filter state = DISCARD ALL MESSAGES
#define FILTER_PASS_SOME                               1  // Filter state = PASS SOME (some filters)
#define FILTER_PASS_ALL                                2  // Filter state = PASS ALL

#define NULL_WINDOW                                    0  // Windows 3.1 is no longer supported.

#define BLOCKING_IO                                    1  // For Blocking calls to send/read.
#define NON_BLOCKING_IO                                0  // For Non-Blocking calls to send/read.
#define BLOCK_INFINITE                                 0  // For Non-Blocking calls to send/read.

#define BLOCK_UNTIL_DONE                               0  // J1939 Address claim, wait until done
#define RETURN_BEFORE_COMPLETION                       2  // J1939 Address claim, don't wait

#define CONVERTED_MODE                                 1  // J1708 RP1210Mode="Converted"
#define RAW_MODE                                       0  // J1708 RP1210Mode="Raw"

#define MAX_J1708_MESSAGE_LENGTH                     508  // Maximum size of J1708 message (+1)
#define MAX_J1939_MESSAGE_LENGTH                    1796  // Maximum size of J1939 message (+1)
#define MAX_ISO15765_MESSAGE_LENGTH                 4108  // Maximum size of ISO15765 message (+1)

#define ECHO_OFF                                    0x00  // EchoMode
#define ECHO_ON                                     0x01  // EchoMode

#define RECEIVE_ON                                  0x01  // Set Message Receive
#define RECEIVE_OFF                                 0x00  // Set Message Receive

#define ADD_LIST                                    0x01  // Add a message to the list.
#define VIEW_B_LIST                                 0x02  // View an entry in the list.
#define DESTROY_LIST                                0x03  // Remove all entries in the list.
#define REMOVE_ENTRY                                0x04  // Remove a specific entry from the list.
#define LIST_LENGTH                                 0x05  // Returns number of items in list.

#define FILTER_PGN                            0x00000001  // Setting of J1939 filters
#define FILTER_PRIORITY                       0x00000002  // Setting of J1939 filters
#define FILTER_SOURCE                         0x00000004  // Setting of J1939 filters
#define FILTER_DESTINATION                    0x00000008  // Setting of J1939 filters
#define FILTER_INCLUSIVE                            0x00  // FilterMode
#define FILTER_EXCLUSIVE                            0x01  // FilterMode

#define SILENT_J1939_CLAIM                          0x00  // Claim J1939 Address
#define PASS_J1939_CLAIM_MESSAGES                   0x01  // Claim J1939 Address

#define CHANGE_BAUD_NOW                             0x00  // Change Baud
#define MSG_FIRST_CHANGE_BAUD                       0x01  // Change Baud
#define RP1210_BAUD_9600                            0x00  // Change Baud
#define RP1210_BAUD_19200                           0x01  // Change Baud
#define RP1210_BAUD_38400                           0x02  // Change Baud
#define RP1210_BAUD_57600                           0x03  // Change Baud
#define RP1210_BAUD_125k                            0x04  // Change Baud
#define RP1210_BAUD_250k                            0x05  // Change Baud
#define RP1210_BAUD_500k                            0x06  // Change Baud
#define RP1210_BAUD_1000k                           0x07  // Change Baud

#define STANDARD_CAN                                0x00  // Filters
#define EXTENDED_CAN                                0x01  // Filters
#define STANDARD_CAN_ISO15765_EXTENDED              0x02  // 11-bit with ISO15765 extended address
#define EXTENDED_CAN_ISO15765_EXTENDED              0x03  // 29-bit with ISO15765 extended address
#define STANDARD_MIXED_CAN_ISO15765                 0x04  // 11-bit identifier with mixed addressing
#define ISO15765_ACTUAL_MESSAGE                     0x00  // ISO15765 ReadMessage - type of data
#define ISO15765_CONFIRM                            0x01  // ISO15765 ReadMessage - type of data
#define ISO15765_FF_INDICATION                      0x02  // ISO15765 ReadMessage - type of data

#define LINKTYPE_GENERIC_CAN                        0x00  // Set_ISO15765_Link_Type argument
#define LINKTYPE_J1939_ISO15765_2_ANNEX_A           0x01  // Set_ISO15765_Link_Type argument
#define LINKTYPE_J1939_ISO15765_3                   0x02  // Set_ISO15765_Link_Type argument

#ifndef TRUE
#define TRUE                                        0x01
#define FALSE                                       0x00
#endif

//-----------------------------------------------------------------------------------------------------
// RP1210B Return Definitions
//-----------------------------------------------------------------------------------------------------

#define NO_ERRORS                                      0
#define ERR_DLL_NOT_INITIALIZED                      128
#define ERR_INVALID_CLIENT_ID                        129
#define ERR_CLIENT_ALREADY_CONNECTED                 130
#define ERR_CLIENT_AREA_FULL                         131
#define ERR_FREE_MEMORY                              132
#define ERR_NOT_ENOUGH_MEMORY                        133

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -