Author Topic: Calling the modbusRTU anywhere else then from the mainform  (Read 1333 times)

Martinus

  • Newbie
  • *
  • Posts: 27
    • View Profile
Calling the modbusRTU anywhere else then from the mainform
« on: December 04, 2014, 10:43:28 AM »
Hello to everybody,

I have a lot working now and....its possible to let the servo drive do positionings and reference as wel as speed and jog profiles.

Now i'm trying to build some own function ( with a class) ...... with normal functions there is no problems.
But what i would like to have is the possibility to use the modbusRTU from my own class.vb

How would i go about this.

Many thanks for your answer you guys

greets

martinus


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5317
    • View Profile
    • AdvancedHMI
Re: Calling the modbusRTU anywhere else then from the mainform
« Reply #1 on: December 04, 2014, 02:02:19 PM »
There are a number of ways you can do this with different degrees of difficultly, flexibility, and re-usability.

The easiest is to directly reference the instance on the MainForm. For instance:

Dim MyValue as string
MyValue=MainForm.ModbusRTUCom1.Read("40001")

This method is the simplest, but is the most encouraged because you are creating dependencies that can easily break the application. For instance, what if the driver is removed from the MainForm or a different driver added.

Another method is to create an instance of the driver within your class. For example:

Dim MyDriverInstance as ModbusRTUCom
MyDriverInstance=New ModbusRTUCom
MyDriverInstance.PortName="COM1"
MyDriverInstance.BaudRate=19200
etc.....

A third method is to create a ComComponent property in your class and have the code that instantiates your class set the ComComponent property. You would then use the property to access the driver.


If you are creating a class with shared methods, then you can simply pass the driver instance as a parameter.

Public Shared Function MyFunction(ByVal driver as IComComponent)

Martinus

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Calling the modbusRTU anywhere else then from the mainform
« Reply #2 on: December 05, 2014, 01:32:37 AM »
Hello Archie,

Thanks for your answer.

I wil give it a try asap.

Wil let you know how it go's


greets

Martinus