Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

AnyCharacterReceived method help.

I am reading data from a serial port. When each character arrives the serial session fires the AnyCharacterReceived event. How do I write this handler in C#, i.e. not the body of the code but the method prototype, etc. Can someone give an example.
Thanks
0 Kudos
Message 1 of 5
(4,093 Views)
Hello Barry,

the AnyCharacterReceived event is of type SerialSessionEventHandler, which has a signature of
void SerialSessionEventHandler (object sender, SerialSessionEventArgs e);

Your event handler needs to have this same signature:

private void MyEventHandler(object sender, SerialSessionEventArgs e)
{
SerialSession session = (SerialSession)sender;
//Code to handle event goes here.
}

You then connect your event handler to the event with the += operator:

mySerialSession.AnyCharacterReceived += new SerialSessionEventHandler(this.MyEventHandler);
0 Kudos
Message 2 of 5
(4,088 Views)
Thanks Glen,
Here is what I have:

A serial dialog box contains a button that opens the serial port. The open port click event looks like the following:

private void buttonOpenPort_Click(object sender, System.EventArgs e)
{
try
{

if(PortOpen == false)
{
this.buttonOpenPort.Text = "Close Port";
this.ledPortOpen.Value = true;
PortOpen = true;

sessionTreeView.Nodes.Clear();
string Port = Configuration.ComPort;
string CurrentSession = "ASRL" + Port + "::INSTR";

//OK, let's open it.
session = ResourceManager.GetLocalManager().Open(CurrentSession);

CurrentSerialSession = session as SerialSession;
if (CurrentSerialSession != null)
{
this.CurrentSerialSession.AnyCharacterReceived += new SerialSessionEventHandler(this.CharacterReceived);

CurrentSerialSession.BaudRate = Convert.ToInt32(Configuration.BaudRate);
CurrentSerialSession.DataBits = Convert.ToByte(Configuration.DataBits);
0 Kudos
Message 3 of 5
(4,075 Views)
Thanks Glen,
Here is what I have:

A serial dialog box contains a button that opens the serial port. The open port click event looks like the following:

private void buttonOpenPort_Click(object sender, System.EventArgs e)
{
try
{
if(PortOpen == false)
{
//OK, let's open it.
session = ResourceManager.GetLocalManager().Open(CurrentSession);

CurrentSerialSession = session as SerialSession;
if (CurrentSerialSession != null)
{
this.CurrentSerialSession.AnyCharacterReceived += new SerialSessionEventHandler(this.CharacterReceived);
//some other stuff
}
}
}
catch(problems)
}
The CurrentSerialSession is defined in
public class frmSerialDialog : System.Windows.Forms.Form
{
private Session session;
private SerialSession CurrentSerialSession;
private Session TempSession;
private SerialSession DisplayCurrentSerialSession;
private bool PortOpen;
// at the top of the class.

//on down I have:
private void CharacterReceived(Object sender, SerialSessionEventArgs e)
{
SerialSession ReceiveSerialSession = (SerialSession)sender;
int NumberOfBytes = ReceiveSerialSession.AvailableNumber;
textBoxReceive.Text = ReceiveSerialSession.ReadString(NumberOfBytes);
}

the CharacterReceived event is never firing even though I know that characters are coming in on the serial port. The question is,
where should the statement
this.CurrentSerialSession.AnyCharacterReceived += new SerialSessionEventHandler(this.CharacterReceived);
go?
Thanks
Barry
0 Kudos
Message 4 of 5
(4,075 Views)
Sorry Barry, I left out a critical piece:

After you add the event handler, put this line:
CurrentSerialSession.EnableEvent(SerialSessionEventType.AnyCharacterReceived, EventMechanism.Handler);

this tells the serial session to actually monitor for that particular event.
0 Kudos
Message 5 of 5
(4,050 Views)