5 Replies - 2345 Views - Last Post: 16 August 2012 - 09:47 AM Rate Topic: -----

#1 vidhyar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-August 12

Initialize form controls based on serial port data

Posted 15 August 2012 - 09:58 AM

All,

I have the following program that reads the serial port
public static void Main()
        {
            SerialPort mySerialPort = new SerialPort("COM1");

            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;

            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            mySerialPort.Open();

            Console.WriteLine("Press any key to continue...");
            Console.WriteLine();
            Console.ReadKey();
            mySerialPort.Close();
        }

        private static void DataReceivedHandler(
                            object sender,
                            SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            Console.WriteLine("Data Received:");
            Console.Write(indata);
        }


I have below method in a application that calls the webservice that pulls the details from database.


  private void Reintialise()
    {
        InitializeComponent();
        try
        {
            kioskService.KioskTimeSheet kiosk = new kioskService.KioskTimeSheet();
            string empCode = "";
            buttonIn.IsEnabled = true;
            buttonOut.IsEnabled = true;
            buttonstart.IsEnabled = true;
            buttonSwitch.IsEnabled = true;
            buttonend.IsEnabled = true;
            
            DataSet ds = kiosk.LoadEmployeeDetails("CO123");



            if (ds != null)
            {
                DataTable dt = ds.Tables[0];

                emp.name = dt.Rows[0]["Name"].ToString();
                emp.empID = dt.Rows[0]["EmployeeID"].ToString();
                emp.status = dt.Rows[0]["Status"].ToString();
                emp.ProjectNo = "18674AB";
                textBoxName.DataContext = emp;
                if (emp.status == string.Empty)
                {
                    buttonIn.IsEnabled = true;
                    buttonOut.IsEnabled = false;
                }
                else
                {
                    buttonIn.IsEnabled = false;
                    buttonOut.IsEnabled = true;
                }
            } //if ds
        }
        catch
        {
            ErrorBox.Text = "Error in Reintialise";
        }
    }


Serial port will continously scan for input but only one user at a time. How can I now pass the value read from DataReceivedHandler to the reinitialise method and start the application?

Is This A Good Question/Topic? 0
  • +

Replies To: Initialize form controls based on serial port data

#2 tlhIn`toq  Icon User is offline

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

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Initialize form controls based on serial port data

Posted 15 August 2012 - 10:20 AM

I can't seem to make a connection between these various bits of code.

You have a serialport handling method. Ok. What does that have to do with your database or your users?

Quote

How can I now pass the value read from DataReceivedHandler to the reinitialise method and start the application?

What? The application is already started. Otherwise how would you have anything?

Are you saying the user name is being passed through the serial port?
So the user name comes in via serial ("bob456") for example, and you want to pass that to your ReInitialise() method? Raise an event in your serial port handler instance. Pass the text. Put in a string parameter in your ReInitialize() method.

Basically you're asking "I've cobbled together this stuff from various internet sources but don't know how to actually design the program. Can someone here take all the parts I've found and make a working program?"

Most of us aren't going to design your program for you. Or put all this found data together into a working program for you.

These tutorials should help you with the events and designing your app.

[*]Bulding an application - Part 1
[*]Building an application - Part 2
[*]Quick and easy custom events
Was This Post Helpful? 1
  • +
  • -

#3 vidhyar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-August 12

Re: Initialize form controls based on serial port data

Posted 15 August 2012 - 12:25 PM

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#4 vidhyar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-August 12

Re: Initialize form controls based on serial port data

Posted 16 August 2012 - 07:29 AM

After yesterday's post, I sat down and thought what made serial port to function differently in console and WPF application. Answer lay in WPF threading model. In console application main thread and serial port thread are synchronised whereas in WPF , main application thread(primary) and serial port (secondary) thread are different. We need to use dispatcher invoke method so that serial port thread can send information to main application.


      

 SerialPort sp = (SerialPort)sender;
                sCommdata = sp.ReadExisting();
                // WPF needs Dispatcher to handle the serial port thread and input the main thread
                Dispatcher.Invoke(DispatcherPriority.Send, new UpdateKiosk(Reintialise), sCommdata);
   

Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

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

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Initialize form controls based on serial port data

Posted 16 August 2012 - 07:41 AM

This might be an unpopular answer but I see that has a hack most times.
When you have to use Dispatcher.Invoke like that it is most often used as a way to bypass the fact that you're directly changing things across threads - rather than fix the logic so each thread properly takes care of its own responsibilities.

Personally, I would make a UserControl that handles your COM ports. Have it raise an event for whatever you want such as NewUserEvent with a NewUserEventArgs that carries the data of the user's credentials/name/login. The extra 30 minutes of coding up front to make a proper design will save tons of headache and more patching for the lifespan of the product.
Was This Post Helpful? 1
  • +
  • -

#6 vidhyar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-August 12

Re: Initialize form controls based on serial port data

Posted 16 August 2012 - 09:47 AM

Thank you for the suggestion. I will update the post later with the user control code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1