Master/Slave Modbus (RTU/TCP) Library

I have trouble with at funktion from codeshare Master/Slave Modbus (RTU/TCP) Library

link: https://www.ghielectronics.com/community/codeshare/entry/880

I have worked with this library for some time now. I started making the master side of a Modbus. I therefore use ModbusMaster.cs and ModbusRtuInterface.cs. I have problems with the content of the buffer is not sent correctly!

Main program


            SerialPort ser = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
            ModbusRtuInterface mdb = new ModbusRtuInterface(ser);

            ModbusMaster Mastermdb = new ModbusMaster(mdb, 0);


            ser.Open();

            ushort[] data =  Mastermdb.ReadHoldingRegisters(0x28,0x0038,0x0004);


When i follow the “buffer” en debug-mode can i see that it is correct.(hex 28 03 00 38 00 04 crc)

ModbusRtuInterface.cs


 public void SendTelegram(byte[] buffer, short telegramLength)
      {
         var crc = ModbusUtils.CalcCrc(buffer, telegramLength - 2);
         buffer[telegramLength - 2] = (byte)(crc & 0x00ff);
         buffer[telegramLength - 1] = (byte)((crc & 0xff00) >> 8);

         // ticks and _NextSend are multiples of 100 ns
         var dt = _NextSend - DateTime.Now.Ticks;
         if (dt > 0)
         {
            Thread.Sleep(Math.Max(1, (int)dt/10000));
         }

         // clear buffers
         _Serial.DiscardInBuffer();
         _Serial.DiscardOutBuffer();

         // next send is 3.5 chars after the end of this telegram
         _NextSend = DateTime.Now.Ticks + (telegramLength * 2 + 7) * _HalfCharLength;
         _Serial.Write(buffer, 0, telegramLength); // <== Buffer is correct!!
      }

with Modbus Slave (from modbustools.com) are the received data (Hex 28 20 38 04 B8 FE)

I don´t understand why i don’t receive(hex 28 03 00 38 00 04 crc)???

To my knowledge (hex 28 03 00 38 00 04 crc) should be correct.
28 slave address
03 function code
00 38 register address
00 04 register count
Instead of the Modbus slave tool, can you read the data with an simple terminal program, which shows you the data received?

It only transmit correct data when set a breakpoint on


_Serial.Write(buffer, 0, telegramLength);

When i don´t have the break point i only transmit hex FF

It makes no sense.

When i make a breakpoint at _serial.write(…) are the buffer transmitted correctly.

When i remove the breakpoint at _serial.write(…) the transmitted data are FF.

Is there anyone who has experienced the same problem with serial.write?

I have a feeling I made a stupid mistake. :think:

These method uses a shred buffer that is used for sending and receiving.
The methods are not thread safe.
Do you have multiple threads which access the ModbusMaster object?

No i do not have multiple threads in the project.

Then it might be hardware related.
What hardware (and firmware) do you use?
Have you tried to just send something using the Serial object, before you hand it over to the ModbusRtuInterface?

And by the way: The default setup for a Modbus RTU connection should be:
8 Data Bits, Even Parity bit and 1 stop bit.
An alternative would be
8 Data Bits, no parity and 2 stop bits.
So in total a Modbus RTU Byte has always 11 bits (since there is also a strat bit you don’t can modify).
But that’s not an absolute must. I even bought professional Modbus Devices, which have no parity and 1 stop bit set by default.
But in fact the device configuration must be configured exactly like your Master.
I also had strange things when TX+ and TX- were exchanged on the RS485 bus.
Sometimes it looks like it’s working but the bits are inverse.