Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I write and read data from serial port simultaneously in VC++ MFC? Can I use multi threading if I can then how? please help me.I am waiting for your reply.please please......

At first thanks for your reply, actually I connect my PC to Serial Port through serial cable and other end of Serial cable connected with FPGA Board(Hardware) then I send data to the serial port and in response from board I get data back.But this sending and receiving operation should be happens simultaneously.
Posted
Updated 19-Dec-12 2:50am
v2
Comments
Jochen Arndt 19-Dec-12 9:30am    
I saw that you have updated your answer. As already noted, sending and receiving can be done simultaneously when using overlapped I/O and/or threads. But this is not necessary when the protocol defines that responses are not send until the complete request has been transferred. However, overlapped I/O and/or threads should be used to avoid blocking the GUI (main) thread of your application.

Your question and provided information is still too general. Even if you would specify all necessary information (flow control / used status lines, protocol: are data text or binary, how is the end of a request/response detected, max. data length, ...), you will not get an answer here. Because this can't be implemented with a few lines of code.

I will update my answer with some more information about serial communication functions.

1 solution

To read and write simultaneously you should use overlapped I/O. Then the read and write calls will not block but return immediately. End of the I/O operation is indicated by events. See Overlapped I/O and I/O Manager Driver Developer Tips[^] in the MSDN.

I recommend the usage of worker threads because you can wait for the completion of I/O and detect specific serial events like status line changings and errors (see SetCommMask() function). Using two threads for reading and writing or only one handling all depends on the specific communication protocol and data flow (e.g. if there is some kind of acknowledgement).

Serial I/O, especially when overlapped and using threads, is an advanced topic. You should read about overlapped I/O and worker threads. Then look for some articles here and elsewhere in the web for examples. You may find one that can be used for your implementation with minor changings. Finding one that completely fits your requirements is rather unlikely.

If you have specific questions about a topic or need help with your code, come back here again. At the moment, your question is too complex to answer it in short.

[UPDATE]
Some information on serial communication:

  • Use CreateFile(), ReadFile(), WriteFile(), and CloseHandle() to perform I/O operations.
  • See Communications Functions[^] for a list of serial communication specific functions.
  • Use at least SetCommState() after opening the port to pass settings.
  • Read about overlapped I/O (see above link).
  • Read about worker threads.
  • Use SetCommMask() and WaitCommEvent() inside threads to detect and handle specific events.
  • Use WaitForMultipleObjects() inside threads to wait for events.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900