locked
WPF IO. Using serial port RRS feed

  • Question

  • Hi everybody I have this code below working for a serial device in my WPF application but I'm not able to display the ReadLine in a textbox
    that I want to instead I'm showing that in a messageBox what should I do to display the serialPort.ReadLine in a textbox see the code below and help me how to do this Please help I've tried my best someone please help, thank you everybody.



    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.IO.Ports;
    using System.Threading;

     

    namespace

     

    ComPort

    {

     

    /// <summary>

     

    /// Interaction logic for Window1.xaml

     

    /// </summary>

     

    public partial class Window1 : Window

    {

     

    static SerialPort Sp;

     

    string Message;

     

    public Window1()

    {

     

    this.InitializeComponent();

     

     

     

    // Insert code required on object creation below this point.

     

    //this.PreviewKeyDown += new KeyEventHandler(PortChat);

     

    }

     

    private void button1_Click(object sender, RoutedEventArgs e)

    {

     

    //Create new serialport.

    Sp =

    new SerialPort();

     

    StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;

     

    Thread readThread = new Thread(Read);

     

    //Set the appropriate properties.

    Sp.PortName = SetPortName(Sp.PortName);

    Sp.BaudRate = SetBaudRate(Sp.BaudRate);

    Sp.Parity =

    Parity.None;

    Sp.DataBits = SetDataBits(Sp.DataBits);

    Sp.StopBits =

    StopBits.One;

    Sp.Handshake =

    Handshake.None;

     

    //Set the read/write timeouts

    Sp.ReadTimeout = 500;

    Sp.WriteTimeout = 500;

     

    Sp.Open();

    readThread.Start();

    ComTB.Text = message;

    // The ComTB is a text box and I'm not able to see the string variable

    }

    // message in the intelisence what shoul I do.

     

    private int SetDataBits(int p)

    {

     

    int dataBits;

    dataBits = 7;

     

    return dataBits;

    }

     

    private int SetBaudRate(int p)

    {

     

    int baudRate;

    baudRate = 9600;

     

    return baudRate;

    }

     

    private string SetPortName(string p)

    {

     

    string portName;

    portName =

    "COM1";

     

    return portName;

    }

     

    public static void Read()

    {

     

     

    string message = Sp.ReadLine();

     

    MessageBox.Show(message); /// I want to show this string message in The ComTB above described

     

    // and I want to convert string in to double So that I could Show.

     

    // decimal point in the numbers.

    }

     

     

     

     

     

     

     

     

    }

    }

     

     

     



    // If you get it a little messed up it's not like this in the Visual Studios 2008 the code is messed up here only I don't know why it's messed
    but the code here is correct and working. 

     

     

    • Edited by Smith010 Wednesday, May 27, 2009 3:43 PM
    Tuesday, May 26, 2009 6:16 PM