SerialPort SerialDataReceivedEventHandler

Hi,

I can't figure out how to get the event handler to work when data is received on the com port. I've looked around the forums here and I downloaded a console sample program that I tested on my computer and it works. But when I try and apply the same Idea to my C# windows form program I can't get the event to fire. Am I doing something wrong Some other post on the forum were talking about using different threads for the SerialDataReceivedEvent handler...  Thanks in advance for your help.

Kevin Jones

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO.Ports;

namespace SerialEcho

{

public partial class SerialEcho : Form

{

SerialPort txPort = new SerialPort();

SerialPort rxPort = new SerialPort();

public SerialEcho()

{

InitializeComponent();

startCom();

}

private void startCom()

{

SerialPort txPort = new SerialPort();

txPort.BaudRate = 19200;

txPort.DataBits = 8;

txPort.ReceivedBytesThreshold = 2;

txPort.StopBits = StopBits.One;

txPort.Parity = Parity.None;

txPort.DtrEnable = true;

txPort.Handshake = Handshake.RequestToSend;

txPort.DataReceived += new SerialDataReceivedEventHandler(txPort_DataReceived);

SerialPort rxPort = new SerialPort();

rxPort.BaudRate = 19200;

rxPort.DataBits = 8;

rxPort.StopBits = StopBits.One;

rxPort.Parity = Parity.None;

string[] ports = SerialPort.GetPortNames();

foreach (string port in ports)

{

txPortList.Items.Add(port);

rxPortList.Items.Add(port);

}

Console.WriteLine("The Following Serial Ports Were Found:");

// Display each port name to the Console

foreach (string port in ports)

{

Console.WriteLine(port);

}

Console.ReadLine();

}

private void txPort_DataReceived(object sender, EventArgs e)

{

byte[] txBuf;

Console.WriteLine("txPort_DataReceived:: Got new data from port!!");

txBuf = new byte[256];

txBuf[0] = (byte)txPort.ReadByte();

rxPort.Write(txBuf, 0, 1);

}

 

 

private void txConnectButton_Click(object sender, EventArgs e)

{

//Loop Back Testing function for the txPort

Console.WriteLine("txPortList.Text = " + txPortList.Text);

if ((txPortList.Text.Length != 0) && (!(txPort.IsOpen)))

{

txPort.PortName = txPortList.Text;

txPort.Open();

if (txPort.IsOpen)

{

txConnectButton.Text = "Disconnect";

}

}

else

{

txPort.Close();

txConnectButton.Text = "Connect";

}

}

private void rxConnectButton_Click(object sender, EventArgs e)

{

Console.WriteLine("rxListBox.Text = " + rxPortList.Text);

if ((rxPortList.Text.Length != 0) && (!(rxPort.IsOpen)))

{

rxPort.PortName = rxPortList.Text;

rxPort.Open();

rxConnectButton.Text = "Disconnect";

}

else

{

rxPort.Close();

rxConnectButton.Text = "Connect";

}

}

private void testButton_Click(object sender, EventArgs e)

{

txPort.WriteLine("ABCDEFG");

}

 

 

 

 

 

}

}

 



Answer this question

SerialPort SerialDataReceivedEventHandler

  • tjjang

    As you use Handshake.RequestToSend.

    Do you check that CtsHolding and RtsEnable is true before send

    Have you checked DsrHolding and DtrEnable too

    These are just checks for debugging.



  • YdN

    Ok, I will try that and see if it works.

    THanks,

    KEvin


  • ozForever

    Form the code that I had read, which actual Serial Port That You Do Mapped during runtime

    txPort.PortName = txPortList.Text;

    txPort.Open();

    ^ Continue to 'Open/Close' on "Various" Port

    sorry if I do miss understand your logic here.


  • DenisM

    Well In the startCom() method I enumerate all of the avaliable Com ports on the computer and place them in a textBoxList. This enables the user to select at runtime what com port he want's to use. You can open and close and change a port while the program is running. The testButton_Click method sends a some characters out of txPort and is hardwired for loopback testing. I can never get the characters that I send out to come back in the txPort_SerialDataReceived method. I think it has to do with something about threads... But I'm at a loss with this.

    Thanks for your help so far!

    Kevin


  • alvi fawad

    It looks like you are attaching the event to the wrong port.

    txPort.DataReceived += new SerialDataReceivedEventHandler(txPort_DataReceived);

    Later you do

    txPort.WriteLine("ABCDEFG");

    Which sends data to rxPort, if I understand your program correctly. But there is no event on that port so no event will be triggerd.



  • Johnson Gao

    Hi,

    Should I try and create a seperate serial port class so that it can run in it's own thread

    Kevin


  • Dan Kuehn

    Yup, I've used several terminal programs for a loop back test and they've worked. I just don't know what I'm doing wrong in my c# code...
  • usmanm

    The button event that sends the txPort.WriteLine("ABCDEFG") it is just a test function because I have the txPort connected to a loopback by external hardware. So the same data I send out I should get back in the SerialDataReceivedEventHandler.

    Thanks,
    Kevin


  • Jordi Valldaura Rique

    Great you found it. I'll mark this thread as answered as it is solved.



  • Emerald

    Hi,

    Yeah both of those settings are true. I'm starting to think that it is not the way I have the port setup but the thread that the event handler is running in. There is a SerialPort console application example that I've compiled and it works. I'm using the sames settings except that I'm using the serial port in the form. How would I put start the event handler in different thread in a windows form application

    Thanks,
    Kevin


  • Paula Mestre

    Have you checked that the hardware works

    If you use HyperTerminal to send some testdata, is it echoed back



  • Tittle Joseph

    Hi,

    I found a working example of a Windows Forms application with a Serial Port. After staring hard at my code, you'll notice that I declare txPort = new SerialPort() twice. Same with the rxPort... There was some other things that I had to take out... Anyhow I wanted to say Thank You to you guys for your suggestions and willingness to help.

    Have a Blessed Day,

    Kevin


  • SerialPort SerialDataReceivedEventHandler