Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
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 Smile | :)
Posted 21-Jan-13 21:30pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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?
  Permalink  
Comments
boogac at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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?)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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[^]
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 1 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100