Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Aug 03 09:39
    Hoan96 synchronize #184
  • Jun 20 23:19
    linbo690 opened #185
  • Apr 14 04:31
    Hoan96 synchronize #184
  • Mar 13 07:55
    Hoan96 synchronize #184
  • Mar 13 07:44
    Hoan96 synchronize #184
  • Mar 12 04:39
    Hoan96 opened #184
  • Jan 04 16:35
    MPapst commented #150
  • Jan 04 16:32
    ChangeTheCode commented #150
  • Jan 04 15:46
    MPapst commented #150
  • Jan 04 14:55
    ChangeTheCode commented #150
  • Jan 04 14:33
    MPapst commented #150
  • Jan 04 14:24
    ChangeTheCode commented #150
  • Dec 07 2020 09:14
    pvakharwala commented #183
  • Dec 07 2020 09:08
    Fonta88 commented #183
  • Dec 05 2020 16:02
    pvakharwala commented #183
  • Dec 05 2020 16:01
    pvakharwala commented #182
  • Dec 03 2020 11:47
    Fonta88 commented #183
  • Oct 14 2020 14:13
    stefurios edited #183
  • Oct 14 2020 14:12
    stefurios edited #183
  • Oct 14 2020 14:11
    stefurios edited #183
lonchorojas
@lonchorojas
Hello, can I used modbus4 for a RS485 network?
Dmitry Turin
@Maxwe11
@lonchorojas sure
Ghost
@ghost~561e7019d33f749381a9572c
Often, when shutting my slave down, I get a System.ArgumentException from a worker thread. Is there any info on how to avoid or deal with this? When I try to open the .chm file, all pages appear blank. Any advice would be appreciated.
Richard Lawley
@richardlawley
The problem with the CHM file will be because the file is blocked on your computer. Find the file in Windows Explorer, right-click it, Properties, then click Unblock at the bottom.
Ghost
@ghost~561e7019d33f749381a9572c
Thank you, I can see it now.
Ghost
@ghost~561e7019d33f749381a9572c
Reviewing docs.. doesn't seem to be any api for specifying slave exception handling. Example code I have (previous version of nmodbus) was setting an exception handler on the slave. Seems this has been removed from nmodbus4?
Richard Lawley
@richardlawley
Can you point at the previous example?
Ghost
@ghost~561e7019d33f749381a9572c
not sure if you mean reference the example here or point to the old dll in my present project. I'm about to try the latter, but had been hoping to use the presently maintained version of nmodbus. Seems like this would be a common problem.. start slave, run... , dispose slave, exception in thread!
Richard Lawley
@richardlawley
I'm not a maintainer, but have this library in production use (replacing the older non-maintained version) with no problems. You'll need to provide more details, such as a stack trace and a code sample.
Ghost
@ghost~561e7019d33f749381a9572c
When the exception occurs, there's no symbols, just assembly code. Reassuring that there shouldn't be odd issues like this. Best way to post a code sample here?
Ghost
@ghost~561e7019d33f749381a9572c
This is the entire slave minus the data writing/reading with the datastore. Test were run with this exact code. I get either the ArgumentException or a NullReference Exception on every test while the application is closing. For the test, I have a master running trying to do some reads periodically. Test case is to start the app, wait a few seconds, close the app. Code...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Modbus;
using Modbus.Device;
using Modbus.Data;
using Modbus.Message;
using System.Net;
using System.Net.Sockets;

namespace ModbusLib
{
    class Slave : Link
    {
        TcpListener slaveTcpListener;
        Modbus.Device.ModbusSlave slave;

        private byte SlaveID = 1;   // making this a non-config item for now

        public Slave(string aName, tMsgHandler aMsgHandler)
            :base(aName, aMsgHandler)
        {}

        public override void Start()
        {
            IPAddress ip = new IPAddress(0);

            if (IPAddress.TryParse(IpAddress, out ip) == false)
            {
                Error("Could not parse IP Address for link: " + Name);
                return;
            }

            try
            {
                slaveTcpListener = new TcpListener(ip, TcpPort);
            }
            catch (Exception e)
            {
                Error("Could not start modbus slave " + Name + ". Exception: " + e.ToString());
                return;
            }

            slaveTcpListener.Start();

            slave = Modbus.Device.ModbusTcpSlave.CreateTcp(SlaveID, slaveTcpListener);
            slave.DataStore = Modbus.Data.DataStoreFactory.CreateDefaultDataStore();

            slave.Listen();
        }
        protected override void Dispose(bool disposing)
        {
            if (slaveTcpListener != null)
            {
                slaveTcpListener.Stop();
                slave.Dispose();
            }
        }
    }
}
This message was deleted
Ghost
@ghost~561e7019d33f749381a9572c
Best I got for a stack trace for the ArgumentException. Looks to be from shutting it down while it was accepting the connection, but how do I make it shutdown gracefully anyway?
System.ArgumentException: The IAsyncResult object was not returned from the corresponding asynchronous method on this class.
Parameter name: asyncResult
   at System.Net.Sockets.Socket.EndAccept(Byte[]& buffer, Int32& bytesTransferred, IAsyncResult asyncResult)
   at System.Net.Sockets.Socket.EndAccept(IAsyncResult asyncResult)
   at Modbus.Device.ModbusTcpSlave.AcceptCompleted(IAsyncResult ar)
   at Modbus.Device.ModbusTcpSlave.<>c.<AcceptCompleted>b__17_0(IAsyncResult state)
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   at System.Net.ContextAwareResult.CompleteCallback(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.ContextAwareResult.Complete(IntPtr userToken)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
   at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
Ghost
@ghost~561e7019d33f749381a9572c
Lastly, I had removed slave = null; and slaveTcpListener=null; from the Dispose routine shown above, and I no longer have the NullReference exception showing up. Before the change it was happening about 1/3 of tests run.
Also, after talking with a coworker here, we think the exception handler setting on a slave that I referred to earlier was probably added to the old nmodbus library by an ex-coworker of ours.
Richard Lawley
@richardlawley
Got a bit mixed up before, I only use the master functionality. However, looking at your code I would try calling slave.Dispose() and not calling .Stop() on your listener. Looking at the code it looks like the Dispose handler takes care of cleaning up the listener.
Ghost
@ghost~561e7019d33f749381a9572c
Ah, you've nailed it! No more exceptions. Thank you very much. Previous version of library didn't properly disconnect without more stuff in the dispose (but then would exception quite a bit too). This version doesn't need it and properly disconnects on its own with no exceptions. Excellent.
David Barda
@davebarda
Hmm, if I disconnect the serial port, there is a reason why the slaves doesn't disconnect? should it be like that?
Kevin Luty
@kevlut
So, I sometimes get a IOException when communicating with a Modbus TCP device regarding to transaction id expected X. Transaction id received X-Y, using the original NModbus from the google repo... does NModbus4 fix that issue?
this doesn't seem too active, hoping you'll see this @Maxwe11
Kevin Luty
@kevlut
@davebarda if you disconnect the serialport, the physical communication line doesn't disappear, so the slaves don't techincally "disconnect" they simply just don't communicate.
Richard Lawley
@richardlawley
@kevlut, this behaviour occurs if your receive timeout is shorter than the timeout on the sender - your first request appears to timeout, but the reply is still waiting in the queue. On the second request, you receive the response to the first. I ran a private fork because of this situation for years, I put this enhancement into NModbus4 when it started up. See NModbus4/NModbus4#41 for details.
Kevin Luty
@kevlut
beautiful
do you recommend using SlaveBusyUsesRetryCount?
Richard Lawley
@richardlawley
Yes - that's another one I added because it caused me problems in the original fork.
Kevin Luty
@kevlut
@richardlawley so i get a bunch of, odd behavior, with the connected modbus tcp/ip device, are you familiar with wireshark and want to lend a helping hand? :D
Kevin Luty
@kevlut
blob
looks like a timeout occurs, then i bombard it, then it returns exception code, then it starts replying, then gets out of order again... then the connection just... closes/disconnects
Kevin Luty
@kevlut
.6: 11320000001b01031805dc0000000000030000000002b100000000000000000022
.182: 1133000000060103003f000c
.6: 11330000001b01031805dc0000000000030000000002b100000000000000000022
.182: 1134000000060103003f000c
.182: 1135000000060103003f000c
.182: 1136000000060103003f000c
.182: 1137000000060103003f000c1138000000060103003f000c
.6: 113600000003018306
.182: 1139000000060103003f000c
.6: 11370000000301830611380000000301830611340000001b01031805dc0000000000030000000002b10000000000000000002211350000001b01031805dc0000000000030000000002b100000000000000000022
.6: 11390000001b01031805dc0000000000030000000002b100000000000000000022
this is over wifi