Skip to main content

Example: CANbus

Product: CODESYS CAN

Description

The CANBusAPI.library library lets you easily use CANbus functions. The library has been optimized for object-oriented programming in ST and graphical programming such as CFC. The library is based on the CAN Bus Low Level system library.

With this library, you can easily use the CANbus functions. It is based on the CANBus system library. Together with the CANBusAPI.library library, a sample application is provided. This application contains two programs with different implementations (object-oriented in ST and graphical in CFC) which show how to use of the library.

Additional information

The CANBusAPIExample.project sample project contains two implementations: one in ST, one in CFC. Both implement in different ways how specific CAN telegrams can be received and resent.

In the task configuration, add the call of one of the two programs (CFC_PRG, ST_PRG) and download the application to the controller. Start the application. Now when you generate a CAN telegram with an appropriate ID (for example, 0x500) from an external device, this telegram is processed and sent with the changed ID.

Example in ST

This program receives all incoming messages and echoes them back with CAN-ID +1.

MsgProcessor_EchoST: The function block implements the CAN.IMessageProcessor interface. The ProcessMessage method has already been implemented. In this method, the CAN ID of the received message is incremented by 1 and written back to the CAN driver.

ST_PRG: The program configures the CAN driver with the DEVICE_CONFIG structure and generates an instance of MsgProcessor_EchoST and MaskReceiver. MaskReceiver should be generated only one time and not cyclically; otherwise a new MaskReceiver will be generated with every call. The MaskReceiver is configured in such as way that all messages can be received with it. Then all messages are automatically passed to the ProcessMessage function of MessageProcessor.

Example in CFC

This program receives all messages in the CAN ID range from 16#500 to 16#550 and echoes them back with CAN-ID+1.

MsgProcessor_EchoST: Implements the CAN.IMessageProcessor interface. The ProcessMessage method has already been implemented. In this method, the CAN ID of the received message is incremented by 1 and written back to the CAN driver.

CFC_PRG: The program configures the CAN driver with the DEVICE_CONFIG structure and generates an instance of MsgProcessor_EchoST and AreaReceiver. The AreaReceiver is configured to receive all messages in the CAN ID range from 16#500 to 16#550. The received messages are then automatically passed to the ProcessMessage function of MessageProcessor. In addition, it is possible to use the CANBusDiagnosis function block to monitor the state of the CAN driver.

For more information about the function blocks, see the PDF file included in the package.

System requirements and restrictions

Programming system

CODESYS Development System (version 3.5.6.0 or higher)

Runtime system

CODESYS Control Win (version 3.5.6.0)

Add-on components

-

Note

_example_icon.png DOWNLOAD Project