Modbus Master and Slave on STM32F103 and UNO

Generic boards that are not Maple or Maple mini clones, and don't contain the additional USB reset hardware
tyro
Posts: 10
Joined: Thu Jan 19, 2017 3:44 pm

Re: Modbus Master and Slave on STM32F103 and UNO

Postby tyro » Tue Jan 24, 2017 8:51 pm

I have tested these updates today. Compiled on Mega2560 it works correctly, gets response from slave, and after compiling to Maple Mini it don't. There is something more hidden here, I do not know where to search :(

victor_pv
Posts: 957
Joined: Mon Apr 27, 2015 12:12 pm

Re: Modbus Master and Slave on STM32F103 and UNO

Postby victor_pv » Tue Jan 24, 2017 8:59 pm

tyro wrote:I have tested these updates today. Compiled on Mega2560 it works correctly, gets response from slave, and after compiling to Maple Mini it don't. There is something more hidden here, I do not know where to search :(


Probably you already know, but the stm32 uses 3.3v IO, while the uno uses 5V. Are you connecting the boards directly during testing? if so, you probably need a level converter.
If using RS485 chips, make sure the one used in the stm32 end can work with 3.3V in the MCU side.

tyro
Posts: 10
Joined: Thu Jan 19, 2017 3:44 pm

Re: Modbus Master and Slave on STM32F103 and UNO

Postby tyro » Tue Jan 24, 2017 9:06 pm

victor_pv wrote:Probably you already know, but the stm32 uses 3.3v IO, while the uno uses 5V. Are you connecting the boards directly during testing? if so, you probably need a level converter.
If using RS485 chips, make sure the one used in the stm32 end can work with 3.3V in the MCU side.


I have made simple test several times - made a simple "chat" between Maple Mini and Arduino Mega2560 using Serial1 connections between them. At first I tried to connect them directly - Tx<->Rx, Rx<->Tx and that worked. Then tried to add level shifter and that worked again. I tried both ways using Maple Mini to Modbus communication too. I saw that comunication was establihed (Data sending and retrieving interval intervals was short), but I think there were bad formatted data sent, so modbus did not returned correct data (or did not responded if CRC was bad). Now I am installing Modbus server on my PC. I will try to check data receiving on serial and compare from both Arduino and STM32.

tyro
Posts: 10
Joined: Thu Jan 19, 2017 3:44 pm

Re: Modbus Master and Slave on STM32F103 and UNO

Postby tyro » Tue Jan 24, 2017 9:34 pm

And here are my first results. I am trying to connect to slave with ID 170, read 1 holding register (operation 0x03) at address 0

Arduino2560 Serial call look like:

Code: Select all

AA 20 00 04 57 FA



and Maple Mini Serial call look like:

Code: Select all

AA 03 00 00 00 04 5D D2


Maple mini data is different. And my Slave server is responding to it :-o And ignores arduino (not replying with data). Hm. Will test few more things
Last edited by tyro on Tue Jan 24, 2017 9:43 pm, edited 1 time in total.

stevestrong
Posts: 736
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Modbus Master and Slave on STM32F103 and UNO

Postby stevestrong » Tue Jan 24, 2017 9:36 pm

Be carefull by endianess, CRC is the best place where it can go wrong.
Can you show us your code?

Mega2560 train is wrong, it does not contain 0x03. Maple mini looks good.

tyro
Posts: 10
Joined: Thu Jan 19, 2017 3:44 pm

Re: Modbus Master and Slave on STM32F103 and UNO

Postby tyro » Tue Jan 24, 2017 10:53 pm

stevestrong wrote:Can you show us your code?


Yes, sure.

Code: Select all

#include <ModbusMaster.h>

ModbusMaster bms;

void setup()
{
  Serial1.begin(115200);

  // communicate with Modbus slave ID 170 over Serial (port 0)
  bms.begin(170, Serial1); //start modbus master connection to slave with ID 170 using Serial1
}

void loop()
{
  uint8_t j, result;
  uint16_t cell;

  result = bms.readHoldingRegisters(0, 1); //read 1 ragister starting at address 0
  if (result == bms.ku8MBSuccess) {
    cell = bms.getResponseBuffer(0);   //read 0 position from result buffer
  }

  delay(10000);
}


before i was testing using Serial (from USB cable). Now I have connected FTDI to my STM32 and trying Serial1 as I am planing to use. And result is totally different. STM32 send something like

Code: Select all

AA
03
or

Code: Select all

AA 03
only.
Tried Software serial too - no results - incorrect data "DA 10 08 04 06 CD F4" and it even changes forward and back one or other symbol - absolutely not reliable.

victor_pv
Posts: 957
Joined: Mon Apr 27, 2015 12:12 pm

Re: Modbus Master and Slave on STM32F103 and UNO

Postby victor_pv » Wed Jan 25, 2017 5:22 pm

tyro wrote:
stevestrong wrote:Can you show us your code?


Yes, sure.

Code: Select all

#include <ModbusMaster.h>

ModbusMaster bms;

void setup()
{
  Serial1.begin(115200);

  // communicate with Modbus slave ID 170 over Serial (port 0)
  bms.begin(170, Serial1); //start modbus master connection to slave with ID 170 using Serial1
}

void loop()
{
  uint8_t j, result;
  uint16_t cell;

  result = bms.readHoldingRegisters(0, 1); //read 1 ragister starting at address 0
  if (result == bms.ku8MBSuccess) {
    cell = bms.getResponseBuffer(0);   //read 0 position from result buffer
  }

  delay(10000);
}


before i was testing using Serial (from USB cable). Now I have connected FTDI to my STM32 and trying Serial1 as I am planing to use. And result is totally different. STM32 send something like

Code: Select all

AA
03
or

Code: Select all

AA 03
only.
Tried Software serial too - no results - incorrect data "DA 10 08 04 06 CD F4" and it even changes forward and back one or other symbol - absolutely not reliable.


Check if you can find a version of the library tested in the DUE or the Teensy 3.x, there may be some problems with the types and the endianness as Steve pointed out, and anyone using them in Due or Teensy should have worked those out already.

tyro
Posts: 10
Joined: Thu Jan 19, 2017 3:44 pm

Re: Modbus Master and Slave on STM32F103 and UNO

Postby tyro » Wed Jan 25, 2017 6:40 pm

victor_pv wrote:Check if you can find a version of the library tested in the DUE or the Teensy 3.x, there may be some problems with the types and the endianness as Steve pointed out, and anyone using them in Due or Teensy should have worked those out already.


Ok, I will try to do that.

Update on my tests. When using Mega2560 as TX (tried Serial, Serial1, SoftwareSerial) and STM32 as RX (receiving on Serial1 and printing by Serial) every time I see message correctly: AA 03 00 00 00 01 9D D1

When switching them Mega2560 as RX (receiving on Serial1 and printing by Serial) and STM32 as TX (tried Serial, Serial1, SoftSerialSTM32) the results are strange. Only Serial (using USB cable) gives correct result (AA 03 00 00 00 01 9D D1). When using other Serial ports or types I receive corrupted data. The data is different on every call - varies one or other HEX value on each call. I receive two identical messages next one to other very rare despite TX is making same call each time. As last I would try to use Serial. Serial pins on Arduino are digital0 and digital1. Which pins is on Maple Mini? I cannot find that info. Maybe someone can share this information?

victor_pv
Posts: 957
Joined: Mon Apr 27, 2015 12:12 pm

Re: Modbus Master and Slave on STM32F103 and UNO

Postby victor_pv » Wed Jan 25, 2017 7:30 pm

tyro wrote:
victor_pv wrote:Check if you can find a version of the library tested in the DUE or the Teensy 3.x, there may be some problems with the types and the endianness as Steve pointed out, and anyone using them in Due or Teensy should have worked those out already.


Ok, I will try to do that.

Update on my tests. When using Mega2560 as TX (tried Serial, Serial1, SoftwareSerial) and STM32 as RX (receiving on Serial1 and printing by Serial) every time I see message correctly: AA 03 00 00 00 01 9D D1

When switching them Mega2560 as RX (receiving on Serial1 and printing by Serial) and STM32 as TX (tried Serial, Serial1, SoftSerialSTM32) the results are strange. Only Serial (using USB cable) gives correct result (AA 03 00 00 00 01 9D D1). When using other Serial ports or types I receive corrupted data. The data is different on every call - varies one or other HEX value on each call. I receive two identical messages next one to other very rare despite TX is making same call each time. As last I would try to use Serial. Serial pins on Arduino are digital0 and digital1. Which pins is on Maple Mini? I cannot find that info. Maybe someone can share this information?


This page shows the pinout. The stm32 has several uarts, it shows the pins for each of them:
http://docs.leaflabs.com/static.leaflabs.com/pub/leaflabs/maple-docs/0.0.12/hardware/maple-mini.html#master-pin-map

messaf
Posts: 7
Joined: Wed Jan 18, 2017 10:55 am

Re: Modbus Master and Slave on STM32F103 and UNO

Postby messaf » Sun Jan 29, 2017 10:27 am

Hi guys!
I added a little library for working with STM32F103 (Library :: ModbusMaster Author :: Doc Walker), I hope he will not swear. :D
Everything is working very well now. I checked on two boards Arduino (slave) and Blue Pill Stm32F103 (master). Here are links to files:
ModbusMaster_STM32_RS-485
ModbusSlave_ArduinoUNO_RS-485


Return to “Generic STM32F103”

Who is online

Users browsing this forum: MSNbot Media and 2 guests