4

これが私がこれまでに思いついたコードです:

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();
}

問題は、コードの実行後、テキストボックスが空のままになることです...(AJAX更新パネルを使用してテキストボックスのテキストを更新しています)デバッグ中にブレークポイントを設定すると、シリアルポートから受信したデータが変数に含まれます。新しいテキストボックステキストとして設定しますが、コードが終了しても何も表示されません。テストしたので、updatepanelは機能すると確信しています。

PSシリアルポートがrfidリーダーに接続されており、タグを読み取ろうとしています。Windowsフォームアプリを正常にコーディングして、やりたいことを実行しましたが、ASP.NETに移行する必要があります

4

1 に答える 1

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.

最善の策はActiveX、Windowsフォームコードに基づいてコントロールを作成し、それをWebサイトに埋め込むことです。

于 2013-03-04T08:29:13.640 に答える