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
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). 
Okay how can I run my conf just once and the write keep ruinning I check the mqtt and I am going to try to apply in on my system
Reply 0 0
thiagoralves
About numeric representation on IEC 61131-3, check this: https://www.fernhillsoftware.com/help/iec-61131/common-elements/literals-numeric.html
Reply 0 0
Aurentaico
Hello @thiagoralves. I tried to make it my system as MQTT but I am getting same error. It just sends twice than the whole systems got stuck again. Is there any way that I can get hexadecimal value from user to write it in buffer and how can I debug my system because when I run my system in OpenPLC editor without uploading a board. It runs without any problem but It runs in dummy code but I want to run my arduino code in plc is that possible? 
[203128%2F8781661%2Fmceclip0]

This is my header file that runs for the boards and in write body I am trying to check when the enable pin is true or false.

//my_custom_library.h – this file contains the C code for the TEST block defined in the “My Custom Library”

// STM32CAN

typedef struct {
  // FB Interface - IN, OUT, IN_OUT variables
  __DECLARE_VAR(BOOL,EN)
  __DECLARE_VAR(BOOL,ENO)
  __DECLARE_VAR(BOOL,SETUP_BLOCK)
  __DECLARE_VAR(LINT,BR)
  __DECLARE_VAR(BOOL,DONE) 
  // FB private variables - TEMP, private and located variables
} STM32CAN_CONF;

typedef struct {
  // FB Interface - IN, OUT, IN_OUT variables
  __DECLARE_VAR(BOOL,EN)
  __DECLARE_VAR(BOOL,ENO)
  __DECLARE_VAR(BOOL,EN_PIN)
  __DECLARE_VAR(UDINT,ID)
  __DECLARE_VAR(USINT,D0)
  __DECLARE_VAR(USINT,D1)
  __DECLARE_VAR(USINT,D2)
  __DECLARE_VAR(USINT,D3)
  __DECLARE_VAR(USINT,D4)
  __DECLARE_VAR(USINT,D5)
  __DECLARE_VAR(USINT,D6)
  __DECLARE_VAR(USINT,D7)
  __DECLARE_VAR(BOOL,DONE) 
  // FB private variables - TEMP, private and located variables
} STM32CAN_WRITE;

typedef struct {
  // FB Interface - IN, OUT, IN_OUT variables
  __DECLARE_VAR(BOOL,EN)
  __DECLARE_VAR(BOOL,ENO)
  __DECLARE_VAR(LWORD,DATA)
 
  // FB private variables - TEMP, private and located variables
} STM32CAN_READ;

#include <stdbool.h>
void *init_stm32can(int);
bool write_stm32can(uint32_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
uint64_t read_stm32can();

//definition of blocks
static void STM32CAN_CONF_init__(STM32CAN_CONF *data__, BOOL retain) {
  __INIT_VAR(data__->EN,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->ENO,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->SETUP_BLOCK,__BOOL_LITERAL(FALSE),retain)
  __INIT_VAR(data__->BR,250000,retain)
  __INIT_VAR(data__->DONE,__BOOL_LITERAL(FALSE),retain)
}

// Code part
static void STM32CAN_CONF_body__(STM32CAN_CONF *data__) {
  // Control execution
  if (!__GET_VAR(data__->EN)) {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(FALSE));
    return;
  }
  else {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(TRUE));
  }
  if(!__GET_VAR(data__->SETUP_BLOCK)){
    init_stm32can(__GET_VAR(data__->BR));
    __SET_VAR(data__->,SETUP_BLOCK,,__BOOL_LITERAL(TRUE));
    __SET_VAR(data__->,DONE,,__BOOL_LITERAL(TRUE));
  }
  

  goto __end;

__end:
  return;
  }

// write single byte
//definition of blocks
static void STM32CAN_WRITE_init__(STM32CAN_WRITE *data__, BOOL retain) {
  __INIT_VAR(data__->EN,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->ENO,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->EN_PIN,__BOOL_LITERAL(FALSE),retain)
  __INIT_VAR(data__->ID,0,retain)
  __INIT_VAR(data__->D0,0,retain)
  __INIT_VAR(data__->D1,0,retain)
  __INIT_VAR(data__->D2,0,retain)
  __INIT_VAR(data__->D3,0,retain)
  __INIT_VAR(data__->D4,0,retain)
  __INIT_VAR(data__->D5,0,retain)
  __INIT_VAR(data__->D6,0,retain)
  __INIT_VAR(data__->D7,0,retain)
  __INIT_VAR(data__->DONE,__BOOL_LITERAL(FALSE),retain)
}

// Code part
static void STM32CAN_WRITE_body__(STM32CAN_WRITE *data__) {
  // Control execution
  if (!__GET_VAR(data__->EN)) {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(FALSE));
    return;
  }
  else {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(TRUE));
  }
  bool can_write = false;
  
  if (__GET_VAR(data__->EN_PIN)){
    
  can_write = write_stm32can(__GET_VAR(data__->ID),__GET_VAR(data__->D0),
    __GET_VAR(data__->D1), __GET_VAR(data__->D2),__GET_VAR(data__->D3),
      __GET_VAR(data__->D4), __GET_VAR(data__->D5),__GET_VAR(data__->D6),
        __GET_VAR(data__->D7));    

  }
  __SET_VAR(data__->,DONE,,can_write);
  
  goto __end;

__end:
  return;
  }

// read
//definition of blocks
static void STM32CAN_READ_init__(STM32CAN_READ *data__, BOOL retain) {
  __INIT_VAR(data__->EN,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->ENO,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->DATA,0,retain)
}
// Code part
static void STM32CAN_READ_body__(STM32CAN_READ *data__) {
  // Control execution
  if (!__GET_VAR(data__->EN)) {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(FALSE));
    return;
  }
  else {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(TRUE));
  }

  __SET_VAR(data__->,DATA,,read_stm32can());


  goto __end;

__end:
  return;
 }
 
This is my arduino code 

#include "STM32_CAN.h"

extern "C" void *init_stm32can(int);
extern "C" bool write_stm32can(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(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)
{  
  
  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(CAN_TX_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;*/
}
 
Please could you help me? Thank you

Reply 0 0
thiagoralves
Your conf block still runs every scan cycle. As I said on my previous post, you must add new inputs to your blocks to control when they should execute certain functions. For example, the MQTT_CONNECT has the CONNECT input that controls when the block should initiate a connection. The MQTT_SEND block has the SEND input that controls when the block should send a message, etc...
 
You can debug the PLC running your code with the live monitoring (blue bug icon on the toolbar). Make sure you've enabled Modbus TCP or RTU for that to work. Also, remove your delays from your block code, otherwise it will get the runtime stuck and nothing will work - including the live monitoring.
Reply 0 0
Aurentaico
I added these inputs and test it still I got problems. And can I see serial prints on debug terminal or do I need to do something else? 

[203128%2F8781716%2Fmceclip0]

//my_custom_library.h – this file contains the C code for the TEST block defined in the “My Custom Library”
 
// STM32CAN
 
typedef struct {
  // FB Interface - IN, OUT, IN_OUT variables
  __DECLARE_VAR(BOOL,EN)
  __DECLARE_VAR(BOOL,ENO)
  __DECLARE_VAR(BOOL,CONF)
  __DECLARE_VAR(LINT,BR)
  __DECLARE_VAR(BOOL,DONE) 
  // FB private variables - TEMP, private and located variables
} STM32CAN_CONF;
 
typedef struct {
  // FB Interface - IN, OUT, IN_OUT variables
  __DECLARE_VAR(BOOL,EN)
  __DECLARE_VAR(BOOL,ENO)
  __DECLARE_VAR(BOOL,EN_PIN)
  __DECLARE_VAR(UDINT,ID)
  __DECLARE_VAR(USINT,D0)
  __DECLARE_VAR(USINT,D1)
  __DECLARE_VAR(USINT,D2)
  __DECLARE_VAR(USINT,D3)
  __DECLARE_VAR(USINT,D4)
  __DECLARE_VAR(USINT,D5)
  __DECLARE_VAR(USINT,D6)
  __DECLARE_VAR(USINT,D7)
  __DECLARE_VAR(BOOL,DONE) 
  // FB private variables - TEMP, private and located variables
} STM32CAN_WRITE;
 
typedef struct {
  // FB Interface - IN, OUT, IN_OUT variables
  __DECLARE_VAR(BOOL,EN)
  __DECLARE_VAR(BOOL,ENO)
  __DECLARE_VAR(LWORD,DATA)
 
  // FB private variables - TEMP, private and located variables
} STM32CAN_READ;
 
 
void *init_stm32can(int);
bool write_stm32can(uint32_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
uint64_t read_stm32can();
 
//definition of blocks
static void STM32CAN_CONF_init__(STM32CAN_CONF *data__, BOOL retain) {
  __INIT_VAR(data__->EN,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->ENO,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->CONF,__BOOL_LITERAL(FALSE),retain)
  __INIT_VAR(data__->BR,250000,retain)
  __INIT_VAR(data__->DONE,__BOOL_LITERAL(FALSE),retain)
}
 
// Code part
static void STM32CAN_CONF_body__(STM32CAN_CONF *data__) {
  // Control execution
  if (!__GET_VAR(data__->EN)) {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(FALSE));
    return;
  }
  else {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(TRUE));
  }
  if(__GET_VAR(data__->CONF)){
  init_stm32can(__GET_VAR(data__->BR));
  }
  
  __SET_VAR(data__->,DONE,,__BOOL_LITERAL(TRUE));
 
  goto __end;
 
__end:
  return;
  }
 
// write single byte
//definition of blocks
static void STM32CAN_WRITE_init__(STM32CAN_WRITE *data__, BOOL retain) {
  __INIT_VAR(data__->EN,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->ENO,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->EN_PIN,__BOOL_LITERAL(FALSE),retain)
  __INIT_VAR(data__->ID,0,retain)
  __INIT_VAR(data__->D0,0,retain)
  __INIT_VAR(data__->D1,0,retain)
  __INIT_VAR(data__->D2,0,retain)
  __INIT_VAR(data__->D3,0,retain)
  __INIT_VAR(data__->D4,0,retain)
  __INIT_VAR(data__->D5,0,retain)
  __INIT_VAR(data__->D6,0,retain)
  __INIT_VAR(data__->D7,0,retain)
  __INIT_VAR(data__->DONE,__BOOL_LITERAL(FALSE),retain)
}
 
// Code part
static void STM32CAN_WRITE_body__(STM32CAN_WRITE *data__) {
  // Control execution
  if (!__GET_VAR(data__->EN)) {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(FALSE));
    return;
  }
  else {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(TRUE));
  }
  bool can_write = false;
  
  if (__GET_VAR(data__->EN_PIN)){
  
can_write = write_stm32can(__GET_VAR(data__->ID),__GET_VAR(data__->D0),
__GET_VAR(data__->D1), __GET_VAR(data__->D2),__GET_VAR(data__->D3),
__GET_VAR(data__->D4), __GET_VAR(data__->D5),__GET_VAR(data__->D6),
__GET_VAR(data__->D7));
 
  }
  __SET_VAR(data__->,DONE,,can_write);
  
  goto __end;
 
__end:
  return;
  }
 
// read
//definition of blocks
static void STM32CAN_READ_init__(STM32CAN_READ *data__, BOOL retain) {
  __INIT_VAR(data__->EN,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->ENO,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->DATA,0,retain)
}
// Code part
static void STM32CAN_READ_body__(STM32CAN_READ *data__) {
  // Control execution
  if (!__GET_VAR(data__->EN)) {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(FALSE));
    return;
  }
  else {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(TRUE));
  }
 
  __SET_VAR(data__->,DATA,,read_stm32can());
 
 
  goto __end;
 
__end:
  return;
 }
//my_custom_library.h – this file contains the C code for the TEST block defined in the “My Custom Library”

// STM32CAN

typedef struct {
  // FB Interface - IN, OUT, IN_OUT variables
  __DECLARE_VAR(BOOL,EN)
  __DECLARE_VAR(BOOL,ENO)
  __DECLARE_VAR(BOOL,CONF)
  __DECLARE_VAR(LINT,BR)
  __DECLARE_VAR(BOOL,DONE) 
  // FB private variables - TEMP, private and located variables
} STM32CAN_CONF;

typedef struct {
  // FB Interface - IN, OUT, IN_OUT variables
  __DECLARE_VAR(BOOL,EN)
  __DECLARE_VAR(BOOL,ENO)
  __DECLARE_VAR(BOOL,EN_PIN)
  __DECLARE_VAR(UDINT,ID)
  __DECLARE_VAR(USINT,D0)
  __DECLARE_VAR(USINT,D1)
  __DECLARE_VAR(USINT,D2)
  __DECLARE_VAR(USINT,D3)
  __DECLARE_VAR(USINT,D4)
  __DECLARE_VAR(USINT,D5)
  __DECLARE_VAR(USINT,D6)
  __DECLARE_VAR(USINT,D7)
  __DECLARE_VAR(BOOL,DONE) 
  // FB private variables - TEMP, private and located variables
} STM32CAN_WRITE;

typedef struct {
  // FB Interface - IN, OUT, IN_OUT variables
  __DECLARE_VAR(BOOL,EN)
  __DECLARE_VAR(BOOL,ENO)
  __DECLARE_VAR(LWORD,DATA)
 
  // FB private variables - TEMP, private and located variables
} STM32CAN_READ;


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

//definition of blocks
static void STM32CAN_CONF_init__(STM32CAN_CONF *data__, BOOL retain) {
  __INIT_VAR(data__->EN,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->ENO,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->CONF,__BOOL_LITERAL(FALSE),retain)
  __INIT_VAR(data__->BR,250000,retain)
  __INIT_VAR(data__->DONE,__BOOL_LITERAL(FALSE),retain)
}

// Code part
static void STM32CAN_CONF_body__(STM32CAN_CONF *data__) {
  // Control execution
  if (!__GET_VAR(data__->EN)) {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(FALSE));
    return;
  }
  else {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(TRUE));
  }
  if(__GET_VAR(data__->CONF)){
  init_stm32can(__GET_VAR(data__->BR));
  }
  
  __SET_VAR(data__->,DONE,,__BOOL_LITERAL(TRUE));

  goto __end;

__end:
  return;
  }

// write single byte
//definition of blocks
static void STM32CAN_WRITE_init__(STM32CAN_WRITE *data__, BOOL retain) {
  __INIT_VAR(data__->EN,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->ENO,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->EN_PIN,__BOOL_LITERAL(FALSE),retain)
  __INIT_VAR(data__->ID,0,retain)
  __INIT_VAR(data__->D0,0,retain)
  __INIT_VAR(data__->D1,0,retain)
  __INIT_VAR(data__->D2,0,retain)
  __INIT_VAR(data__->D3,0,retain)
  __INIT_VAR(data__->D4,0,retain)
  __INIT_VAR(data__->D5,0,retain)
  __INIT_VAR(data__->D6,0,retain)
  __INIT_VAR(data__->D7,0,retain)
  __INIT_VAR(data__->DONE,__BOOL_LITERAL(FALSE),retain)
}

// Code part
static void STM32CAN_WRITE_body__(STM32CAN_WRITE *data__) {
  // Control execution
  if (!__GET_VAR(data__->EN)) {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(FALSE));
    return;
  }
  else {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(TRUE));
  }
  bool can_write = false;
  
  if (__GET_VAR(data__->EN_PIN)){
    
  can_write = write_stm32can(__GET_VAR(data__->ID),__GET_VAR(data__->D0),
    __GET_VAR(data__->D1), __GET_VAR(data__->D2),__GET_VAR(data__->D3),
      __GET_VAR(data__->D4), __GET_VAR(data__->D5),__GET_VAR(data__->D6),
        __GET_VAR(data__->D7));    

  }
  __SET_VAR(data__->,DONE,,can_write);
  
  goto __end;

__end:
  return;
  }

// read
//definition of blocks
static void STM32CAN_READ_init__(STM32CAN_READ *data__, BOOL retain) {
  __INIT_VAR(data__->EN,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->ENO,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->DATA,0,retain)
}
// Code part
static void STM32CAN_READ_body__(STM32CAN_READ *data__) {
  // Control execution
  if (!__GET_VAR(data__->EN)) {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(FALSE));
    return;
  }
  else {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(TRUE));
  }

  __SET_VAR(data__->,DATA,,read_stm32can());


  goto __end;

__end:
  return;
 }
 
Could help me to fix that problem it is almost done that's why I am pushing that much. Thank you for your help @thiagoralves
Reply 0 0
thiagoralves
You can add Serial.print to the Arduino portion of your code and see them using any serial monitor. Just remember to add a serial.begin somewhere and disable Modbus rtu or that will interfere with the serial data.
Reply 0 0
Aurentaico
Is there any easy way to disable modbus rtu? @thiagoralves
Reply 0 0
Grz
Hello,

Yes you uncheck RTU mode in the program transfer window.
 
Best regard. 
Pierre 
Reply 0 0
Aurentaico
Hello,

Yes you uncheck RTU mode in the program transfer window.
 
Best regard. 
Pierre 
Hello, 

I look at it and found it but it is unchecked already. When I tried to open serial monitor of that com it gives an error. Access denied or Com port busy. What can cause this problem do you have any idea? 

Best regards,
Eray
Reply 0 0
thiagoralves
Hello, 

I look at it and found it but it is unchecked already. When I tried to open serial monitor of that com it gives an error. Access denied or Com port busy. What can cause this problem do you have any idea? 

Best regards,
Eray
This error means that another program is using that COM port. You must close whichever program is using the COM port first.
Reply 0 0
Aurentaico
Hello @thiagoralves. I manage to work with it. The blocks are working but there is only I problem left it is how can I change the clock configuration of my stm32 and I want to learn how many ms does generated pulse stays. I have an issue that my stm32_canwrite function is triggered every 0.5 ms 
[203128%2F8783082%2Fmceclip0]
 
Every 2 seconds it is sending the data needs to be sended but I don't understand why it is triggered every 0.5 ms. Here is my write init and write body could you please check them and tell me what can I do for it. I am trying to fix this error for 8 hours long but I couldn't find the problem maybe clock is the issue or maybe there is a mistake in my code. Thank you for now @thiagoralves. Waiting your response 
static void STM32CAN_WRITE_init__(STM32CAN_WRITE *data__, BOOL retain) {
  __INIT_VAR(data__->EN,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->ENO,__BOOL_LITERAL(TRUE),retain)
  __INIT_VAR(data__->EN_PIN,__BOOL_LITERAL(FALSE),retain)
  __INIT_VAR(data__->ID,0,retain)
  __INIT_VAR(data__->D0,0,retain)
  __INIT_VAR(data__->D1,0,retain)
  __INIT_VAR(data__->D2,0,retain)
  __INIT_VAR(data__->D3,0,retain)
  __INIT_VAR(data__->D4,0,retain)
  __INIT_VAR(data__->D5,0,retain)
  __INIT_VAR(data__->D6,0,retain)
  __INIT_VAR(data__->D7,0,retain)
  __INIT_VAR(data__->DONE,__BOOL_LITERAL(FALSE),retain)
}

// Code part
static void STM32CAN_WRITE_body__(STM32CAN_WRITE *data__) {
  // Control execution
  if (!__GET_VAR(data__->EN)) {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(FALSE));
    return;
  }
  else {
    __SET_VAR(data__->,ENO,,__BOOL_LITERAL(TRUE));
  }

  if (__GET_VAR(data__->EN_PIN)){
    
  can_write = write_stm32can(__GET_VAR(data__->ID),__GET_VAR(data__->D0),
    __GET_VAR(data__->D1), __GET_VAR(data__->D2),__GET_VAR(data__->D3),
      __GET_VAR(data__->D4), __GET_VAR(data__->D5),__GET_VAR(data__->D6),
        __GET_VAR(data__->D7));    
  __SET_VAR(data__->,DONE,,__BOOL_LITERAL(TRUE));
  }
  __SET_VAR(data__->,DONE,,__BOOL_LITERAL(FALSE));
  
  goto __end;

__end:
  return;
  }
 
 
Reply 0 0
Aurentaico
@thiagoralves I tried with functional body diagram and problem fixed but I guess while initializing the block it is runnig blocks because it is sending nonsense data for two times than it starts sending data that make sense. Does Openplc editor makes that kind of things?

[203128%2F8783171%2Fmceclip0]
Reply 0 0
thiagoralves
On your first example, I believe the issue might be that some of your blocks are taking too long to execute. OpenPLC tries to keep the scan cycle in sync, and that is how it keeps track of time. If your task is scheduled to run at every 20ms, but for whatever reason your scan cycle took longer than that (say 100ms for example), it will then compensate the next scan cycles running them back to back until the timing is in sync with the 20ms system ticks. So for example, in a 100ms interval you should run your 20ms task 5 times. But if your first cycle took 100ms to run, on the next 20ms cycle your task will be called 4 more times to compensate for being late, so that on average, your scan cycle is still 20ms (100ms + 1ms + 1ms + 1ms + 1ms + 16ms / 6 cycles = 20ms per cycle). Things can add up the more late you get. That's why I emphasized how important it is to remove all and every delay call as that locks the scan cycle and makes it late.
 
This can also be the problem you're facing with your second example. In your code it seems that you haven't really defined when DONE is set. You only set it to true when the block returns. Maybe you should investigate when the actual initialization of the CAN interface is complete to finally set the DONE pin to true, and then only write messages when the DONE pin for the CAN_CONF block is true. You're trusting your timing to timers, but things can vary significantly depending on your hardware. Maybe one board will take 100ms to initialize CAN, where another board will take 400ms. Hardcoding the time like that is not wise and will lead you to trouble, like trying to send a message before the hardware is actually initialized.
Reply 0 0
Aurentaico
Thank you for your comment @thiagoralves. So could you send me an example diagram if there is a chance I know you dont have the blocks but can you draw something to the paper or on the photo that I uploaded. I mentioned earlier. I don’t know plc a lot thats why I am trying to figure it out. I will change it in code to check wheather it is returning true or false that is  the part that I can do it easily. Thank you so much again
Reply 0 0
thiagoralves
You must fix your blocks first before trying to change anything on the diagram. As I mentioned on my previous post, you must evaluate if the block is taking too long to execute and also trigger the DONE pin when it is done.
Reply 0 0
Reply