0

Hello I have a PIC16F877A micro-controller and Lcd display. I have made PIC controller to display random numbers in the LCD screen for every 2mins and it also send those randomly generated number to my laptop for every 2 mins through RS232 cable. I am creating a website using ASP.NET 4 C# where I want to read those numbers and do functions. I have written the following coding but its not working but I am receiving those random numbers in my laptop hyper-terminal. Please suggest me how to achieve this - I have to receive those codes every 2mins.

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

public partial class Entercode : System.Web.UI.Page
{
    static SerialPort mySerialPort = new SerialPort("COM46", 1200, Parity.None, 8,     StopBits.One);
    protected void Page_Load(object sender, EventArgs e)
    {
        mySerialPort.Open();
        String str = mySerialPort.ReadLine();
        mySerialPort.Close();

    }
}

1 Answer 1

1

I suggest re-architecturing your program.

ASP.NET is designed to be stateless (because of its HTTP basis). ASP.NET host processes (w3wp.exe) can be terminated and recycled at will, without warning. If you need reliability you should perform SerialPort communication in a dedicated Windows Service process which communicates with your ASP.NET application via some form of IPC.

Using Page_Load is incredibly inappropriate, not to mention terribly un-thread-safe. The .Close method is equivalent to calling .Dispose, which defeats the point of having your SerialPort as a static member (as its lifetime ends).

I get the idea you're not familiar with either ASP.NET or IO port programming. I suggest you hone your skills in these areas separately (i.e. develop a command-line serial port program first, then a simple ASP.NET web application that doesn't use the serial port), then combine them after you're strong in both areas.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Ya I am new to both language. But I am an electronics student, so I am not in need of learning the complete chapter of ASP.NET. I want this code temporarily to check my application. So can you please suggest what code must I use in replace of this.
They aren't languages. C# is the language, ASP.NET is a platform. Anyway, ditch ASP.NET entirely and just develop a console application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.