You must Sign In to post a response.
  • Category: ASP.NET

    Serial Port Application In ASP.Net

    Hello Everyone,
    I am New To ASP.Net
    i have a Serial Port Application in ASP.Net. I have done the serial port connection part but unable to display the received data on TEXTBOX.
    Here is My Code

    using System;
    usingSystem.Collections.Generic;
    usingSystem.Linq;
    usingSystem.Web;
    usingSystem.Web.UI;
    usingSystem.Web.UI.WebControls;

    usingSystem.Threading;

    namespace WebApplication1
    {
    publicpartialclass_Default : Page
    {
    System.IO.Ports.SerialPortsp = newSystem.IO.Ports.SerialPort();
    staticbool _continue;
    stringDispString = "";



    protectedvoidPage_Load(object sender, EventArgs e)
    {
    if(!IsPostBack)
    Get_COMPorts();
    // TextBox1.Text = "123456";
    }

    protectedvoid Button1_Click(object sender, EventArgs e)
    {
    if (DropDownList1.Text != null)
    {
    if ((sp.IsOpen) || (Button1.Text == "Disconnect"))
    {
    // serialPort1.Close();
    Button1.Text = "Connect";
    sp.Dispose();

    }
    else
    {
    // Set_Settings();
    sp.PortName = DropDownList1.Text;
    sp.Parity = System.IO.Ports.Parity.None;
    sp.BaudRate = 38400;
    sp.StopBits = System.IO.Ports.StopBits.One;
    sp.DataBits = 8;
    sp.DataReceived+=sp_DataReceived;
    sp.Open();
    TextBox1.Text = "Connection Established" + Environment.NewLine;
    Button1.Text = "Disconnect";
    }
    }
    }
    privatevoidGuiThread(Action action) //For IP Camera Discovcery
    {
    // BeginInvoke(action);
    }
    privatevoidsp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
    //GuiThread(() =>
    //{
    // if (sp.IsOpen)
    // {
    // DispString = sp.ReadExisting();
    // TextBox1.Text = DispString;
    // }
    //});

    if (sp.IsOpen)
    {

    DispString = sp.ReadExisting();

    TextBox1.Text = TextBox1.Text + DispString + Environment.NewLine;

    Button2_Click(sender, e);
    Label1.Text = DispString;
    // this.TextArea1.InnerText = DispString;

    System.Windows.Forms.MessageBox.Show(DispString);
    }

    }
    publicvoidGet_COMPorts()
    {
    foreach (StringportNameinSystem.IO.Ports.SerialPort.GetPortNames())
    {
    DropDownList1.Items.Add(portName);
    }
    }

    protectedvoid Button2_Click(object sender, EventArgs e)
    {
    TextBox1.Text = TextBox1.Text+DispString;
    }

    protectedvoid TextBox1_TextChanged(object sender, EventArgs e)
    {
    TextBox1.Text = TextBox1.Text + DispString;
    }
    }
    }


    So kindly Help me develope the above application
  • #763149
    Hai Dinesh,
    You need to make sure that the port names are valid which are retrieving or provide the hard code name.
    Also change the baud rate as 9600, as this could be problem in sending the data.
    You can also go to the below link and compare your code:

    http://csharp.simpleserial.com/

    Just use the minimal code and check. Once the communication is done, you can use the dropdown and other things.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #763156
    Dear Pawan,
    My device communicates with baud rate 38400.

    if i keep message box to see the received Data. i can see it but unable to write in textbox in webpage.

    So kindly Help


Sign In to post your comments