Click here to Skip to main content
15,887,386 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to read serial data in ASP.NET application but i get an exception ->
C#
"System.NullReferenceException: Object reference not set to an instance of an object."
I have attached my code. Please look into it and tell me how can i read the data. Thanks.

Code--------
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private System.IO.Ports.SerialPort serialPort1;
    private System.ComponentModel.IContainer components;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
            }
            else
            {
                serialPort1.Open();
            }

            Label1.Text += serialPort1.ReadExisting(); //exception here
        }

        catch (Exception eo)
        {          
            Label1.Text = "exception open==>" + eo;
        }         
    }     
}

---------------------------------------------------

P.S. the same code works well in form application.


[Edit member="Tadit"]
Corrected formatting issues.
[/Edit]
Posted
v2
Comments
Sergey Alexandrovich Kryukov 26-Feb-14 0:33am    
Serial port? In ASP.NET!? Do you think your service host is connected to something via a RS-232 cable?! :-)
—SA
Dean_Wi 26-Feb-14 5:05am    
YES...exactly...
Admin's pc is connected through RFID for adding new book in database..
Sergey Alexandrovich Kryukov 26-Feb-14 9:53am    
Are you saying that "admin PC" is the save as the HTTP server's host? or a client?..
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900