3 Replies - 747 Views - Last Post: 23 September 2011 - 09:35 AM Rate Topic: -----

#1 BadREDDIT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-September 11

Passing Serial Data back to the initializing class with priority

Posted 21 September 2011 - 06:00 PM

Hello and good day to everyone!


I'm working on a project in WPF where I read in serial data from my "Serial_In" class. Currently I am using:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace Airlink_II
{


    class Serial_In
    {
        private string data;
        public System.IO.Ports.SerialPort Serial;
        public void GPS_Init()
            
        {
            Serial = new SerialPort();
            Serial.PortName = "COM1";
            Serial.BaudRate = 9600;
            Serial.Parity = Parity.None;
            Serial.StopBits = StopBits.One;
            Serial.DataBits = 8;
            Serial.Handshake = Handshake.None;
            Serial.ReadTimeout = 50000;
            Serial.WriteTimeout = 50000;
            Serial.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
            Serial.Close();
            Serial.Open();
        }
        

        void DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // data = recieved string that I want to pass back to my "public MainWindow()" (which is the entry point for WPF)        
        }


    }
}



The strings received on the serial port will be random and I need to parse and populate a structure with the information before I can read in another string. I'm having problems with this, I don't want to have one large .cs file I would like to have it split up.



Here is my WPF entry point:

namespace Airlink_II
{

    public partial class MainWindow : Window
    {
        Aircraft_Database WMI_AC;
        Serial_In SERIAL;

        

        public MainWindow()
        {
             InitializeComponent();
             controller();
        }

        public void controller()
        {
            WMI_AC = new Aircraft_Database(); //My structure that I want to parse and populate with serial data
            SERIAL = new Serial_In();
            SERIAL.GPS_Init();
        }

  


    }
}


Any help or suggestions would be greatly appreciated!


Much thanks,

Kane

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Serial Data back to the initializing class with priority

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Passing Serial Data back to the initializing class with priority

Posted 23 September 2011 - 02:14 AM

I would create an event for your SerialIn class, that is triggered when data is received. The main window listens to that event, and responds on the dispatcher at the desired priority. Your SerialIn class should never know about the MainWindow full stop. It could do, but it would be awful OO programming.

So you will want to:
  • Create a new class for the event argumnents for serial string received
  • Create a delegate (event handler) for handling the events
  • Create an ew event on your SerialIn class using the delegate from the previous point
  • Subscribe to the event on your main window
  • When data is recieved check your event has subscribers and dispatch it


Now when you handle the event on the main window you will want to use the main windows dispatcher to action the event as it will likely be from another thread (you can check if you want) and this will allow you to specify a DispatcherPriority telling the main window the priority to push your handler into the dispatcher at!

It sounds way more complex than it is. This should also be in the WPF forum FYI
Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

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

Reputation: 5436
  • View blog
  • Posts: 11,665
  • Joined: 02-June 10

Re: Passing Serial Data back to the initializing class with priority

Posted 23 September 2011 - 06:30 AM

Expanding on what Ben said, we have some good tutorials (winForms) on cross-class communication and raising events to pass data.

See FAQ #2


FAQ (Frequently Asked Questions - Updated Sep 2011

Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Passing Serial Data back to the initializing class with priority

Posted 23 September 2011 - 09:35 AM

Moved to WPF
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1