JZH85F
Hi,
I have decided to add the CAN bus management to my ESP32 platform with OpenPLC, and I need a few details :
I have searched into many thread in the forum, but not really found something which applies to my configuration, many threads make reference to the previous editor, or different language like Python.
I'm using editor version 4.1.2.
 
I understand that the simplest way is to write a custom function block in C/C++ language (python is not enabled), but I don't know how to share data from inside this block with my other regular PLC tasks, since I believe that it is the only way of interact with the PLC program.
 
I'm not sure of how to write code for the can bus, and if I can rely on external code from arduino libraries or source code to be inserted in these function blocks.
will the CAN data from the ESP32 be processed by interrupt or not, is there some risks of missing CAN frames....
 
are there some examples of such configuration already been implemented ?
Reply 0 0
thiagoralves
The C/C++ Function Block is plain C. If your platform is arduino-compatible (which ESP32 is), then anything that works on Arduino IDE will also work inside a C/C++ Function Block. You can think of it like an "Arduino Sketch" inside your PLC project. Variable sharing follow the exact same approach as any other IEC 61131-3 block. All variables you define at the top of the block on the variables table are IEC 61131-3 variables for the block, visible across the entire C/C++ source code, but also outside the block. For example, if you create a C/C++ block and add two input variables called var_a and var_b, and one output variable called result, then you can simply write on your C code:
result = var_a + var_b; 
and it would work. You can send var_a and var_b values from other blocks or other parts of your PLC program, the same way you do with any other block on your PLC library. You can also get the result output from the block through the same pattern.
 
As for interrupts, etc, this could obviously disrupt the determinism of the runtime, but they are supported the same way they can be supported on Arduino IDE. In fact, anything you write on a C/C++ will pretty much be compiled by arduino-cli, so it should all be compatible.
Reply 0 0
JZH85F
thank you for your reply.
I have implemented a simple fonction to test, and it is working. I have set canout to a value in the fonction setup, and I can read it from outside with Modbus protocol, at MW4.
I suppose that the only way to share data from the custom function block and the other POUs is to use the in and out from the block after having placed it on the diagram, and connecting ins and outs to input and output blocks like on my screenshot.
I also guess that declaring canout as external like here has no effet ?
What is the usage of 'CANBUS0' proposed in the list of variables, in the example below ?
[203128%2F9698662%2Fmceclip0]
Reply 0 0
thiagoralves
A block is similar to a class in object-oriented programming. CANBUS0 is the instance of your class. Each instance has its own memory an context. External is used when you have a global variable and want to access it from the POU. If you declare your block as global (on the resources screen) then you will be able to access the block instance from anywhere using External. I have never tried C/C++ blocks declared as global, hopefully the compiler will handle it correctly. If not, let us know.
Reply 0 0
JZH85F
For the moment all is OK, I can access to the function from any POU, since it is available in the library list on the left.
[203128%2F9698825%2Fmceclip0]
I have not declared the function in the Resources page.
What kind of thing can be done with the function variable (here named CANBUS0) available in the POU's variable list, if assigned to an input block ?
[203128%2F9698834%2Fmceclip0]
Reply 0 0
thiagoralves
As I explained on my previous message, the "function variable" is an instance of your block. You can take a look at object-oriented programming concepts to get a better understanding of what instance means, but essentially every instance is a different "copy" of your block. If you have your block called by two different POUs, each one is calling a different version of your block. You can access your block shared variables (input and output) using the dot notation, as you can do with any other IEC 61131-3 block. In your particular case, for example, you can access CANBUS0.canin and CANBUS0.canout.
Reply 0 0
JZH85F
As I explained on my previous message, the "function variable" is an instance of your block. You can take a look at object-oriented programming concepts to get a better understanding of what instance means, but essentially every instance is a different "copy" of your block. If you have your block called by two different POUs, each one is calling a different version of your block. You can access your block shared variables (input and output) using the dot notation, as you can do with any other IEC 61131-3 block. In your particular case, for example, you can access CANBUS0.canin and CANBUS0.canout.
ok, I understand.
thanks
Reply 0 0
mrea
@JZH85F I found it better to write the entire protocol as an arduino library with simple API calls that you use in the OpenPLC function blocks to the core protocol library class, in the FB all you need to do is declare the class instance once in say a FB called CAN_CONFIG and then you have your CAN_READ and CAN_WRITE FBs use the instance in CAN_CONFIG. So in your read and write FBs you have

 
#include <your_library_name.h>
extern can_protocol_class_name class_instance;
 
 
This way you can now access every public member in the class such as you API calls to read and write functions etc

On my Github I will soon have examples using esp arduino modbus library with call backs for DONE and ERROR on the FB

https://github.com/mod-bee/modbee-node-uio

Check out the openPLC folder, has example projects and the function blocks
Reply 0 0
JZH85F
@JZH85FI found it better to write the entire protocol as an arduino library with simple API calls that you use in the OpenPLC function blocks to the core protocol library class, in the FB all you need to do is declare the class instance once in say a FB called CAN_CONFIG and then you have your CAN_READ and CAN_WRITE FBs use the instance in CAN_CONFIG. So in your read and write FBs you have

 
  1. #include <your_library_name.h>
  2. extern can_protocol_class_name class_instance;
 
 
This way you can now access every public member in the class such as you API calls to read and write functions etc

On my Github I will soon have examples using esp arduino modbus library with call backs for DONE and ERROR on the FB



Check out the openPLC folder, has example projects and the function blocks
thank you, I wwill take some time to read , I'm very new to OpenPLC so I need time.... to understand all of it
Reply 0 0
Reply