Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

In my application, I am using SerialPort class to access a COM port. This COM port is accessed by two different threds for reading & writing respectively.

Is it possible to access same COM port parallely through different threads at the same time?
Or
Do I need to implement critical section to access COM port using SerialPort object?

Thanks in advance :)
Posted

Why don't you use the DataReceived-Event[^]?

Or you could access the BaseStream [^] and use the asynchronous Read/Write-Methods (BeginRead/EndRead(BeginWrite/EndWrite).

I only know the Read/Write-thread approach from Win32.
 
Share this answer
 
VB
I am using SerialPort class of .net framework , also I am using data received event.

Suppose, in certain scenario, thread t1 is writing bytes to COM port & at the same time, thread t2 is trying to read byte data from the same COM port as it already received event,

Then is it possible to access the COM port (same Serial Port class object) simulataneously for reading & writing or

Do I need to implement critical section to access COM port using SerialPort object?
 
Share this answer
 
Comments
boogac 1-Feb-13 5:11am    
i didnt get it,sorry for bad english,are you trying to do while t1 is writing t2 could not reach data received event?

i'm not sure but replying a comment as solution can be confusing? commenting sure be better
Umm, I not 100% sure, but I don't think that is possible using the Com port as you would have to devices trying to use the same IC at the same time and the UART would either get confused (or damaged) by that.
I think what you need is an SPI interface board out side of the PC to take the data in and send the data out. That way you could write the data in a block send it, the board could chop the data up as needed and send it.
Once sent you could listen for reply. Using the Data-Recieved event suggest above I have done similar things in the past, but have not come across the "implement critical section to access COM using the serial port class" Is it a hack to get COM based applications to talk to the NET frame work (if it is I see trouble coming your way!)

Glenn
(oh yeah why the code tags?)
 
Share this answer
 
Technically it should be OK, since there are separate read and write buffers.
But only if one thread does writing and never tries to read and the other just reads and never writes.
In some scenarios this is acceptable (if writes are independent of reads).

But if the protocol you use is request-response based (expects you to write some command and immediately read device response), then you would have problem synchronizing these threads.

So the solution really depends on device type and communication protocol you use.

Take a look at this: http://stackoverflow.com/questions/1488712/is-it-safe-to-read-and-write-to-a-serial-port-at-the-same-time-via-different-thr[^]
 
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