Menu

Use of EasyModbus.dll with Powershell

amreagan
2017-08-08
2020-08-11
  • amreagan

    amreagan - 2017-08-08

    After reviewing https://sourceforge.net/p/easymodbustcp/discussion/general/thread/61859b68/ it appears that the following code should create a powershell object that can be used to communicate with ModBus devices over serial connections, but I am getting an error. I use powershell often, but have very limited experience using .net objects.

    $ModBusDll = 'C:\Scripts\Modbus\EasyModbus.dll'
    add-type -Path $ModBusDll
    $MBCli=[EasyModbus.ModbusClient]::new("COM1")

    $MBCli=[EasyModbus.ModbusClient]::new("COM1") returns the following error:

    Method invocation failed because [EasyModbus.ModbusClient] does not contain a method named 'new'.
    At line:4 char:1
    + $MBCli=[EasyModbus.ModbusClient]::new("COM1")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

    However, [EasyModbus.ModbusClient].GetMethods().Name returns a valid list of the methods for [EasyModbus.ModbusClient]. I'm also running Powershell elevated. Any suggestions for what I may be doing wrong?

    I have also verified that the com port can be used to communicate and read registers using another application.

     

    Last edit: amreagan 2017-08-08
  • amreagan

    amreagan - 2017-08-08

    This is strange, I can replicate this problem on Windows 7 box running .net 4.6 and Windows Management Framework 4 using either 32 bit or 64 bit versions of Windows 7, but I don't have this problem on Windows 10. The "new" method simply doesn't appear on Win 7.

     

    Last edit: amreagan 2017-08-08
  • amreagan

    amreagan - 2017-08-09

    I was able to work around this by upgrading the machine in use to Windows 10

     
    • Rossmann Engineering

      great to hear that you could make it work.

      I am not really familiar with Powershell. Maybe it is an issue with the .NET Framework it is using.

       
  • Anonymous

    Anonymous - 2018-10-03

    Yeah this issue is persistent. In order to get it to work with powershell on windows 7 you have to initialize the client as $MBClient = New-Object -TypeName EasyModbus.ModbusClient and then set the IP Address and Port from the propeties , otherwise on a Win10 machine you can just do $MBClient = [EasyModbus.ModbusClient]::new("192.168.40.112", 502)

     
  • Anonymous

    Anonymous - 2018-10-03

    [This is continuation of my above post with the Win 7 and Win10 examples] Or you can call the constructor on a Win7machine like so $MBClient = New-Object -TypeName EasyModbus.ModbusClient -ArgumentList "192.168.40.112",502. Both my Win10 and Win7 machines have all the same .NET libraries installed , but produce different behaviors.

     
  • Anonymous

    Anonymous - 2024-05-12
    Post awaiting moderation.
  • Anonymous

    Anonymous - 2024-05-12
    Post awaiting moderation.
  • Anonymous

    Anonymous - 2024-05-13
    Post awaiting moderation.

Anonymous
Anonymous

Add attachments
Cancel