cancel
Showing results for 
Search instead for 
Did you mean: 

MODBUS RTU reading problems

Lagodolio
Senior

I have a problem with modbus-rtu communication in my stm project.

The STM32F446RE is connected to a MAX485 board by UART1 to RO-DI pin and by a GPIO pin to RE-DE.

A typical instruction (variable:INSTRUCTION.array_instruction) is something like 01 06 26 04 00 57 + CRC16 (variable:cksum16).

Where:

01 slave address

06 write single instruction

26 04 my instruction

00 57 instruction value

xx xx CRC1

Here my programm:

HAL_GPIO_WritePin(MODBUS_DE_RE_GPIO_Port, MODBUS_DE_RE_Pin, GPIO_PIN_SET); //MAX485 write mode
 
HAL_Delay(DELAY_FOR_MODBUS);
 
  HAL_UART_Transmit(&huart1,(uint8_t*)&INSTRUCTION.array_instruction,6,1000);
 
  HAL_UART_Transmit(&huart1,(uint8_t*)&cksum16,2,1000);
 
 HAL_Delay(DELAY_FOR_MODBUS);

And all works great for all kind of write instructions (0x06).

The problems come with read function. 

After sending a request (01 03 00 00 00 01 +crc 84 0A) I read my port 

HAL_Delay(DELAY_FOR_MODBUS);
 
	HAL_GPIO_WritePin(MODBUS_DE_RE_GPIO_Port, MODBUS_DE_RE_Pin, GPIO_PIN_RESET); //MAX 485 read mode
 
  	HAL_Delay(DELAY_FOR_MODBUS);
 
		HAL_UART_Receive(&huart1,(uint8_t*)given_response.array_response,sizeof(response),3000);
 
	HAL_Delay(DELAY_FOR_MODBUS);

But "given_response.array_response" is always 00 00 00 00 00 00 00 and not 01 03 xx xx xx +crc16 xx xx 

I tried with greater DELAY_FOR_MODBUS value (now =10 for 19200 baud serial) and longer timeout, with no results.

Thanks a lot,

Mario 

2 REPLIES 2
Ozone
Lead II

Actually, I wonder if Cube/HAL can/does implement the RTU timing requirements and character timeouts in a sensible way. I suppose not. But I'm no Cube user.

I would suggest to check and verify the command reception down to interrupt handler level, perhaps with special instrumentation.

I would have started a Modbus RTU project without Cube, but that's my personal view.

AGold.1
Associate II

I have written a book on Modbus slave design using C targeted to STM32: https://www.amazon.com/dp/B086PNZF4W