How to read values from a Device which has CANOpen

I have a device which has CAN open interface with CAN Low/High/GND/Shield connector pins. I also have the X20F1041 Master module from B&R with X20EM1611 PLC Controller. I opened the I/O mapping and configuration windows on X20F1041 in Automation Studio. But i was not able to find any help in Help topics how to read specific CAN address from the device. Can anyone help me with it.?

Thanks
Drakku

Hi Drakku,

The X20IF1041-1 is a CANopen master interface. This means that it will talk to other CAN devices via the CANopen protocol. Usually, the device you want to talk to will come with an EDS (Electronic DataSheet) file which can be imported into Automation Studio. This tells the master how to read all of the data on the device. You could also use the AsCANopen library, though this is a more manual process.

1 Like

Hi Marcus

Thanks for the reply. I dont have aeds file from the device manufacturer. So i guess i will have to use the AsCANopen library. Where do i write those functions to read the values from specific CAN IDs ? I have seen that there is a Help section ( B&R Online Help But its not mentioned where to call the function., where to define variables and so on. Also i am not able to download example zip files in the above link.
Thanks
Regards
Drakku

Happy to help!

First, I want to make sure that the device you want to communicate with is a CANopen device, rather than a standard CAN device. If it’s a standard CAN device, and you want to read standard CAN frames (11-bit or 29-bit), then you’ll need a different module, such as:

  • X20IF1072 (Interface module form factor like what you have now)
  • X20CS1070 (X20 slice form factor)

With these, you’d use the ArCan library to read frames given a CAN ID.

If your device is in fact CANopen, then you can get sample code for the AsCANopen library in Automation Studio. The zips are not downloaded from the help. Instead you can add them to your project by going to the Logical View, double-clicking Library Samples in the Toolbox, and then selecting the sample you want to add in the window that pops up. With CANopen, you’re not reading CAN IDs but rather PDOs or SDOs. And rather than a CAN ID, you need to know the device’s node number and the message’s COB ID.

My device is a CANOpen. I was able to select the zip file and now am able to see the FBs like CANopenPDORead8 and so on. I placed the FB “CANopenPDORead8” in Master CAN variables window. Where do i define which PDO to read and the device node and COB ID?

The second example (CANopen read/write functions in LibAsCanOpen2_st.zip) can be used as a starting point for this. You can just change this code to match your setup. The steps are:

  1. Register the COB-ID with CANopenRegisterCOBID
  2. Start all nodes with CANopenNMT
  3. Read the PDO with CANopenPDORead8

The COB-ID is an input to both the CANopenRegisterCOBID and the CANopenPDORead8 function blocks. The Node is an input to the CANopenNMT function block (or 0 can be specified to start all nodes).

Hi MArcus

Thanks for your help. I am quite new to AS and B&R and thats why. You mentioned :"You can just change this code to match your setup. " But will it not affect the functionality of FB? Or is it better to copy the 3 parts mentioned in your previous comments to “Master_CAN–> Main.c–>ProgramCyclic” block.?? and then compile and run ?

Also another question is : in the FB , " CAN_L2.CANopenRegisterCOBID_0.cobid:= 16#182" What does 16#182 mean?
What if i needs to read the PDO1 at CAN ID 181h?

Hi Drakku,

You can think of a “function block” as a function with multiple inputs and multiple outputs. After you set the inputs, you call the function block and the outputs update. Essentially, you’re passing a structure as the input to a function. The function uses some of the structure elements as inputs (does not modify them) and changes others (the outputs). The function block may also change something else on the system (i.e. send a CANopen message via the CANopen interface). So yes, changing the inputs to the function block will change how it works, but you want it to work off of the inputs you have. This example program should be modified to fit your use case and then it will run as-is. The sample includes a state machine so it will step through all of the steps necessary to read a message and then send a message. Of course, the sequence of steps can be modified if needed.

It sounds like you’re writing most of your project in C. Unfortunately, it looks like most of the samples for this library are only available in the Structured Text programming language. That’s okay though! You can mix and match (have both C and ST programs running at the same time). I would recommend first leaving everything in this program as-is for learning and testing purposes. Once you feel confident that you understand how you want to lay your program out, you can start copying (or recreating) the sample in a C program. There are some minor syntax differences between ST and C, so you can’t just copy and paste without making any edits.

Also another question is : in the FB , " CAN_L2.CANopenRegisterCOBID_0.cobid:= 16#182" What does 16#182 mean?
What if i needs to read the PDO1 at CAN ID 181h?

16# is the Structured Text notation for a hexadecimal value. So 16#182 just means a 182 value in hex format. If you need to read a value at 181, you would use 16#181.

Thanks Marcus. I have started working from scratch to implement and test what you described. Thanks for your help. I will let you know if i succced

1 Like