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++;
}
}