Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using SerialPort_DataReceived event in my project. But I want to have different Strategies of receiving data inside the body of this event.

I have googled and I found that one way is to inject the Func<> to the methods and changing their behaviour (Strategy Pattern).

But is that possible to do the same for evnets like SerialPort_DataReceived? If yes how can I do that? (without violating the principles)

Edit:

Actually I want to add Func<> Parameter to the SerialPort_DataReceived Like this:

public Rs232(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
        {
            SerialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
            SerialPort.DataReceived += SerialPort_DataReceived;
        }

        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            throw new NotImplementedException();
        }

I want to change the SerialPort_DataReceived as the following:

private void SerialPort_DataReceived(Func<sth,sth> Strategy,object sender, SerialDataReceivedEventArgs e)
            {
                throw new NotImplementedException();
            }
share|improve this question
I am of the opinion that the DataReceived event handler should do one thing, receive the data. It should determine the bytes available, read that number of bytes, store those bytes in a queue or list, and exit. Remember that only one serial event can be active at a time. Put your strategy pattern in another thread or timer. – dbasnett yesterday
@dbasnett You know I have a set of devices that I have to read values from them. But I have problem with them. to read some of them I have to use the "ReadLine()" or "ReadByte" for the others I have to use "ReadExisting()" I can not use for example "ReadLine()" method for all of them. and obviously I can't change the devices. – Mehrdad Kamelzadeh yesterday
Everyone of them will accept msdn.microsoft.com/en-us/library/ms143549%28v=vs.100%29.aspx . How you decode / interpret the raw bytes is up to you. – dbasnett yesterday
@dbasnett thanks for your link but my question is whether it is possible to inject Func<> to an event like SerialPort_DataReceived at all? Imagine that I have to use different method for each device. my question is about Design Patterns and Method Injection not working with SerialPort. – Mehrdad Kamelzadeh yesterday
My point was that the event is not the place to decode data. When the event fires there may be 1 or more bytes available to be read. One other thing, it is a very bad idea to put a blocking read in the event handler. – dbasnett yesterday

2 Answers

If you talk about Serial Port's specific, I think this answer may not apply.

I thought of an adapter-specific pattern to handler your Method Abstraction. Assume you already know the delegation and event handler.

public class DeviceASerialPort{
    public DeviceASerialPort{
        // the parameter below may be self-defined instead of passed
        this.SerialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
        SerialPort.DataReceived += SerialPort_DataReceived;
    }
    private SerialPort;
    //event delegation here (i forgot how to declare delegation
    // let's say the name is also DataReceived

    private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        //null guard
        DataReceived(
            sth=> {
                //custom operation here
                return sth;
            }
            , sender
            , e);
    }
}

In the end, you can end up making interfaces for that serial port adapter or even injecting the function to extend the abstraction.

share|improve this answer

Sure you can inject some Func<> to the class that contains your DataReceived event handler and call Func<> from the handler.

Also you could deliver received data to a certain handler class that has one or more Func<> to deal with data depending on decoding the raw bytes.

share|improve this answer
I don't want to inject the Fucn<> to the class containing the DataReceived. Actually I want to inject Func<> to the DataReceived Event itself (Doing something like Method Injection). – Mehrdad Kamelzadeh yesterday
1  
Are you looking for something like PostSharp? Otherwise you should post some code about what you do now and what you'd like to do. "Inject" is a bit ambigous here. – JeffRSon yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.