Modbus Master and Slave on STM32F103 and UNO

messaf
Wed Jan 18, 2017 11:29 am
Good day!
I have a problem:
I use Arduino Ide and fees on STM32F103C8T6, you must establish a connection via the RS-485 between the Master (STM32F103) and slave (Arduino Uno). Use the library SimpleModbus NG (https://github.com/angeloc/simplemodbusng). Sketches are compiled without problems. To use an example from STM32F103 library (SimpleModbusMasterExample.ino) and use UART1 – Serial1 (PA9-TX1, PA10-PX1 and control of transmit / receive pin PA8 for the MAX485). For example UNO used SimpleModbusSlaveExample.ino and Serial (default TX, PX and pin 2 – transmit / receive control).
So, the problem is that the UNO does not respond to requests STM32. Master returns a timeout error. But the fact is that if the master flash sketch in Arduino Mega 2560 is a problem in the communication between master and slave is not there, everything works, and the data is transmitted, and even tried to slave (UNO) to connect to a PC using an app for Windows “Modbus Master”- problems with this too does not arise.
Please answer, what could be the problem?

stevestrong
Wed Jan 18, 2017 12:41 pm
Try to connect serial 1 of F103 with the PC over RS485, see if the PC receives data when F103 is master and PC is slave.

You could post here your sketch.


tyro
Thu Jan 19, 2017 3:52 pm
I am using different library for Modbus Master functionality. Author is Doc Walker. It fully works and was tested using Arduino Uno, Arduino Mega, Arduino Nano. But when I compiled this code to use with Blue Pill Or Maple Mini device do not get answers from devices. Maybe it do not call proper calls too. Before compiiling I had problem with CRC functionality. Function “word” and data type “word” is not compatible with STM32F103, so I updated using examples how to change word to different types found on forums. I think there may be problem errors comes from.

So, I would be really interested to find solution to make Maple Mini work as Modbus Master. Otherwise I will have to add something like Attiny85 between Modbus slave device and Maple mini to play role of translator.


stevestrong
Thu Jan 19, 2017 7:47 pm
I have 2 blue pills communicating with each other over RS485 without any special lib. It is just using the normal serial communication and one control signal for direction.
The possible problem you can have is that the oroginal RS485 adapters use chips that are working with 4.5V and above. So it will most probably not work with 3.3V from the F103 board. I solved the problem by replacing the onboard RS485 chip with the 3.3V working ADM3485.

messaf
Mon Jan 23, 2017 2:50 am
Hello guys. Thanks for answers! Am sorry for not having replied.
I’ve already figured out in his problemme, it appears in the source code of the library Modbus Master function of sending the package: void sendPacket (unsigned char bufferSize). So pay attention to this code:
void sendPacket(unsigned char bufferSize)
{
digitalWrite(TxEnablePin, HIGH);

for (unsigned char i = 0; i < bufferSize; i++)
Serial2.write(frame[i]);

Serial2.flush();

// allow a frame delay to indicate end of transmission
delayMicroseconds(T3_5);

digitalWrite(TxEnablePin, LOW);

previousTimeout = millis(); // initialize timeout delay
}


tyro
Mon Jan 23, 2017 9:28 pm
messaf wrote:Could you share the code with the problem “word” and data type “word” ?

stevestrong
Mon Jan 23, 2017 10:26 pm
Regarding overloaded “send()”.
I suppose you know how many bytes or words do you want to send.
So just cast the passed parameters/constants like:
send( (uint8_t)0x5a );

tyro
Tue Jan 24, 2017 9:06 am
As I returned to this library code I saw that there is type “word” inside. And this function uses one more too:
void ModbusMaster::send(uint8_t data)
{
send(word(data));
}

RogerClark
Tue Jan 24, 2017 10:16 am
Its generally not advisable to use types like “word” if you need cross platform compatibility, as the meaning seems to vary depending on the processor architecture / data width.

better to use uint16_t etc


tyro
Tue Jan 24, 2017 10:33 am
RogerClark wrote:Its generally not advisable to use types like “word” if you need cross platform compatibility, as the meaning seems to vary depending on the processor architecture / data width.

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
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 :(

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.

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:
AA 20 00 04 57 FA


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
Tue Jan 24, 2017 10:53 pm
stevestrong wrote:Can you show us your code?

victor_pv
Wed Jan 25, 2017 5:22 pm
tyro wrote:stevestrong wrote:Can you show us your code?

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.

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.

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

tyro
Sun Jan 29, 2017 12:27 pm
messaf wrote: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

stevestrong
Sun Jan 29, 2017 3:42 pm
Can you tell us what exactly was the problem, how did you solve it?

messaf
Mon Jan 30, 2017 2:54 am
stevestrong wrote:Can you tell us what exactly was the problem, how did you solve it?

stevestrong
Mon Jan 30, 2017 7:18 am
The best would be to share the code on github, rather than in a zip package.

messaf
Tue Jan 31, 2017 2:26 am
stevestrong wrote:The best would be to share the code on github, rather than in a zip package.

messaf
Tue Jan 31, 2017 2:30 am
I am interested in this question (although it is not in this thread): How to configure Pligin Arduino for Eclipse that would use all the libraries for STM32 boards, many did not try to set up – I have nothing turns out … Prompt please, or may have some sort of already ready manul on this topic?

stevestrong
Tue Jan 31, 2017 8:07 am
Here is a possibility: http://wiki.stm32duino.com/index.php?ti … th_Eclipse

messaf
Tue Jan 31, 2017 10:53 am
stevestrong wrote:Here is a possibility: http://wiki.stm32duino.com/index.php?ti … th_Eclipse

zouk199
Tue Apr 25, 2017 9:07 am
hi sir

i am doing f103 with enc28j60 to become modbus tcp.

do u have any library for modbus tcp for stm32.

with regards

zouk


stevestrong
Tue Apr 25, 2017 9:44 am
@zouk199,
stop multiple-posting, your issue will not be solved if you post in every possible thread.
If you have already Enc26 working on F103, then ModbusIP (link from your other post) should work out of the box.
Just try one of those examples.

zouk199
Fri May 05, 2017 3:36 am
hi there

is there any way to do uno as master and stm32 f103 as slave??? :)


vary
Fri Jun 16, 2017 6:05 am
Hi,
A useful work. I am getting the following error in Arduino 1.8.2

sketch\ModbusMaster.h:54:53: fatal error: libmaple/usart.h: No such file or directory

#include <libmaple/usart.h> // Modified Egor Orlenok


kiloWATT21
Tue Jul 03, 2018 9:30 am
Hi,

is there ModBus RTU library for use between STM32s? I tried SimpleModbus with no luck.


Leave a Reply

Your email address will not be published. Required fields are marked *