A method of writing CAN analyzer Sfunction module by calling dynamic link library in Simulink

By writing the CAN analyzer Sfunction module and utilizing the dynamic link library API function, the problems of expensive and difficult development of Simulink and CAN communication real-time simulation equipment were solved, the combination of MATLAB software and the real environment was realized, the scientific research cost was reduced, and independent control was achieved.

CN115686524BActive Publication Date: 2025-09-19NAVAL UNIV OF ENG PLA
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211054307.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-08-31
Publication Date
2025-09-19
Estimated Expiration
2042-08-31

AI Technical Summary

Technical Problem

The existing real-time simulation method of Simulink and CAN communication has problems such as long equipment procurement cycle, high price, limited technical support and great development difficulty, especially in non-desktop computer or industrial computer environment where PCI interface devices cannot be used.

Method used

By writing the CAN analyzer Sfunction module, using the dynamic link library API function, combining MATLAB commands and Simulink packaging functions, the development is simple and inexpensive, suitable for USB_CAN analyzer, and realizes the connection between MATLAB software and the real environment.

Benefits of technology

It realizes the combination of MATLAB simulation software and real environment, shortens the research cycle, reduces scientific research costs, and achieves independent control, which is suitable for USB_CAN analyzer.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115686524B_ABST
    Figure CN115686524B_ABST
Patent Text Reader

Abstract

The present invention discloses a method for writing a CAN analyzer Sfunction module by calling a dynamic link library in Simulink, which relates to the technical field of simulation software. The C language code of the Sfunction module is written according to the dynamic link library API function, and the C language is compiled into a mexw64 file that can be run by MATLAB, and the Sfunction module encapsulation interface is created based on the compiled file. The method realizes the connection between MATLAB software and the CAN analyzer, realizes the combination of MATLAB simulation software and the real environment, provides a means for algorithm verification and performance analysis, and greatly shortens the research cycle; the method is applicable to USB_CAN analyzers on the Chinese market, does not require the mastery of Windows underlying driver protocols, uses C language development, is simple, fast, safe and reliable; the CAN analyzer Sfunction developed by the method can replace the CAN analyzer module that comes with Simulink, greatly saves the cost of scientific research and algorithm development, and realizes independent control.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of simulation software, in particular to a method for writing a CAN analyzer Sfunction module by calling a dynamic link library via Simulink. Background Art

[0002] Simulink combined with CAN communication for real-time simulation is a widely used technology. Generally, the CAN analysis module provided by Simulink is directly used in conjunction with the official CAN analyzer equipment for engineering design and research. Simulink combined with CAN communication is widely used in various fields, such as automotive systems, aerospace systems, ship control systems, etc.

[0003] Currently, there are two methods for real-time simulation using CAN communication with Simulink:

[0004] Use Simulink to provide CAN communication solutions: Perform real-time simulation of the system based on the equipment and software officially supported by Simulink;

[0005] Using the PCI-CAN board solution: According to the Windows kernel driver solution, write the corresponding Simulink driver, and then write the Sfunction module;

[0006] The above methods are widely used, but they all have certain defects, mainly including:

[0007] When using Simulink to provide CAN communication solutions, the equipment procurement cycle is long, the price is high, technical support is limited, a lot of data and protocols need to be designed according to specified formats, and the ability to adapt to the system is limited.

[0008] The development of the PCI-CAN board solution based on the Windows kernel requires users to be familiar with the kernel's operating mechanism, which is difficult and time-consuming. In non-desktop computer or industrial computer environments, devices with PCI interfaces cannot be used. Summary of the Invention

[0009] In view of the shortcomings of the existing technology, the purpose of the present invention is to provide a method for writing a CAN analyzer Sfunction module by calling a dynamic link library in Simulink, which is simple to develop, inexpensive, easy to use and carry, and has strong adaptability.

[0010] In order to achieve the above-mentioned purpose, the present invention is implemented through the following technical scheme: a method for writing a CAN analyzer Sfunction module by calling a dynamic link library in Simulink, writing the Sfunction module C language code according to the dynamic link library API function, and compiling the C language into a mexw64 file that can be executed by MATLAB, and creating an Sfunction module encapsulation interface based on the compiled file.

[0011] The specific steps include:

[0012] 1. Include the header file ECanVci.h of the CAN analyzer;

[0013] 2. Write the C language code of the Sfunction module corresponding to the API function provided by the header file;

[0014] 3. Use the mex command in the MATLAB command dialog box to load the dynamic link library and compile the Sfunction of the corresponding module into an executable mexw64 file;

[0015] 4. Create a packaging interface for the mexw64 file of the corresponding module: Create a packaging interface for the mexw64 file of the corresponding module and use the packaging function provided by Simulink for packaging.

[0016] Preferably, the step 1 includes the header file ECanVci.h of the CAN analyzer and the official header file mex.h of Simulink;

[0017] Preferably, in step 2, the API function provided by the header file is used to write the Sfunction module C language code of the corresponding function; the modules with different functions include the CAN initialization module usbcaninit, the CAN sending module usbcantransmit, and the CAN receiving module usbcanreceive.

[0018] As a preference, obtain the CAN device ID, CAN baud rate, CAN device index, and CAN device mode from the module interface through the system function mxGetPr; set the module sampling time to inherit the sampling time of other modules ssSetModelReferenceSampleTimelinheritanceRule(S,USE_DEFAULT_FOR_DISCRETE_INHERITANCE);

[0019] Preferably, global variables are set through the system function ssSetIWorkValue, and the CAN device ID, CAN baud rate, CAN device index, and CAN device mode are written into the global variables for other modules to call.

[0020] Preferably, the CAN device ID, CAN baud rate, CAN device index, and CAN device mode are written into the CAN analyzer device opening function OpenDevice, the CAN analyzer initialization function InitCAN, and the CAN device start function StartCAN.

[0021] Preferably, the CAN device ID, CAN baud rate, CAN device index, and CAN device mode are obtained from global variables through the system function ssGetIWorkValue for use by the CAN sending module usbcantransmit.

[0022] Preferably, the CAN sending module calls the system function ssGetInputPortRealSignal to obtain the data to be transmitted, and composes a complete sending frame with the CAN device ID, and writes the complete sending frame into the Transmit function of the CAN analyzer.

[0023] Preferably, the CAN device ID, CAN baud rate, CAN device index, and CAN device mode are obtained from global variables through the system function ssGetIWorkValue for use by the CAN receiving module usbcanreceive; the CAN receiving module calls the system function mxGetPr to obtain the filter ID, filter mode, and filter frame type parameters from the module encapsulation interface, and writes the obtained parameters into the dynamic link library function SetReference; after writing the parameters to SetReference, data is read from the dynamic link library function Receive.

[0024] Preferably, the step 3 uses the mex command to load the dynamic link library in the MATLAB command dialog box, and compiles the Sfunction of the corresponding module into an executable mexw64 file in the format of mex'space''library name''space''Sfunction module name'.

[0025] The beneficial effects of the present invention include: realizing the connection between MATLAB software and a CAN analyzer, realizing the combination of MATLAB simulation software and a real environment, providing a means for algorithm verification and performance analysis, and greatly shortening the research cycle; the present invention is applicable to USB-CAN analyzers on the Chinese market, does not require mastering the Windows underlying driver protocol, uses C language for development, is simple, fast, safe and reliable; the CAN analyzer Sfunction developed by the present method can replace the CAN analyzer module provided by Simulink, greatly saving the cost of scientific research and algorithm development, and realizing independent control. BRIEF DESCRIPTION OF THE DRAWINGS

[0026] The present invention will be described in detail below with reference to the accompanying drawings and specific embodiments;

[0027] Figure 1 is an architectural diagram of an embodiment of the present invention;

[0028] Figure 2 The software flow chart used in the embodiment of the present invention. DETAILED DESCRIPTION

[0029] In order to make the technical means, creative features, objectives and effects achieved by the present invention easier to understand, the present invention is further described below in conjunction with specific implementation methods.

[0030] Reference Figure 1-2 This specific implementation adopts the following technical solutions: a method of using Simulink to call a dynamic link library to write a CAN analyzer Sfunction module, selecting a common USB-CAN analyzer on the market; developing the Simulink Sfunction module based on the dynamic link library provided by the manufacturer; writing the Sfunction module C language code based on the dynamic link library API function, and compiling the C language into a mexw64 file that can be run by MATLAB, and creating the Sfunction module encapsulation interface based on the compiled file.

[0031] The specific steps include:

[0032] 1. Include the header file ECanVci.h of the CAN analyzer;

[0033] 2. Write the C language code of the Sfunction module corresponding to the API function provided by the header file;

[0034] 3. Use the mex command in the MATLAB command dialog box to load the dynamic link library and compile the Sfunction of the corresponding module into an executable mexw64 file;

[0035] 4. Create the encapsulation interface of the mexw64 file corresponding to the module.

[0036] It is worth noting that step 1 includes the CAN analyzer header file ECanVci.h and the Simulink official header file mex.h;

[0037] This specific embodiment uses the API functions provided in the header file to write C language code for the corresponding Sfunction module. The C code modules include the CAN initialization module usbcaninit, the CAN transmission module usbcantransmit, and the CAN reception module usbcanreceive. The system function mxGetPr is used to obtain the CAN device ID, CAN baud rate, CAN device index, and CAN device mode from the module interface. The system function ssSetIWorkValue is used to set global variables, and the CAN device ID, CAN baud rate, CAN device index, and CAN device mode described in claim 5 are written to the global variables for easy access by other modules.

[0038] This specific implementation writes the CAN device ID, CAN baud rate, CAN device index, and CAN device mode into the CAN analyzer's device-opening function, OpenDevice, the CAN analyzer's initialization function, InitCAN, and the CAN device-starting function, StartCAN. The CAN device ID, CAN baud rate, CAN device index, and CAN device mode are retrieved from global variables using the system function ssGetIWorkValue for use by the CAN transmitter module, usbcantransmit.

[0039] In this specific embodiment, the CAN transmitting module calls the system function ssGetInputPortRealSignal to obtain the data to be transmitted, and forms a complete transmit frame with the CAN device ID. The complete transmit frame is then written into the CAN analyzer's Transmit function. The system function ssGetIWorkValue is used to obtain the CAN device ID, CAN baud rate, CAN device index, and CAN device mode from global variables for use by the CAN receiving module usbcanreceive. The CAN receiving module calls the system function mxGetPr to obtain the filter ID, filter mode, and filter frame type parameters from the module encapsulation interface, and writes the obtained parameters into the dynamic link library function SetReference. After writing the parameters to SetReference, the data is then read from the dynamic link library function Receive.

[0040] This specific implementation sets the module sampling time to inherit the sampling time of other modules ssSetModelReferenceSampleTimelinheritanceRule(S,USE_DEFAULT_FOR_DISCRETE_INHERITANCE);

[0041] Use the mex command in the MATLAB command dialog box to load the dynamic link library and compile the Sfunction of the corresponding module into an executable mexw64 file in the format of mex'space''library name''space''Sfunction module name'.

[0042] Create the encapsulation interface of the mexw64 file of the corresponding module and use the encapsulation function provided by Simulink for encapsulation.

[0043] This specific implementation method writes the Sfunction module C language code based on the dynamic link library API function, compiles the C language into a mexw64 file that can be run by MATLAB, and creates the Sfunction module encapsulation interface based on the compiled file. This method connects MATLAB software to the CAN analyzer, combining MATLAB simulation software with the real environment, providing a means for algorithm verification and performance analysis, and greatly shortening the research cycle. This method is applicable to USB_CAN analyzers on the Chinese market, does not require mastering the Windows underlying driver protocol, and is developed in C language, making it simple, fast, safe and reliable. The CAN analyzer Sfunction developed by this method can replace the CAN analyzer module that comes with Simulink, greatly saving scientific research and algorithm development costs and achieving independent control.

[0044] The basic principles, main features, and advantages of the present invention are shown and described above. Those skilled in the art should understand that the present invention is not limited to the above embodiments. The above embodiments and descriptions are merely illustrative of the principles of the present invention. Various changes and modifications may be made to the present invention without departing from the spirit and scope of the present invention. Such changes and modifications are intended to fall within the scope of the present invention. The scope of protection claimed in the present invention is defined by the appended claims and their equivalents.

Claims

1. A method for writing a CAN analyzer Sfunction module by calling a dynamic link library in Simulink, characterized in that: Write the Sfunction module C language code according to the dynamic link library API function, and compile the C language into a mexw64 file that can be run by MATLAB. Create the Sfunction module encapsulation interface based on the compiled file; The specific steps include: (1) Include the header file ECanVci.h of the CAN analyzer; (2) Write the C language code of the Sfunction module corresponding to the function using the API function provided by the header file; (3) Use the mex command in the MATLAB command dialog box to load the dynamic link library and compile the Sfunction of the corresponding module into an executable mexw64 file; (4) Create the encapsulation interface of the mexw64 file of the corresponding module: Create the encapsulation interface of the mexw64 file of the corresponding module and use the encapsulation function provided by Simulink for encapsulation; The step (1) includes the header file ECanVci.h of the CAN analyzer and the official header file mex.h of Simulink; Get the CAN device ID, CAN baud rate, CAN device index, and CAN device mode from the module interface through the system function mxGetPr; set the module sampling time to inherit the sampling time of other modules ssSetModelReferenceSampleTimelinheritanceRule(S,USE_DEFAULT_FOR_DISCRETE_INHERITANCE); Set global variables through the system function ssSetIWorkValue, and write the CAN device ID, CAN baud rate, CAN device index, and CAN device mode into the global variables for other modules to call; The CAN device ID, CAN baud rate, CAN device index, and CAN device mode are obtained from global variables through the system function ssGetIWorkValue for use by the CAN receiving module usbcanreceive; the CAN receiving module calls the system function mxGetPr to obtain the filter ID, filter mode, and filter frame type parameters from the module encapsulation interface, and writes the obtained parameters into the dynamic link library function SetReference; after writing the parameters to SetReference, data is read from the dynamic link library function Receive.

2. The method of writing a CAN analyzer Sfunction module by calling a dynamic link library using Simulink according to claim 1, characterized in that: In the step (2), the API function provided by the header file is used to write the C language code of the Sfunction module corresponding to the function; the modules with different functions include the CAN initialization module usbcaninit, the CAN sending module usbcantransmit, and the CAN receiving module usbcanreceive.

3. The method of using Simulink to call a dynamic link library to write a CAN analyzer Sfunction module according to claim 2, characterized in that: Write the CAN device ID, CAN baud rate, CAN device index, and CAN device mode into the CAN analyzer's open device function OpenDevice, the CAN analyzer's initialization function InitCAN, and the CAN start device function StartCAN.

4. The method of writing a CAN analyzer Sfunction module by calling a dynamic link library using Simulink according to claim 1, characterized in that: The CAN sending module calls the system function ssGetInputPortRealSignal to obtain the data to be transmitted, and combines it with the CAN device ID to form a complete sending frame, and then writes the complete sending frame into the CAN analyzer Transmit function.

5. The method of using Simulink to call a dynamic link library to write a CAN analyzer Sfunction module according to claim 1, characterized in that: The step (3) uses the mex command in the MATLAB command dialog box to load the dynamic link library, and compiles the Sfunction of the corresponding module into an executable mexw64 file in the format of mex'space''library name''space''Sfunction module name'.

Citation Information

Patent Citations

  • Method for performing model simulation and code generation on custom hardware by using Simulink

    CN102087610A

  • Method for generating configuration for middleware protocol conversion of Internet of vehicles

    CN104331292A