Showing results for 
Search instead for 
Did you mean: 

How to implement MODBUS protocol communication using UART in STM32L151RCT6 MCU

Associate III


I am using the STM32L151RCT6 in STM32cube IDE, i need to create MODBUS protocol communication between MCU and other board as slave..

Please suggest me any related link for implement this..

it will be helpful for me...



I am trying to pass float value from the MODBUS.

requirement is like i need to read adc data and after that i am converting it into voltage which is of float value .

That float value i need to map in MODBUS holding register and need to pass the float values from Modbus..

I am not getting how to manage the floats in the MODBUS register map , how to read float values from holding register and send???

Can you help me to fix this issue?

You don't have to worry about the mantissa and the exponent. Treat a float as a 4-byte integer by changing its type. In C language it is referred as type casting. An example is given below.

float x,y;

unsigned short int register1,register2;

register1= (unsigned short int)((unsigned int)x & 0xFFFF0000) >> 16; //(16 bit MSB)

register2= (unsigned short int)((unsigned int)x & 0x0000FFFF); //(16 bit LSB)

To recreate the float number, do the following:

y =(float) (((unsigned int)register1<<16) | (unsigned int)register2);

In my opinion this should work, but I haven't checked it out.

In (unsigned int)x casting a float to integer will get an integer part of float value. One can get a binary value by using address of a variable with ((uint16_t*)&x)[0] and ((uint16_t*)&x)[1] . But even better is a union type, which is dedicated for such purposes:


basically my sensor is 7  in 1 soil sensor to read the sensor data i am using the rs485 so at first i used to arduino it was worked so i need some support for interfacing the sensor for the stm32 b-l475e-iot01a ,where the sensor working on the modbus portocol