5 Replies - 698 Views - Last Post: 04 December 2012 - 10:12 AM Rate Topic: -----

#1 NNX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-December 12

SerialDataReceived not working.

Posted 03 December 2012 - 07:47 PM

Hi! I'm having trouble getting my code to receive serial data in and displaying it. I know that the port is open and works because I can send data out/in using another program. I have a USB to Serial device and its connected to a micro-controller sending data out that I can view in other programs like hyper=term.

Any help is appreciated. Thanks!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace serial_port_test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
            //Scans computer for Com ports and loads names into list box
            foreach (string item in System.IO.Ports.SerialPort.GetPortNames())
            {
                ComportBox.Items.Add(item);
            }
        }

        private string rxString;

        private void MySerial_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            rxString = MySerial.ReadExisting();
            rxString = MySerial.ReadLine();
            this.Invoke(new EventHandler(displayText));
        }

        private void displayText(object o, EventArgs e)
        {
            rtbRX.AppendText(rxString); //append adds to the prev.
        }

        private void bSend_Click(object sender, EventArgs e)
        {
            MySerial.Write(rtbTX.Text); //reads string and sends to rtbTX
        }

        private void bClear_Click(object sender, EventArgs e)
        {
            rtbTX.Clear();
            rtbRX.Clear();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            MySerial.Close();
        }
        // Auto-send 
        private void rtbTX_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (MySerial.IsOpen && tbIM.Checked)
            {
                char[] ch = new char[1];
                ch[0] = e.KeyChar;
                MySerial.Write(ch, 0, 1);
            }
        }
       
        private void tbIM_CheckedChanged(object sender, EventArgs e)
        {

        }

        //second loop gets the item selected then sets that as the port name
        private void ComportBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i;
            string s;
            for (i = 0; i <= (ComportBox.Items.Count - 1); i++)
            {
                if (ComportBox.GetItemChecked(i))
                {
                    s =  ComportBox.Items[i].ToString();
                    MySerial.Close();
                    MySerial = new SerialPort(s, 9600, Parity.None, 8, StopBits.One);
                    MySerial.Open();
                    MySerial.DtrEnable = true;
                    rtbRX.Text = "Port " + s + " is open" + "\n";
                }
            }
           
        }

        private void bRXclear_Click(object sender, EventArgs e)
        {
            rtbRX.Clear();
        }

        private void AutoRx_Checked(object sender, EventArgs e)
        {
            //not being used yet
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: SerialDataReceived not working.

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: SerialDataReceived not working.

Posted 03 December 2012 - 07:57 PM

I see where you've defined the MySerial_DataReceived callback, but I don't see where you actually tie it to anything.

Have you placed a breakpoint in this method? Why do you read twice?

This post has been edited by Momerath: 03 December 2012 - 08:01 PM

Was This Post Helpful? 2
  • +
  • -

#3 NNX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-December 12

Re: SerialDataReceived not working.

Posted 03 December 2012 - 09:02 PM

Breakpoint? Could you explain a little more on that, I'm just starting out with C#. Sorry


The double read shouldn't be there. It should read:

private void MySerial_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

        {

            rxString = MySerial.ReadExisting();

            this.Invoke(new EventHandler(displayText));

        }


Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: SerialDataReceived not working.

Posted 04 December 2012 - 08:07 AM

For the DataReceived event to actually fire, you need to subscribe to it.

MySerial.DataReceived += MySerial_DataReceived;



That code would go in the SelectedIndexChanged event for the combobox.

Also, here is a great tutorial on debugging.
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,193
  • Joined: 02-June 10

Re: SerialDataReceived not working.

Posted 04 December 2012 - 08:55 AM

View PostNNX, on 03 December 2012 - 10:02 PM, said:

Breakpoint? Could you explain a little more on that,


What does this error message mean?
Do this one first, then the rest below

Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:

This post has been edited by tlhIn`toq: 04 December 2012 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 NNX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-December 12

Re: SerialDataReceived not working.

Posted 04 December 2012 - 10:12 AM

Thanks eclipsed4utoo! Its working now. I'm going to look more into debugging and such, as well as why your answer works. Wow I'm dense sometimes....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1