Menu

.NET RTU Client (2.1)

Ilgrank
2016-04-30
2016-06-13
  • Ilgrank

    Ilgrank - 2016-04-30

    Hello everyone
    I'm trying to use EasyModbus .NET as an RTU client but I really can't figure out a couple of things:
    Loading the library works fine: (Using Powershell but should not matter much)
    Add-Type -Path '.\EasyModbus.dll'
    as does creating the object. According to
    http://easymodbustcp.net/modbusclient-methods
    i should be able to create an RTU client using:
    $modbusclient = [EasyModbus.ModbusClient]::new("COM3")
    but this does not work, as it always returns a TCP client:

    Connected : False
    IPAddress : 127.0.0.1
    Port : 502
    UDPFlag : False
    UnitIdentifier : 0
    Baudrate : 9600
    Parity : Even
    StopBits : One
    ConnectionTimeout : 500

    btw this:
    $modbusclient = [EasyModbus.ModbusClient]::new()
    or this..
    $modbusclient = [EasyModbus.ModbusClient]::new("127.0.0.1",502)
    do work as intended, but even if I can set the properties of Baudrate, the connection remains TCP.

    I'm also able to set the protocol this way,
    $modbusproto = [EasyModbus.ModbusProtocol+ProtocolType]::ModbusRTU
    but I don't understand where to tell the client to use this protocol.
    also I can't figure out the difference between
    [EasyModbus.ModbusProtocol+ProtocolType] and [EasyModbus.ModbusProtocol]

    Can someone please shed some light? Thanks!
    Thanks!

     

    Last edit: Ilgrank 2016-05-01
  • Rossmann Engineering

    Hello,

    to use Modbus RTU, it is important to create the Client Object using the constructor "ModbusClient(string ipAddress, int port)". Although all properties for Modbus TCP are still present, Modbus RTU is used, ad only properties for Modbus RTU connection are relevant.

    Constructors "ModbusClient( string serialPort)" and ModbusClient( ) are using Modbus TCP

     
  • Ilgrank

    Ilgrank - 2016-05-02

    Sorry but I don't get you.
    If I initialize the client this way:

    Add-Type -Path .\EasyModbus.dll
    $mbc=[EasyModbus.ModbusClient]::new("COM3")
    $mbc.Baudrate=2400
    $mbc.Parity="none"
    $mbc.ReadInputRegisters(1,1)
    

    I get a "Connection Error", even tho COM3 is readily accessible.

    I've downloaded the only .NET module available on the download page.. is there a separate RTU module elsewhere?
    Thanks!

     

    Last edit: Ilgrank 2016-05-02
    • Rossmann Engineering

      Call "Connect()" Method to open the serial port.

       
  • Ilgrank

    Ilgrank - 2016-05-03

    Thanks Stefan! It was it!
    Now I can read correctly!
    About the values returned.. I expected two floats, but I received a two INTs(?) array which have different values than expected.
    Again, many thanks for your help

     
  • Ilgrank

    Ilgrank - 2016-05-03

    To be more specific, the read should return two 16 bits registers, using other tools for example,here I have
    bytes: 4362 09E9 -> 226,03871

    but issuing

    $mbc.ReadInputRegisters(30001,2)

    I have in return:

    12544
    571

    I'm surely missing something here.. but I'm close :)
    thanks!

     

    Last edit: Ilgrank 2016-05-03
  • Ilgrank

    Ilgrank - 2016-05-05

    ..please, I'm just asking for a little bit of support.
    If there was some RTU example around, I'd follow it, but RTU seem to be not too well documented..
    Thanks!

     
    • Rossmann Engineering

      Sorry for late anwer.

      Do you use the latest Version 2.1? Baucause in Previous Versions, there was a Problem changing the Baurate.
      Modbus RTU uses even parity and 9600 baud by default.

       
  • Ilgrank

    Ilgrank - 2016-05-19

    Hi Stefan
    Sorry for the double post, I had fear of having been unnoticed :)

    Thanks for your reply
    yes, I'm on 2.1 and as per my post of 2n of May, I do initialize the client in this way:

    Add-Type -Path .\EasyModbus.dll
    $mbc=[EasyModbus.ModbusClient]::new("COM3")
    $mbc.Baudrate=2400
    $mbc.Parity="none"
    $mbc.Connect()
    $mbc.ReadInputRegisters(30001,2)
    

    but alas, I do get that 'strange' values (other sw tools are reading correctly, so I know my Modbus device is answering with the right data)

    Many thanks

     

    Last edit: Ilgrank 2016-05-19
    • Rossmann Engineering

      Which tool do you use as Slave device? Is it available for free?

       
      • Ilgrank

        Ilgrank - 2016-05-22

        Hi Stefan
        It's a hardware (ie: non software) device.
        It is a energy meter.
        But please tell me any way I can be useful in testing it
        thanks!

         
        • Rossmann Engineering

          Hello,

          just released a Version with some Bugfixes. Could you please try?

           
  • Ilgrank

    Ilgrank - 2016-05-30

    Hi Stefan
    Thanks for the update
    Installed the new (2.2) library, but I still get values I don't understand:

    PS C:\modbus> Add-Type -Path .\EasyModbus.dll
    PS C:\modbus> $mbc=[EasyModbus.ModbusClient]::new("COM3")
    PS C:\modbus> $mbc.Baudrate=2400
    PS C:\modbus> $mbc.Parity="none"
    PS C:\modbus> $mbc.Connect()
    PS C:\modbus> $mbc.ReadInputRegisters(30001,2)
    12544
    571
    

    this does NOT mean they're wrong.. just maybe they need some sort of.. translation?
    I expected something like
    4362 09E9
    in return (it is a live voltage reading, and should output two 16 bit register representing a float from 220Volts to 240Volts.. in the above example (4362 09E9) -> 226,03871V

    Many thanks

     
    • Rossmann Engineering

      Hi,

      is there any possibility to have a look via teamviewer or something similar?

       
  • Ilgrank

    Ilgrank - 2016-06-06

    Thanks for you offer Stefan, I've sent you a private message
    thanks!

     
  • Ilgrank

    Ilgrank - 2016-06-13

    Stefan was so kind and he spent a good part of the morning helping me find out what I was doing wrong.
    If someone is having my same problem:
    1st: you need to set the UnitIdentifier!
    I had erroneously tought the default was 1, but it is not.
    2nd. If you have, like me, a device with no parity, you'll have to step back 1 unit.. ie: if you need to read input register 30001, you'll need to enter 0 (ZERO)
    if you need to read 30029,you'll need to enter 28.

    Finally, here's the working code for the EASTRON SDM220 energy meter:

    $mbc=[EasyModbus.ModbusClient]::new("COM3")
    $mbc.Baudrate=2400
    $mbc.Parity="none"
    $mbc.UnitIdentifier=1
    $mbc.connect()
    $mbc.ReadInputRegisters(0,2)
    [EasyModbus.ModbusClient]::ConvertRegistersToFloat($mbc.ReadInputRegisters(0,2),1)
    

    Thanks again Stefan!

     
  • Anonymous

    Anonymous - 2018-08-23
    Post awaiting moderation.

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.