PIC to PC using MFC Visual Studio through serial communication
The project is about reading data from PIC and display the data on the dialogue created by the MFC GUI of Visual studio 2010. There are 4 data need to be displayed (2 weight and 2 angle). the data will be display on the edit control box on the dialogue.
I have try to solve the COMPort and Readdata issues many times, but I'm fail. I have read many sources and implement the source codes... They are never work....
The detail about the issues that I need to solve is clearly mentioned on the main dialogue.cpp. Take a look to an uploaded zip file that I have attached.
Re: PIC to PC using MFC Visual Studio through serial communication
To me, the trouble starts whenever the programmer attempts to do any time sensitive I/O in the main UI thread. Using a timer is popular, but what many folks don't realize is a timer running in the main UI thread is at the mercy of what the user is doing at the time. A user simply moving a window can cause enough of an interruption that data is lost. In my opinion, you'll have better results if you set up the SerialPort class to use I/O completion and move it into a worker thread (then use the typical approaches to synchronize data between worker and ui threads).
Re: PIC to PC using MFC Visual Studio through serial communication
I/O completion ports are a bit of a big cannon to shoot at a mosquito. And even if you do need these, you can make I/O completion ports work on your main UI thread with a minimum (or even no) undesired sideeffects. But this may not be easily possible depending on what UI framework you pick (typically it'll involve rewriting the main messageloop with a more elaborate one that also accounts for the IO completing alongside messages being posted). This complexity in the messageloop may be desirable over the complexities of having a Multithreaded solution with everything that comes with it. (doing multithreading right is not easy)
The simplest solution is blocking calls in a console app. In this case, the console manager takes care of the "keeping your app responsive".
If you need a GUI app, then the simplest (recommended) solution is blocking calls on a separate thread. You can do blocking calls in the UI thread, but it's a bad idea for many reasons.
Asynchronous IO (overlapped IO or completion ports) can be done in a limited amount of threads or in a separate thread per connection. Pooling is the best design, but it's got more complex management.
Given the description of the OP, blocking calls on a worker thread is probably the best fit for your needs.
Alternatively, for serial comms, there are also libraries that handle the IO for you and put the data in a list, you then just need to periodically check the list for any new input.
Note that there is a problem with the code examples for non-overlapped com io. ReadFile can return non-zero to indicate successful read but not have read any characters as the number of chars actually read is 0. So to know that a char has been read from the comm port you need to check that ReadFile() returns a non-zero value AND that the number of chars read is also non-zero. The example code doesn't do this check for number of chars read and so if you use the example code as posted you can get chars repeated.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.