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)???