CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    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.

  2. #2
    Join Date
    Apr 2007
    Posts
    162

    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.

  3. #3
    Join Date
    Jan 2013
    Posts
    7

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

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Asynchronous reading

    Quote Originally Posted by emidevil View Post
    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.

  5. #5
    Join Date
    Jan 2013
    Posts
    7

    Re: Asynchronous reading

    thank yout

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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
  •  





Click Here to Expand Forum to Full Width

Featured