Divers problemes avec NModbus4 library under CSharp with dot Net 4.8.1 when trying to create a Modbus Slave with Serial Port

19 views
Skip to first unread message

Martin Mayr

unread,
Apr 15, 2025, 5:34:40 AMApr 15
to NModbus
Hello 

I encounter some strange difficulties with the NModbus4 Library under C# - hope somebody can help me here.

I try to implement a test Form within  C# with Windows FormsApp in .Net4.8.1 environment and when I try to create a ModbusRtuSlave 
with the command 
slave = ModbusSerialSlave.CreateRtu(1, serialPort);
I get an exception:
System.InvalidCastException: 'Impossible d'effectuer un cast d'un objet de type 'System.IO.Ports.SerialStream' en type 'Modbus.IO.IStreamResource'.'
In the documentation I found there would be a command allowing to use the SerialPort as second parameter , but when looking in the sources only the Modbus.IO.IStreamResource can be used, but I can't figure out how to create a IStreamResouce compatible from my SerialPort BaseStream ?
Anybody has an explanation or help for me please ?
Best regards
Martin

PST

unread,
Apr 15, 2025, 6:50:54 AMApr 15
to NModbus
Hi Martin

Help
вторник, 15 апреля 2025 г. в 12:34:40 UTC+3, Martin Mayr:

mm...@mmm-it.com

unread,
Apr 16, 2025, 3:44:15 AMApr 16
to nmodbus...@googlegroups.com

Hello,

 

thanks for your response, unfortunately I still struggle with some details in my implementation, maybe you can help me once more:

 

I could connect the modbus now with your example but still don’t get the event rised for a Slave 1 Function 17 request … The Listen function is called in a parallel thread but I don’t get the

ModbusSlaveRequestReceived Event rised when my master sends the command. Since I have no access to the master sources (which are inside an application I didn’t make myself) I tried with polling and I see there is data coming from the master but I don’t get the Event from the ListenAsync() …. What could I have done wrong ?

 

In case I can send you my modbus slave test application project it’s a simple VS Studio application. Maybe you have another master to test with … ?

 

Maybe I just do something wrong with my Event Handler ?

 

Best regards and thanks a lot for your help.

 

 

    Martin

 

De : nmodbus...@googlegroups.com <nmodbus...@googlegroups.com> De la part de PST
Envoyé : mardi, 15 avril 2025 12:51
À : NModbus <nmodbus...@googlegroups.com>
Objet : Re: Divers problemes avec NModbus4 library under CSharp with dot Net 4.8.1 when trying to create a Modbus Slave with Serial Port

--
You received this message because you are subscribed to a topic in the Google Groups "NModbus" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nmodbus-discuss/Een-3eglmOc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nmodbus-discu...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/nmodbus-discuss/d1370564-be81-42e3-b26a-5ba16be275b2n%40googlegroups.com.

ООО "ЦНТ-Энерго"

unread,
Apr 16, 2025, 5:38:36 AMApr 16
to nmodbus...@googlegroups.com
Hi
function 17 ?
https://github.com/NModbus4/NModbus4/issues/132
==
testing function 03//06 ? ok

ср, 16 апр. 2025 г. в 10:44, <mm...@mmm-it.com>:
You received this message because you are subscribed to the Google Groups "NModbus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nmodbus-discu...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/nmodbus-discuss/!%26!AAAAAAAAAAAuAAAAAAAAAPwKS4TM/zREqR8qiefSXkEBANlTnCJhprtFudq2LHCBs8EBACQA//8AABAAAABfGUJfjYQfR7O2xLhSe7mYAQAAAAA%3D%40mmm-it.com.

mm...@mmm-it.com

unread,
Apr 16, 2025, 5:47:22 AMApr 16
to nmodbus...@googlegroups.com

Hello,

 

Function 17 should respond with the slave name  - but no event is raised for the  ModbusSlaveRequestReceived.

 

With Other functions I run into the same problem, no event is raised either …. Would you have a possibility to send me your private email ? I tried to send the project here but the zip files are refused to be attached ….

Maybe you can look in the sources if I do something fundamentally wrong ?

 

Best regards

 

Martin

 

 

 

De : nmodbus...@googlegroups.com <nmodbus...@googlegroups.com> De la part de ??? "???-??????"
Envoyé : mercredi, 16 avril 2025 11:38
À : nmodbus...@googlegroups.com

mm...@mmm-it.com

unread,
Apr 16, 2025, 5:54:49 AMApr 16
to nmodbus...@googlegroups.com

With function 3 – same reaction from the master who claims the slave failed to respond

 

 

    Martin M. Mayr

 

Description: Visio exported MMM-IT Logo Bitmap Drawing 148x85

 

     MMM-IT Sàrl

Ruelle des Oublis 1

   2523 Lignières

     Switzerland

 

Tel.    +41 (0)32 751 8008

Fax    +41 (0)32 751 8009

Mobil / WhatsApp +41 (0)79 501 8008

Skype : MartinMaximilianMayr

 

Email: mm...@mmm-it.com

Web:   www.mmm-it.com

           www.mmm-vision.com

 

De : nmodbus...@googlegroups.com <nmodbus...@googlegroups.com> De la part de mm...@mmm-it.com
Envoyé : mercredi, 16 avril 2025 11:47
À : nmodbus...@googlegroups.com
Objet : RE: Divers problemes avec NModbus4 library under CSharp with dot Net 4.8.1 when trying to create a Modbus Slave with Serial Port

image001.png
Reply all
Reply to author
Forward
0 new messages