sontx/modbus

Modbus TcpRequest

Metsatonttu opened this issue ยท 2 comments

Hi, your libraries looks amazing. ๐Ÿ‘

I cant figure out how to make TcpRequest, can you give me any advice/hint?

Hi,

I was checking this today

my test code of this, does only work once
first sending, seems invalid,
second sending is okay,
third sending sends only some parts

my example as so far

hope it helps you, a little

using System;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using CommandLine;
using Modbus.Core;
using Modbus.Core.Converters;
using Modbus.Core.DataTypes;
using Modbus.Core.Streams;

namespace Modbus.Terminal
{
internal class Program
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct ResponseFunc03
{
public byte numberOfBytes;

  [Endian(Endianness.BigEndian)]
  public short value;

  [Endian(Endianness.BigEndian)]
  public ushort dotPosition;

  public short padding1;
  public short padding2;
}

private static void Main(string[] args)
{
  var options = new NetworkOptions();
  //--address=192.168.1.158 --port=502 --interval=500
  if (!Parser.Default.ParseArguments(args, options))
    return;

  for (int x = 0; x <= 20; x++)
  {
    TcpClient client = new TcpClient(options.Address, options.Port);
    client.ReceiveTimeout = 1000;
    client.SendTimeout = 1000;

    // The socket will linger for 10 seconds after Socket.Close is called.
    //LingerOption lingerOption = new LingerOption(true, 10);
    //SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
    //client.LingerState = lingerOption;

    var stream = new TcpStream(client);
    stream.ReadTimeout = 500;
    stream.WriteTimeout = 500;

    ModbusTcpSession session = new ModbusTcpSession(new ModbusProtocolImpl(stream), 1);

    //string strLine;
    for (int t = 0; t < 2; t++)
    {
      if (!client.Connected)
      {
        Console.WriteLine("not connected to socket anymore");
        break;
      }

      RequestFunc03 func03 = new RequestFunc03();
      func03.firstRegisterAddress = 0;
      func03.numberOfRegisters = 4;

      var response = session.SendRequest<ResponseFunc03>(3, func03);

      if (response != null)
      {
        StringBuilder builder = new StringBuilder();
        builder.Append("RX : ");
        for (int i = 0; i < response.ResponseBytes.Length; i++)
        {
          builder.Append(response.ResponseBytes[i].ToString("x2"));
          builder.Append(" ");
        }

        //builder.Append("\r\n");

        Console.WriteLine(builder.ToString());

        //session.Dispose();
        //break;
      }
      else
      {
        Console.WriteLine("ERROR NOTHING RECEIVED");
        //break;
      }
      Thread.Sleep(500);
    }
    //response.Data
  }
  Console.Read();
}

Hi Hossein and others,
I stopped using this library and I am using EasyModbusTCP.4.5.1 instead
because I have no time left to found out, why I encountered this problem
regards,
dan