I am trying to refactor my project so that instead of many functions i would have some objects instead.
I am writing an application, which is communicating with a specific optical scanner via Serial port. I use usual SerialPort's class methods
(Write, Open, Close) and some advanced methods, which aren't included in standard class, like ActivateLaser (it writes specific bytes to SerialPort).
I made first step: I created new class, which inherits SerialPort class and added my own methods, like ActivateLaser. This project is compiled to dll and then included in my main project.
Now I'd like to go a step further and include also listening to SeiralPort to my dll. How it works now: first I assign DataReceived event to my scanner (scanner is instance of my own SerialPort class):
scanner.DataReceived += MyDataReceivedEventHandler;
Then in the MyDataReceivedEventHandler I call a delegate, which displays received data in RichTextBox or in DataGridView:
private void MyDataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e)
{
...
this.BeginInvoke(new DisplayDataDelegate(DisplayData), receivedText);
...
}
private void DisplayData(string receivedText)
{
// display received text in RichTextBox in one project, display received text in DataGridView in another project
}
Now I wonder, how could I implement listening to serial port in MySerialPort class. If I just add event handler for DataReceived, then statement which binds MyDataReceivedEventHandler to scanner, would look something like this:
scanner.DataReceived += scanner.MyDataReceivedEventHandler;
I just don't know how to put all this together (should I define MyDataReceivedEventHandler in MySerialPort class, where should I put delegate for displaying text, how can I make possible, to show text in RichTextBox or in DataGridView, ...)
I know my question is a little complex, but I would really like to organize my work better, so that I could use my MySerialPort class in another projects...
Thanks!