none
Unable to read serail port data in my web page?? RRS feed

  • General discussion

  • Unable to read serail port data in my web page??

    I am pasting my code below.

    Please help.

    Default.aspx

            

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>

            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Receive" />

        </div>
        </form>
    </body>
    </html>

    Default.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.IO.Ports;

    public partial class _Default : System.Web.UI.Page
    {
        SerialPort sp = new SerialPort();

        protected void Page_Load(object sender, EventArgs e)
     

      {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            sp.PortName = "COM1";
            sp.BaudRate = 9600;
            sp.Parity = Parity.None;
            sp.StopBits = StopBits.One;
            sp.DataBits = 8;
            sp.Handshake = Handshake.None;
            if (sp.IsOpen)
                sp.Close();
            sp.Open();
            string text = TextBox1.Text;
            sp.Write(text);
            //char[] textBuffer= new char[10];
           // int i=0;
           // foreach (char ch in text)
            //{
             //   textBuffer[i] = ch;
            //    i++;
          //  }
            //string postData = sp.Read(textBuffer, 0, 10).ToString();

            //TextBox1.Text = postData;
            //TextBox1.Text = "";
            sp.Close();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (sp.IsOpen)
                sp.Close();
            sp.Open();
            string postData = sp.ReadExisting();
            TextBox1.Text = postData;
            sp.Close();
        }
    }

    what I am trying to do is to send the data from textbox to my port "COM1" from button1_click event and when button 2 is clicked then that data will be receieved from port and should be shown in the textbox.

    Wednesday, June 20, 2012 4:46 AM

All replies

  • You make many mistakes.

    First of all - open the Serialport when you start your application and don't close it until you terminate it. If you really want to close the port, give it at least 200 ms to close down before you reopen it. Remember that the port is initialized each time you open it.

    Until you press Button2, the port is closed (except for the time you transmit) and no data will be received, so when you open the port and call ReadExisting, the data is long gone and nothing is displayed.

    Why not make the reception automatic by means of the DataReceived event? Then you don't need to press any button, but data will automatically be displayed as soon as they arive. If you don't mind VB examples, you can find all the information you need to do it properly in my SerialPort tutorial.


    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

    Wednesday, June 20, 2012 9:14 AM
  • thanks for your help.

    Acyually I have tried with DataRecevied but the event does not fire.

    Can you just modify the above code with DataRecevied  event.

    I may have done some mistake in my code.

    Thanks in advance.

    Wednesday, June 20, 2012 9:27 AM
  • I have tried this with DataReceived

    sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

    private static void DataReceivedHandler(
                            object sender,
                            SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            TextBox1.Text=indata;
        }

    but in last line i.e. TextBox1.Text=indata,its showing an error.

    Wednesday, June 20, 2012 9:31 AM
  • The error indicates that the event does fire.  Read Control.InvokeRequired in the Docs.
    Wednesday, June 20, 2012 10:10 AM
  • Control.InvokeRequired is for the windows application.

    this can not be accessed in web application.mine is web appliaction.

    Wednesday, June 20, 2012 10:22 AM
  • @ Priyesh88

    The handler for the DataReceived event runs on a thread pool thread, but TextBox1 is defined on the UI thread. Therefore you are not allowed to call TextBox1.Text=indata; from the handler.

    You need to use one of the four thread safe methods - in practice either control.Invoke or control.BeginInvoke as described in chapter "SerialPort Receive" of my tutorial.


    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

    Wednesday, June 20, 2012 1:05 PM