Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

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

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
judico
Asked:
judico
  • 5
  • 4
1 Solution
 
drichardsCommented:
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
 
judicoAuthor Commented:
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
 
drichardsCommented:
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
[Video] Create a Disruption-Free Workspace

Open offices have their challenges. And Sometimes, it's even hard to work at work. It's time to reclaim your office and create a disruption-free workspace. With the MB 660, you can:

-Increase Concentration
-Improve well-being
-Boost Productivity

 
judicoAuthor Commented:
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
 
drichardsCommented:
>> 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
 
judicoAuthor Commented:
OK, I'll try that but I don't know how to close the port while I'm in temperature.h.
0
 
drichardsCommented:
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
 
judicoAuthor Commented:
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
 
judicoAuthor Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now