Solved

A question to mrdtn: How does one close the port of the multimeter?

Posted on 2004-08-21
9
297 Views
Last Modified: 2008-03-06
mrdtn,

The code you provided which was rewritten for managed Visual C++ .NET by drichards seems to be working very well. Unfortunately, I still can only acquire data at 0.5s interval since other, larger intervals, cause delays and faulty readings for some reason. For now, having no choice, I will stay with this 0.5s interval.

I have now an additional question which we did not discuss before. How does one close the port? I would like to get out of the part of the program which acquires data, then do some other tasks and at some point go back and acquire a new set of data, unrelated to the first set. It seems that to do that one has to close the port (and then open it upon subsequent call to the device). Hope you’re around and can help me with this. Thanks in advance.

judico

0
Comment
Question by:judico
  • 5
  • 4
9 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 250 total points
ID: 11872968
You close the port with 'CloseHandle'.  In this case, add a method to the MultiMeter class:

    void Close();

Implement it like this:

void MultiMeter::Close()
{
    CloseHandle(m_hComm);
}

You'll have to figure out a good way to call it in your program - manually by a button click or automatically by handling some event in your program (closing the form, or a timer if you want it to just run for some fixed period).
0
 

Author Comment

by:judico
ID: 11874694
I placed your above code in MultiMeter.h. However, can't figure out how to close the port from the form temperature.h. In temperature.h I have #include "MultiMeter.h" but

private: System::Void temperature_Load(System::Object *  sender, System::EventArgs *  e)
             {
                  
MultiMeter::Close()

                 }

doesn't do any good.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11874974
You need a pointer to the MultiMeter object.  Also, closing the multimeter on the form load seems to be inappropriate unless this is a new form coming up after you're finished reading data.  Also, tou'll have to kill the timer so you don't try to read data after the meter is closed.  Otherwise, you'll have to add a state variable indicating whether the meter is open or not.  You could set m_hComm to 0 in CLose() and check if m_hComm is 0 before reading data.
0
 

Author Comment

by:judico
ID: 11875413
See, the idea is when the work in temperature.h is finished to go back to Form1.h by clicking on the X in the upper right corner. I don't want this going back to Form1.h to be done by pressing a special button ... That's why the form load ... I'll try to do it differently.

But, before that, I don't know how to set a pointer to MultiMeter object from temperature.h or from Form1.h (maybe the closing of the port should occur upon returning to Form1.h).
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 19

Expert Comment

by:drichards
ID: 11876079
>> when the work in temperature.h is finished to go back to Form1.h by clicking on the X in the upper right corner

Then you probably want to do it in the 'Closing' or 'Closed' event of the temperature form, not the 'Load' event.
0
 

Author Comment

by:judico
ID: 11876532
OK, I'll try that but I don't know how to close the port while I'm in temperature.h.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11878078
What's the relationship between Form1 and temperature?  You'll probably have to give temperature a reference to multimeter when you create temperature.  How do you create temperature?
0
 

Author Comment

by:judico
ID: 11878502
You know, my main problem is that when you rewrote into managed VC++ the program given by mrdtn, I treated it as a black box and wrote the rest of the code around it without getting into the details of the code you sent me. It works beautifully now as is (with the exception that I can't have intervals other than 0,5s, as well as this business with closing the port) but now I need to understand the nitty gritty of your code because I need to simplify it so that I can use it with a PIC device. I know how to do it in VB.NET with a special rs232comms.dll I have but I want to learn how to do it your way which I like better.

In the program that I have now, temperature.h, where the main activity in calling the device and plotting the data occurs, is called from Form1.h. When I finish with the tasks in temperature.h, I go back to Form1.h. This is the moment when I want to close the device. Because I may need later to go back into temperature.h and acquire a new set of data, unrelated to the first.

At present, if I want to take a new set of data (without exiting the program), I unplug and then plug back the USB-RS232 cable while in Form1.h and then get into temperature.h -- the data is acquired fine in this way. I want, however, to avoid that step of unplugging and again plugging the cable.

More importantly, I want to simplify your code, so that, as I mentioned, I can use it with a PIC device, instead of with the multimeter which we've been discussing so far.
0
 

Author Comment

by:judico
ID: 11879160
Now, I figured that out -- the closing of port part. I placed in MultiMeter.cpp the code you gave above for closing of the port:

void MultiMeter::Close()
{
    CloseHandle(m_hComm);
}

Then, in MultiMeter.h I placed:

public:
     void Close();

and finally I had to relinquish the idea of closing the port upon leaving temperature.h and in its stead I placed

mmeter->Close();

in a button which also kills the timer.

Thanks again for your help on this problem. I will probably open a separate question to discuss the code you translated from unmanaged to managed VC++.



P.S. I increased your points to 250. Please let me know if you have problems in getting the increased number of points.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Is it worth it to buy an Echo? In a word, yes! For me it was definitely worth it. I use mine on a daily basis. Prologue & Privacy At first, I was very skeptical about the Amazon Echo. In general, I don't like voice assistants. I don't li…
This paper addresses the security of Sennheiser DECT Contact Center and Office (CC&O) headsets. It describes the DECT security chain comprised of “Pairing”, “Per Call Authentication” and “Encryption”, which are all part of the standard DECT protocol.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now