From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Modbus Initialization and Termination

Hi all,

 

I am writing a program that requires multiple MODBUS read and write at different instances in the program.  I am using the MODBUS driver by NI.  However, I put a wrapper around the driver, so that it would be more geared toward my application (just a subvi with read, write, and close comm functions).  I have a question about different options in initialization and termination for the MODBUS communiction.  Please give me some pros and cons for each.  Thanks!  See below.

 

1. Initialize MODBUS before each read/write and close MODBUS after each read/write (all in one subvi)

    - pro: clean, since the user doesn't need to worry about initializing/closing comm (all in one subvi)

    - con: more overhead (not sure how much though), since I am opening/closing comm for every read/write operation.

2. Initialize MODBUS only for the first time that read/write is called without closing the communication after the read/write operation.  The user will have to remember to close out the comm at the end of the program.

    - pro: only one initialization and termination for the comm in program.  Decrease overhead (how much?).

    - con: it is easy to accidentally insert a termination in a subvi and forget about it, which create an error, since comm not longer exist.  It is easy to forget to close the comm. 

 

Yik

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 1 of 6
(2,966 Views)
Someone please help!!!
------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 2 of 6
(2,939 Views)

I generally use what you list as option 2.  Intialize at beginning of program.  Read/Write as necessary during program.  Close at end.  Why?  Because I am continually reading and writing to update information from the device, so I want less overhead. I make it as part of its own loop and will usually pass commands to it through a queue as part of a producer/consumer architecture.

 

You say you are worried about a user doing something wrong.  But a user should never be touching your underlying code.  How is a user going to "accidentally insert a termination ina subVI"?  You only need to worry about a programmer doing things correctly.  That is likely to be you, or maybe a colleague who works with you.  A programmer should be thoroughly testing their code to be sure it works, so even if that person does something wrong, they'll discover and fix it.  You shouldn't have to worry about a user doing something wrong because those details should never be visible to the user of your program.

Message 3 of 6
(2,933 Views)

I concur with Ravens Fan. Initialize at hte beginning. If you are worried about how other may use the VI(s) that you create, then add very specific usage information to the documentation.

 

I am a firm believer in fully documenting how something is supposed to be used.

 

    Rob

0 Kudos
Message 4 of 6
(2,925 Views)
In my recent MODBUS project I created an "Action Engine", essentially a functional global that encapsulates all the functions. The initialization case places the reference on a shift register where the other cases (write/read/close) can access them. Then you can place the vi where ever you need it, making sure to have the close as you exit your program, avoiding the risk of closing a needed reference. This means you don't have the overhead of opening/closing, which can be significant. You can then place the init in the beginning of your program (I have an "init vi" where I do much of this type of stuff near the beginning of my programs), and use the other functions where needed, since the reference is carried with the vi. I also make sure that the "close" function filters errors so that it will execute regardless of errors upstream. It is a good idea to pass the error through, though, for troubleshooting.
Putnam
Certified LabVIEW Developer

Senior Test Engineer North Shore Technology, Inc.
Currently using LV 2012-LabVIEW 2018, RT8.5


LabVIEW Champion



Message 5 of 6
(2,911 Views)

Thank you!  I agree with you all.

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 6 of 6
(2,896 Views)