views:

54

answers:

2

Im a novice in c sharp and im stuck with this task. My requirement is , I create many threads and these threads (send using COM1) have to communicate with a single serial port say COM2. All the threads have to send message using a single COM port(receive using COM2).

say,send "helloworld1"(using thread1) and "helloworld2"(thread2) using COM1 and receive using COM2 in hyperterminal. So i need to see both the helloworlds in the hyperterminal at the same time.

Please help me out.

+2  A: 

You will want to start here. You can instantiate 2 instances the SerialPort class for each COM port you want to send/receive on. I have used 2 variations of receiving data us the SerialPort class:
1. You can manually "Read" on the port at a certain interval (e.g. you can have each thread read as needed).
2. The SerialPort class exposes a DataReceived event that can be subscribed to (an ErrorReceived is also available).

Option 1 might be the best fit.

Edit

After reading your comment, Option 2 may be a better fit so that you can have one "receive" thread that subscribes to the DataReceived/ErrorReceived events. Per @Slider, the lock will also be required to ensure only 1 thread is writing at any given time.

gooch
Does every thread need to Open And Close port on reading, Because I am afraid that .NET is not going to allow two instances to open one COM port
adopilot
There should be one instance for COM1, shared between threads, and one instance for COM2. You shouldn't have to keep opening and closing the ports.
Justin
user403489 Said: All the threads have to send message using a single COM port(receive using COM2). I would like to inform him that Only one theard should listen COM2 port and all others after revived data can write to their ports
adopilot
@adopilot: We currently use the SerialPort class extensively to talk from a pc to device for long (days) periods of time. We open a shared instance of the port once and read/write. Then on Dispose, close the port and clean up threads, etc.
gooch
@user403489: If only 1 thread should listen, then the DataReceived event listening may be a better solution. You can subscribe to that event, parse the data as needed to determine if the data is "complete" before informing the proper thread(s). Remember that serial communication does NOT have a "smart protocol", its just bytes across a line. You have to determine (e.g. via a packet) that all the expected data has been "read". Also, look at @Slider345 answer about the "lock" for port writing.
gooch
+1  A: 
Slider345
Thank you all for you replies.. il check on to it.. Thanks :)
SLp
@Slider: Thanks for your reply. I have opened the port (COM1) using serialport only (with passing arguments inside(COM1 in this case)) and using this single port, I need to open multiple threads and send messages using the same port and receive it at COM2. So do I have to lock the port in a thread and give any delay between the threads so that that port is open for a certain time for a certain thread and then unlock it so that another thread accesses it for a particular time and so on ??? Thanks.
SLp
Yes, that is basically the correct strategy. The lock command itself will cause the thread to delay until all other theads have unlocked the SerialPort.
Slider345