1 Case hardware introduction
Kvaser is a Swedish company that specializes in providing CAN and LIN bus analyzers and data loggers. It has nearly 30 years of experience in the field of CAN product development. The CAN communication hardware model selected in this case is Kvaser Leaf Light v2. The product is as shown below Shown:
[ Public account dotNet industrial control host computer: thinger_swj]
In order to support this product, an analog controller RL78 CAN ECU provided by Westpac is also prepared for functional testing. As shown in the figure below, the analog controller can be controlled by the host computer to achieve speed adjustment and turn signal control. .
The hardware is ready, and the wiring is relatively simple. Because it is a DB9 plug that has been integrated, it is enough to directly connect the male and female connectors between the ECU and the CAN card. The CAN card is connected to the computer via USB. After installing the driver, You can see the CAN device in the device manager of the computer, as shown in the figure below:
2 Preparation for case development
CAN communication development needs to call some SDK or Lib libraries provided by the manufacturer, and the manufacturers generally provide some Demo, which contains many different languages. We find the words dotnet or C# or cs, and then open the relevant case for research . Kvaser does not provide a similar demo, but provides a Kvaser CANLib SDK software. After installation, it contains the following content:
Then open dotnet >> win32 >> fw40 and find the following dlls. Our development is mainly based on the dlls in this folder. There are two folders under dotnet, win32 and x64. Here, according to different project platform versions, Select the corresponding dll.
Kvaser provides an HTML help manual for the use of CANLib library, as shown in the figure below, if you don’t understand a function or parameter, you can find it through this help manual:!
Although there are dlls and help manuals, we are still at a loss as to which methods should be called and the order between calling methods. The following development guide should be a help.
3 Case program development
After everything is in place, we can start program development at this time. First create a Windows Forms application with the project name thinger.com.kvaserCANECU. The preliminary design of the interface is shown in the figure below:
[ Public account dotNet industrial control host computer: thinger_swj]
(1) After the UI interface design is completed, first copy canlibCLSNET.dll to the project and add a reference
(2) Call canInitializeLibrary to initialize and initialize the baud rate in the form initialization
(3) Under the refresh button event, get all the CAN communication information that can be obtained
(4) In the open button event, realize the function of opening and closing the CAN card