I am working on a winforms application. I have a list of SerialPorts, each one listening on a different COM port.
I am planning to add a unique event handler SerialPort.DataReceived for all the SerialPorts. Is this a good design? Thank you.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, sure, nothing wrong with that. And often quite useful since the DataReceived event handler should try to receive an entire “packet” before handing it off to other code that processes the data. Particularly if you use BeginInvoke to run code on the UI thread. Different devices usually have different protocols so would need different code in their respective event handlers. Or different buffers to store partial responses.
Or not and all devices operate the same and sharing the same event handler code will work. You’d need to cast the sender argument to SerialPort to make the proper Read call. There is little to guess at the appropriate way to go in your question.