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

مشاهدة النسخة كاملة : Communicating with a serial port



C# Programming
06-23-2009, 11:01 PM
I have a program that needs to do two-way communication with a serial port. Once I set up the serial port, I will do a WriteLine("something"), then the program must wait for a response, read the response, and then based on that response do another WriteLine, rinse and repeat. Ideally I want to be able to call some function match(SerialPort s, string matchText) that will block until I get a response from the SerialPort and then let me know if my text matches the response.

I've tried using ManualResetEvents and WaitOne(), but calling WaitOne() seems to block everything: the SerialPort.DataReceived event never triggers! Note that all the thread management is occurring in a static management class, but I don't believe this would cause the problem.

Any ideas? Perhaps I'm going about this the wrong way. It seems like the write->wait->read->decide->write... workflow should be a pretty common task with serial ports.