Modbus communication not working properly

Hello,

I have written a code for modbus communication that uses Arduino serial and software serial library. However, I am facing two issues as below when the code is tried with ModRSsim2 software:

Before that my code simply reads few sets of registers from modbus slave device and forms a single string and sends to cloud.

Problem 1 : In my code, if I comment out the function GSM_Send_HTTP_Data(), then all modbus requests are sent and responses are received for it. However, the order in which the modbus request is sent, is not same as in my code.

Expected sequence :
Request 1 : 01,04,00,0D,00,10... :: Receive response for this packet

Request 2 : 01,04,00,96,00,01... :: Receive response for this packet

Request 3 : 01,04,00,99,00,01... :: Receive response for this packet

Request 4 : 01,04,00,9C,00,01... :: Receive response for this packet

Sequence in communication trace:
Request 1 : 01,04,00,0D,00,10... :: Receive response for this packet

Request 4 : 01,04,00,9C,00,01... :: Receive response for this packet

Request 3 : 01,04,00,99,00,01... :: Receive response for this packet

Request 2 : 01,04,00,96,00,01... :: Receive response for this packet

Problem 2 : If I call GSM_Send_HTTP_Data() in my code, then data exchange happens for only one modbus request 01,04,00,0D,00,10.... It should happen for all four.

I tried checking what is happening inside the function GSM_Send_HTTP_Data() function. I found that if I removed delays and software serial print (mySerial.print()) function statement, then I was able to get response for all four requests that I am sending.

I did not understand this behaviour. Could you please help me out with where I am lagging?

I have attached my newly implemented code and screenshot of communication trace from modRSsim2 software.

Please help me find out what is wrong where!

Please excuse me for blind delays. I have implemented a method to accept only valid data, based on expected number of response bytes. For now, no validation of CRC upon data receipt.

code.c.pdf (23.2 KB)

Please attach your code as a text file, it doesn't make sense to post pure text as PDF.

Never use a while(1) loop inside loop() and undermines the IDE basics.

Don't use the String class on AVR Arduinos as it fragments the RAM in no time.

while (!Serial.available());
if (Serial.available() == expRespLen)

Here you wait until any character is received. After that you only read the characters if the complete expected response is already in the buffer. Either wait until all expected characters are received or try to read until you got all.

I don't know what you think Serial.flush() is doing but probably it's not what actually happens. It simply waits until all characters in the write buffer are completely sent. As you use the method in the receive part you may think it clears the input buffer but that's wrong.

This is probably because you use the String class and the memory gets corrupted. This may also cause your problem1 given the posted code is actually on the Arduino.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.