How to create dynamically event _DataReceived

L

León

hello everybody,

i want to do this:

i´m developing an application that reads serial port some data. Until here
easy.

But the problem is that can to connect the cable for whatever port, so i
need
to catch it dynamically and then my application start to listen data.

I know how to get the names of serial ports, but i don´t know how to
associate
events to each serial port dynamically, i heard about delegates o something
like
that.

Please, i need it very quickly, my boss is a little nuisance

It´s all, thanks in advance everybody.



fausto
 
L

Leon

Thanks Peter,

sorry for the words about my boss.

About your opinion, is true i have to create multiple instances for each
port, i know
hotw to do that.
The problem is how to create event instance for each port (port.DataReceived
+= new SerialDataReceivedEventHandler(port_DataReceived);).
Also, i have to create the event port_DataReceived dynamically, how does to
do?

This is a little of my code:

public partial class ListeningPort

{ private SerialPort port = new SerialPort();

public ListeningPort()

{

InitializeComponent();

// When data is recieved through the port, call this method

port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);

ConfigPort();

}

private void ConfigPort()

{

if (port.IsOpen) port.Close();

else

{

// Configuraci¢n del puerto

port.BaudRate =
Convert.ToInt32(ConfigurationSettings.AppSettings["BaudRate"]);

port.DataBits =
Convert.ToInt32(ConfigurationSettings.AppSettings["DataBits"]);

port.StopBits =
(StopBits)Enum.Parse(typeof(StopBits),
ConfigurationSettings.AppSettings["StopBits"]);

port.Parity = (Parity)Enum.Parse(typeof(Parity),
ConfigurationSettings.AppSettings["Parity"]);

port.PortName = "COM1";

port.Open();

}

private void port_DataReceived(object sender,
SerialDataReceivedEventArgs e)

{

string sData = port.ReadExisting();

InsertInMSMQ(sData);

sbDatosPort.Length = 0;

}

}

Thanks Peter



Peter Duniho said:
[...]
But the problem is that can to connect the cable for whatever port, so i
need
to catch it dynamically and then my application start to listen data.

I know how to get the names of serial ports, but i don´t know how to
associate
events to each serial port dynamically, i heard about delegates o
something
like that.

Have you looked at the SerialPort class? It's difficult to understand
your description, but it sounds like you want to monitor multiple ports
and handle i/o on whichever one is active. For that, I'd think you'd need
to create an instance of SerialPort for each port you want to monitor, and
then just handle the events for each instance.
Please, i need it very quickly, my boss is a little nuisance

Maybe it's not such a good idea to call your boss a "nuisance" (even a
little one) in public? :)

Pete
 
L

Leon

Hello Pete,

Here is the code.

Could you show me, how to do dynamic this one: (port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);).?

And how to do dynamic the name for each port name: port_DataReceived?


Thanks

public partial class ListeningPort : ServiceBase

{

private SerialPort puerto = new SerialPort();

private string sNombrePuerto;

private static StringBuilder sbDatosPuerto;

public string NombrePuerto

{

get

{

return this.sNombrePuerto;

}

set

{

this.sNombrePuerto = value;

}

}

public ListeningPort()

{

InitializeComponent();

// When data is recieved through the port, call this method

puerto.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);

}

protected override void OnStart(string[] args)

{

NombrePuerto = "Com1";

DetectarPuertos();

ConfigurarPuerto();

}

protected override void OnStop()

{

puerto.Close();

}

private void DetectarPuertos()

{

ArrayList alPuertos = new ArrayList();

foreach (string p in System.IO.Ports.SerialPort.GetPortNames())

alPuertos.Add(p);

//Here i need to associate DataReceived to the ports

}



private void ConfigurarPuerto()

{

try

{

if (puerto.IsOpen) puerto.Close();

else

{

// Configuraci¢n del puerto

puerto.BaudRate =
Convert.ToInt32(ConfigurationSettings.AppSettings["BaudRate"]);

puerto.DataBits =
Convert.ToInt32(ConfigurationSettings.AppSettings["DataBits"]);

puerto.StopBits = (StopBits)Enum.Parse(typeof(StopBits),
ConfigurationSettings.AppSettings["StopBits"]);

puerto.Parity = (Parity)Enum.Parse(typeof(Parity),
ConfigurationSettings.AppSettings["Parity"]);

puerto.PortName = this.NombrePuerto;

puerto.Open();

}

}

catch (Exception ex)

{

}

finally

{

if (puerto.IsOpen)

puerto.Close();

}

}

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)

{

string sData = puerto.ReadExisting();

// here i'll do something

}

}
 
L

Leon

Thank you Peter,

I´ll try to do.

fausto


Peter Duniho said:
At least a couple of problems: first, that's not a complete code sample.
Second, you posted it without any indentation, making it very difficult to
read.


The word "dynamic" doesn't make sense in that context. Each instance of
your ListeningPort class can subscribe that method to the DataReceived
event without any problem.


There's no need to have a different method name for each different
instance of ListeningPort, nor is there really any practical way to do so.

Pete
 
R

Rick Lones

León said:
i want to do this:

i´m developing an application that reads serial port some data. Until here
easy.

But the problem is that can to connect the cable for whatever port, so i
need to catch it dynamically and then my application start to listen data.

I know how to get the names of serial ports, but i don´t know how to
associate events to each serial port dynamically, i heard about delegates or something
like that.

I've been following this thread and I agree with all of Peter's comments,
especially where he points out that: "There's no need to have a different method
name for each different instance of ListeningPort, nor is there really any
practical way to do so. "

Could your confusion on this point be that you don't realize that the "sender"
parameter of the method can be cast to the SerialPort which raised the event?
This means you can assign the same delegate code to the DataReceived event of
all the ports and then determine from the sender parameter which port actually
did the talking.

But I would like to back up a bit . . .
But the problem is that can to connect the cable for whatever port, so i
need to catch it dynamically and then my application start to listen data.

Why? If it's your application then you should be able to create front end code
which takes the port actually used (and optionally its important connection
parameters such as baud, parity, etc.) either through a configuration string or
more dynamically via a simple form.

If, for some rather weird reason, you really do have to open every port on the
machine to figure out which one is actually in use then I hope you will do two
things: 1) handle the case where you are trying to open a port which is already
opened by some other app, and 2) once you find your actual port to use, close
all your other listeners - otherwise you are hogging all the serial ports of the
machine for nothing.

HTH,
-rick-
 
L

Leon

Hello Rick.
I've been following this thread and I agree with all of Peter's comments,
especially where he points out that: "There's no need to have a different
method name for each different instance of ListeningPort, nor is there
really any practical way to do so. "

Yes, is correct i going to be that.
Could your confusion on this point be that you don't realize that the
"sender" parameter of the method can be cast to the SerialPort which
raised the event? This means you can assign the same delegate code to the
DataReceived event of all the ports and then determine from the sender
parameter which port actually did the talking.

ok also, i need to do cast to "sender" and i use the same event.
But I would like to back up a bit . . .

Why? If it's your application then you should be able to create front end
code which takes the port actually used (and optionally its important
connection parameters such as baud, parity, etc.) either through a
configuration string or more dynamically via a simple form.

I can´t to create a fron end, because it'll be a windows service, where
it´ll receive
a set of data for a serial port.
But before i need to put on listening all serial ports and i identify the
serial port,
so after that, i need eliminate all instances of serial port and leave just
one.

I hope was clear.

fausto.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top