Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hey guys

Is it possible that two threads/processes do I/O through one RS232 port?
Does Microsoft Windows synchronize the RS232 port access with the threads?

In other words:
I do have a microcontroller which is connected to a PC using RS232.
The microcontroller sends every ASCII symbol back it receives.

On the PC, I have two threads running.
Thread/Process A sends ASCII "A" to the microcontroller and waits for a response.
Thread/Process B sends ASCII "B" to the microcontroller and waits for a response.

Can I assume that Thread A only receives "A"s back? In case Windows does not synchronize the port, there would be a possibility for Thread A to receive a "B".

Do you know of whether Windows synchronizes the RS232 access, so that two threads/processes can communicate "simultaneously" with e.g. a microcontroller without receiving each other's answers?

Thank you so much.
Posted
Updated 6-Jun-10 9:55am
v3

Absolutely not.
The only ways to do this, are:
1) To open the port in a thread, send your data, receive the answer and release the port, so another can access it. You may want to provide some locking.
2) To create another third thread which handles all communications between the port and the two active threads. It decides where data is to go, and when.

I would personally lean toward the second option as it is cleaner and probably safer, but there is a reasonable amount of work involved.
 
Share this answer
 
Yes, this is very commonly done now. google for "virtual serial port". I believe there is at least one project on SourceForge to do this, maybe more.
 
Share this answer
 

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