المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Serial Port



C# Programming
03-12-2010, 03:50 PM
Hi

What are the things i need to look into when i use serial port class?

My issue is I am getting a Timeout Exception every now and then in Reading the port.
My understanding about the serial port class is as below

1. We need to set the baud rate, parity etc once
2. Timeout value (read timeout and write timeout) every time you read or write.

a. Read timeout timer starts when the first characeter arrives at the hardware buffer and code will wait till all the characters arrived as per the code comPort.Read(buffer, 0, 100); here it will wait for 100 bytes or timeout value whichever is earlier. ie. if it read 100 bytes it will return even if timeout (say 100mS) not reached. but what happens if there is lesser bytes? is there a interbyte timeout as in CPP here in C#? how to set it?

b. Write timeout functions in same fashion with writing to port. it will return after specified bytes have written unless timeout is not reached.

Am i correct in these assumptions?

Do I have to use some sort of thread locking in DataReceived event thread for proper displaying of the data? (I assume that the DataReceived event thread is system handled thread and does not have control over it. Am i correct?)