4

here's the code I've come-up with so far:

protected void Page_Load(object sender, EventArgs e)
{
    try {
    serialPort1.PortName = "COM4";
    serialPort1.BaudRate = 9600;
    serialPort1.Open();
    this.serialPort1.DataReceived += new      
        System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
    Label1.Text = "Connected";
    UpdatePanel1.Update();
    }
    catch (Exception ex) {

    }
}

string x = "";
private void serialPort1_DataReceived(object sender, 
      System.IO.Ports.SerialDataReceivedEventArgs e){

    x = serialPort1.ReadExisting();
    TextBox1.Text = x;
    UpdatePanel1.Update();
}

the problem is after the code runs the text box remains empty... (Im using AJAX update panel to refresh the textbox text) the thing is when I set breakpoints during debug, the data received from the serial port is in the variable and is set as the new textbox text but when the code finishes nothing is displayed.... I'm pretty sure the updatepanel works because I've tested it.

PS The serialport is connected to an rfid reader and im trying to read tags. I have successfully coded a windows form app to do what I want but I need to migrate it to ASP.NET

Amicable
  • 3,115
  • 3
  • 49
  • 77
user2127755
  • 43
  • 1
  • 3
  • Why do you need to migrate this to ASP.NET? – Rawling Mar 04 '13 at 11:36
  • @Rawling well the windows form app I made was actually just a sort of practice/testing. the porject Im working on needs to be in asp. now im thinking, given Knagis 's answer, to use a service to somehow communicate between the windowsform app that reads the rfid data via serial port and the asp.net app taht will process the information read... am i on the right track here guys? – user2127755 Mar 05 '13 at 01:12
  • which reader are you working with ? does it have to be necessary serial communication ? – BrOSs Mar 05 '13 at 16:44
  • @BrOSs this is what i have to work with: http://www.e-gizmo.com/KIT/RFID%20LOWCOST.html – user2127755 Mar 06 '13 at 15:51

1 Answers1

1

Reading data from serial port in ASP.NET application will only work if the reader is connected to the server (not the machine that is running the browser). It seems to be working only because you are testing it locally.

The serialPort1 object will be destroyed after the initial page render is complete. The ASP.NET page only lives until the page is rendered. Then it is destroyed. The next request again recreates all objects. So the serial port would be opened again and again every time the browser goes to the server.

UpdatePanel only changes the classic browser requests into AJAX - it does not change how ASP.NET server processing works. It is still request-response actions triggered by the client browser. In your case the UpdatePanel would have to query the server on a timer.

Your best bet is to create a ActiveX control based on your Windows Forms code and embed that in your website.

Knaģis
  • 20,827
  • 7
  • 66
  • 80