-
February 4th, 2013, 02:01 PM
#1
Asynchronous reading
Hey everyone.
I am new to windows programming, in particular Visual C++, as I am a long time web developer.
I am developing a windows forms app in express 2010, and I am struggling to figure out how to do something.
I have a simple form that upon a button click, will go out to the serial port and read the serial port and display it to a text box. It works as coded.
The problem I have is I don't want to have to click the button each time, and would rather have the method just continuously run asynchronously, and update the text box whenever new data comes in.
Right now, if I run a do while loop, it basically freezes up my form as the entire app is waiting for the button click event to finish.
Is there a way for me to call a method asynchronously, and get info back, when it receives new data?
I know in some languages there are things like delegates and async function calls, but I am very new to VC++.
Any help would be appreciated.
Thanks.
-
February 5th, 2013, 09:09 AM
#2
Re: Asynchronous reading
Set up a timer (SetTimer)...it will read the port every so many milliseconds and you can update using that data.
Last edited by zapper222; February 5th, 2013 at 09:11 AM.
-
February 5th, 2013, 10:21 AM
#3
Re: Asynchronous reading
sir, can you tell me how to post a new topic here? i want to ask about what i should do for my Gravitational Search algorithm distance, a way of computing it for solving 0/1 multidimensional knapsack ... please..
-
February 5th, 2013, 09:44 PM
#4
Re: Asynchronous reading
For serial port reading, I'd recommend going the Overlapped I/O route and do all the port reading from a secondary thread. Search bing or google for serial port class implementations that will do this for you.
While you can use the polling Timer approach, I wouldn't recommend it because the Timer approach uses windows messages and message queue. If the timer polling occurs in the main UI thread, then user operations such as moving the mouse or resizing a window can interrupt the polling resulting in a loss of serial port data.
Of course you can put the timer into a second thread, but if you do this you might as well take it one step further and eliminate the polling all together by going with the Overlapped I/O route.
-
February 5th, 2013, 09:48 PM
#5
Re: Asynchronous reading
Originally Posted by emidevil
sir, can you tell me how to post a new topic here? i want to ask about what i should do for my Gravitational Search algorithm distance, a way of computing it for solving 0/1 multidimensional knapsack ... please..
You can't post a new thread topic from inside an existing thread. So you need to back out into the forum the thread is in and then you'll see a "Post a new thread" button near the top left of the forum. For example, this thread is in the "Visual C++ Forum". Just click on the "Visual C++ Forum" breadcrumb at the top of this thread. That will take to you the forum, then just click on the "Post a new thread" button.
-
February 5th, 2013, 11:41 PM
#6
Re: Asynchronous reading
thank yout
-
February 7th, 2013, 10:07 AM
#7
Re: Asynchronous reading
Well, if it's a Windows Forms app, I'd recommend you make use of the SerialPort class' DataReceived event. That way the class will handle the asynchronuous operation internally for you, and you get the opportunity to pick up received data when it arrives.
However, in this case you have posted in the wrong forum section. Windows Forms apps are C++/CLI (as opposed to native C++), which is discussed in a separate forum section.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|