Difficulty with MODBUS project

Good afternoon people.
I'm developing a communication project using MODBUS.

Equipment/Software:
Mini Arduino
USB/RS232 TTL FTDI module
I2C LCD Display
PC Win10
Radzio!ModBus Master Software

During the project I encountered a situation that I couldn't understand.
To make it easier, I built a very small sketch to test the problem.

To test MODBUS I use the small Radzio!ModBus Master software.
Modbus Master Simulator - free software utility for testing Modbus slave devices

To use this soft just configure the serial and then click Open.
On the screen select "Lenght 8" and click on Connect.

As the sketcht does not send anything to the master, the ModBus message timeout msg will appear on the soft screen.

This soft sends values ​​as shown on the screen to the arduino, by serial each time
of the logic analyzer.

The soft also generates a log named " Transmission.log ", with the data that was sent,
in the folder where it was installed.
In this folder I have the values ​​sent by the soft equal to
"0x01 0x01 0x00 0x00 0x00 0x08 0x3D 0xCC".

With my sketch, I get these bytes, saved in an array and when the serial is empty,
I print these values ​​in Hexadecimal on an LCD.

The LCD should show the values ​​"1100083DCC", but it shows the values ​​"0083DFFFFFFCC110".

I couldn't understand the reason for this problem.

Thanks in advance.

Regards

RV mineirin.

#include <LiquidCrystal_I2C.h>                                        // Lib LCD I2C
LiquidCrystal_I2C lcd(0x39,  2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);       // Set the LCD I2C address
byte i = 0;                         // Count control
char received[8] ;                  // Array to save data from serial
bool rec = false;                   // Flux Control 
//-------------------------------------------------------------------------------------
void setup()
{
  Serial.begin(9600);               // Inicialise Serial
  lcd.begin(2, 16);                 // Inicialise LCD
  lcd.clear();                      // Clear LCD
}
//-------------------------------------------------------------------------------------
void loop()
{
  while (Serial.available() > 0)        // While data on Serialo
  {
    received[i] = Serial.read();        // Read data on array
    i++;                                // Increment count
    rec = true;                         // Sinalise data on array
  }
  if (rec == true)                      // If data on array
  {
    for ( int j = 0; j < 8; j++)        // Selecta 0 to 8 array cell
    {
      lcd.print(received[j], HEX);      // LCD display in HEX
    }
    rec = false;                        // Sinalise no data on array
    lcd.setCursor(0, 0);                // Select pos 0 line 0 LCD
  }
}

Read on the RX pin of the arduino.

I'll hazard a guess that somewhere in the code there is an issue between signed and unsigned values. I think that if 0xCC is stored in a signed 8 bit int, then it will be interpreted as a negative number. Maybe change:

char received[8] ;

to

uint8_t received[8];
1 Like

Thank you very much for your attention Mr. @markd833

Okay, this part Mr. was correct.
I changed the variable to: uint8_t received[8] ;
and the value was correct.

Now all that's left is to understand why the display values are like this:
"0083DCC110" instead of display like this: "1100083DCC".

PS:
I didn't restore the value of variable i at the end of printing.
How I put i = 0;
Now display like this:
"100083DCC0" instead of display like this: "1100083DCC".

It looks like the code has lost sync with the start of the message. If you reset your Arduino and then send 1 message, does it display the correct sequence?

Hi @markd833
Thanks again for your attention.
Even with the Arduino reset, the display showed the same value.
It only started to show correct when I used a delay of 10 ms before starting the display routine.

Thank you very much.

RV mineirin

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