Aurentaico
Hello @thiagoralves, I am trying to add new function blocks to OpenPLC editor and I got some errors. 

I tried these steps. I could'nt find how to create an .st file without body. I create a new program you can see it in the photo.
[203128%2F8771782%2Fmceclip0]
I added new function block. And choose language as FBD.
[203128%2F8771783%2Fmceclip0]
When I add the variables as in photo. I got an error named as the No body defined in "STM32CAN_CONF" POU but I don't know how to create a body for CAN communication. I am planning to handle it in code part.
[203128%2F8771791%2Fmceclip0]

Can anyone help me about it? How can I add a custom function block without body or which body should I use? I read adding new blocks to openplc's library but I stuck at the beginning. 
Reply 0 0
Aurentaico
Thank you a lot @thiagoralves . I am going to test them and let you know for future problems and progress. When I am done I want to fork that blocks with you so everyone can use it for their projects. 
Reply 0 0
Aurentaico
@thiagoralves I have one more question. Do I need to do something for addin an arduino library for my block. For example I am using Stm32_CAN library and I added it in code it works but do I need to implement that library externally?
Reply 0 0
thiagoralves
Yes. You just need to ensure that the library is installed. Check the arduino builder.py file at line 156: https://github.com/thiagoralves/OpenPLC_Editor/blob/master/editor/arduino/builder.py
Reply 0 0
Aurentaico
Yes. You just need to ensure that the library is installed. Check the arduino builder.py file at line 156: https://github.com/thiagoralves/OpenPLC_Editor/blob/master/editor/arduino/builder.py
How can I add it just writing the name is enough or is there anything that I need to do. Do you have any documents about it? 
Reply 0 0
Aurentaico
I am asking like this @thiagoralves because the library that I am trying to use need seperate header file like this when I add it in STM32_CAN.h file I got errors I guess it needs to be in a seperate file :
[203128%2F8773661%2Fmceclip0]

Could you help me with it also please? 
Reply 0 0
thiagoralves
You must use Arduino managed libraries (like the ones you can find at the Arduino IDE under Tools -> Manage Libraries). For those libraries, arduino-cli (which is bundled with OpenPLC) does all the management during compile time. Separate libraries with their own separate header files are extremely hard to manage and therefore can't be added to OpenPLC Editor.
Reply 0 0
thiagoralves
How can I add it just writing the name is enough or is there anything that I need to do. Do you have any documents about it? 
Yes, just add the name. But again, as I said, they must be Arduino managed libraries. 
Reply 0 0
Aurentaico
You must use Arduino managed libraries (like the ones you can find at the Arduino IDE under Tools -> Manage Libraries). For those libraries, arduino-cli (which is bundled with OpenPLC) does all the management during compile time. Separate libraries with their own separate header files are extremely hard to manage and therefore can't be added to OpenPLC Editor.
STM32_CAN is one of them but it just requires another file thats all. This is the library that I am talkin about: https://github.com/pazi88/STM32_CAN/blob/main/STM32_CAN.h 

[203128%2F8773673%2Fmceclip0]
 
I just need to put that file while I am composing the code for HAL libraries for STM family could you glance that repo please? 
Reply 0 0
thiagoralves
Looks like all it needs is a 
#if !defined(HAL_CAN_MODULE_ENABLED)
#define HAL_CAN_MODULE_ENABLED
#endif
Try adding that to the defines.h file. If that doesn't work then I'm out of ideas
Reply 0 0
Aurentaico
Looks like all it needs is a 
  1. #if !defined(HAL_CAN_MODULE_ENABLED)
  2. #define HAL_CAN_MODULE_ENABLED
  3. #endif
Try adding that to the defines.h file. If that doesn't work then I'm out of ideas
Okay I will try it and let you know @thiagoralves. Thank you so much. Appreciate it.
Reply 0 0
thiagoralves
Keep in mind that defines.h is dynamically generated during compile time. Check https://github.com/thiagoralves/OpenPLC_Editor/blob/master/editor/dialogs/ArduinoUploadDialog.py at line 598
Reply 0 0
Aurentaico
Hello @thiagoralves, I got problem on openplc ediyor while I am trying to upload the code to the board and the error is like that 
[203128%2F8781332%2Fmceclip0]

What can cause this error? I check the file btw and the file looks like this. While I am trying to compile the code on the editor it works fine I got this error only when I tried to update this code to the board.
Reply 0 0
Aurentaico
Hello @thiagoralves, I got problem on openplc ediyor while I am trying to upload the code to the board and the error is like that 
[203128%2F8781332%2Fmceclip0]

What can cause this error? I check the file btw and the file looks like this. While I am trying to compile the code on the editor it works fine I got this error only when I tried to update this code to the board.
Never mind fixed that.
Reply 0 0
Aurentaico
Hello @thiagoralves. I want to share a progress with you the function blocks are almost done. I need to as couple questions. I am trying to understand that how can I write hexadecimal values on openplc editor or how can I turn them in hexadecimal format in function this is my first question and the second one is. I have an isse when I set the function body diagram like in the picture. I just send the data twice and it stops sending it. I tried to add delay in my function but it does not working well I gues there is an overflow issue is happenning do you have any suggestion that I can do and also when I write the datas to the buffer it sends it different format not the numbers that I set in openplc editor. Here are the codes first one is the code that I write for openplc editor and the second is example arduino code for the same CAN communication 
#include "STM32_CAN.h"

extern "C" void *init_stm32can(int);
extern "C" bool write_stm32can(bool, uint32_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
extern "C" uint64_t read_stm32can();

STM32_CAN Can( CAN1, ALT_2 );  //Use PD0/1 pins for CAN1.
static CAN_message_t CAN_TX_msg;

void *init_stm32can(int baudrate)
{   
  Can.begin();
  Can.setBaudRate(baudrate);
  
}

bool write_stm32can(bool pin_en, uint32_t id ,uint8_t d0, uint8_t d1 ,uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, 
                                                                    uint8_t d6, uint8_t d7)
{  
  delay(500);
  
  CAN_TX_msg.id = id;
  CAN_TX_msg.len = 8;
  CAN_TX_msg.buf[0] =  d0;
  CAN_TX_msg.buf[1] =  d1;
  CAN_TX_msg.buf[2] =  d2;
  CAN_TX_msg.buf[3] =  d3;
  CAN_TX_msg.buf[4] =  d4;
  CAN_TX_msg.buf[5] =  d5;
  CAN_TX_msg.buf[6] =  d6;
  CAN_TX_msg.buf[7] =  d7;
  
    if (int const rc = CAN.write(msg); rc < 0)
    {
        return 0;
    }
    return 1;
}

uint64_t read_stm32can()
{
    /*uint64_t data = 0;
    if (CAN.available())
    {
        CanMsg const msg = CAN.read();
        uint8_t payload[8] = {0};
        for(int i = 0; i < msg.data_length || i < 8; i++) {
            payload[i] = msg.data[i];
        }
        memcpy((void *)(&data), &payload, sizeof(payload));
    }
    return data;*/
}
This code below is example code 

/*
This is simple example to send random data to CAN bus in 20Hz rate, using delay (not recommended in real implementations).
*/

#include "STM32_CAN.h"


//STM32_CAN Can( CAN1, DEF );  //Use PA11/12 pins for CAN1.
//STM32_CAN Can( CAN1, ALT );  //Use PB8/9 pins for CAN1.
STM32_CAN Can( CAN1, ALT_2 );  //Use PD0/1 pins for CAN1.
//STM32_CAN Can( CAN2, DEF );  //Use PB12/13 pins for CAN2.
//STM32_CAN Can( CAN2, ALT );  //Use PB5/6 pins for CAN2
//STM32_CAN Can( CAN3, DEF );  //Use PA8/15 pins for CAN3.
//STM32_CAN Can( CAN3, ALT );  //Use PB3/4 pins for CAN3

static CAN_message_t CAN_TX_msg;

void setup() {
  Serial.begin(115200);
  Can.begin();
  Can.setBaudRate(250000);  //250KBPS
  pinMode(PG_9,OUTPUT);
  //Can.setBaudRate(500000);  //500KBPS
  //Can.setBaudRate(1000000);  //1000KBPS
}

void loop() 
{
  uint8_t Counter = 0;

  while(1)
  {
    if (Counter > 255){ Counter = 0;}
    delay(1000);
    CAN_TX_msg.id = (0x1A5);
    CAN_TX_msg.len = 8;
    CAN_TX_msg.buf[0] =  0x03;
    CAN_TX_msg.buf[1] =  0x41;
    CAN_TX_msg.buf[2] =  0x11;
    CAN_TX_msg.buf[3] =  Counter;
    CAN_TX_msg.buf[4] =  0x00;
    CAN_TX_msg.buf[5] =  0x00;
    CAN_TX_msg.buf[6] =  0x00;
    CAN_TX_msg.buf[7] =  0x00;
    digitalToggle(PG_9);
    Can.write(CAN_TX_msg);

    CAN_TX_msg.id = (0x1AC32CF5);
    CAN_TX_msg.flags.extended = 1;  // To enable extended ID.
    CAN_TX_msg.len = 8;
    CAN_TX_msg.buf[0] =  0x03;
    CAN_TX_msg.buf[1] =  0x41;
    CAN_TX_msg.buf[3] =  0x21;
    CAN_TX_msg.buf[4] =  0x00;
    CAN_TX_msg.buf[5] =  Counter;
    CAN_TX_msg.buf[6] =  0x00;
    CAN_TX_msg.buf[7] =  0xFF;

    Can.write(CAN_TX_msg);

    CAN_TX_msg.id = (0xA63);
    CAN_TX_msg.len = 8;
    CAN_TX_msg.buf[0] =  0x63;
    CAN_TX_msg.buf[1] =  0x49;
    CAN_TX_msg.buf[2] =  0x11;
    CAN_TX_msg.buf[3] =  0x22;
    CAN_TX_msg.buf[4] =  0x00;
    CAN_TX_msg.buf[5] =  0x00;
    CAN_TX_msg.buf[6] =  Counter;
    CAN_TX_msg.buf[7] =  0x00;

    Can.write(CAN_TX_msg);

    CAN_TX_msg.id = (0x23);
    CAN_TX_msg.flags.extended = 0;  // Back to standard ID.
    CAN_TX_msg.len = 8;
    CAN_TX_msg.buf[0] =  0x03;
    CAN_TX_msg.buf[1] =  0x41;
    CAN_TX_msg.buf[2] =  0x11;
    CAN_TX_msg.buf[3] =  0x33;
    CAN_TX_msg.buf[4] =  0x00;
    CAN_TX_msg.buf[5] =  0x00;
    CAN_TX_msg.buf[6] =  0x00;
    CAN_TX_msg.buf[7] =  Counter;

    Can.write(CAN_TX_msg);

    CAN_TX_msg.id = (0x55);
    CAN_TX_msg.len = 8;
    CAN_TX_msg.buf[0] =  0x03;
    CAN_TX_msg.buf[1] =  0x44;
    CAN_TX_msg.buf[2] =  0x31;
    CAN_TX_msg.buf[3] =  0x53;
    CAN_TX_msg.buf[4] =  0x00;
    CAN_TX_msg.buf[5] =  Counter;
    CAN_TX_msg.buf[6] =  0x00;
    CAN_TX_msg.buf[7] =  0x00;

    Can.write(CAN_TX_msg);
    Serial.print("Sent: ");
    Serial.println(Counter, HEX);
    Counter++;
  }
}
 
Reply 0 0
thiagoralves
PLC code is cyclic. All your blocks are constantly called on every PLC cycle (the default is every 20ms). In your code, there is no logic to prevent them from being called again. This means that every 20ms your init block and your write block are being called and executed. A 500ms delay in your write block is messing up the entire PLC cycle and make it run back to back. You should never add delays to blocks.
 
Check the MQTT blocks and use them as example. All blocks have a boolean input that controls their execution (when to init, when to write, when to read). 
Reply 0 0
Reply