Solved

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

Posted on 2004-08-21
9
303 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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
 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
security string in a noisy bar 5 87
USB ports 4 27
Looking for a network enabled locker remotely controlled like Amazon locker 2 79
Pressure Test 1 48
What do we know about Legacy Video Conferencing? - Full IT support needed! - Complicated systems at outrageous prices! - Intense training required! Highfive believes we need to embrace a new alternative.
Stuck in voice control mode on your Amazon Firestick?  Here is how to turn it off!!!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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