简体   繁体   中英

C# listening to serial port and proceed as needed , after page loaded on client side

I'm developing an C#/Asp.net application that comunicates with a device that uses serial ports.

First, i have a button that does the following:

Response.Redirect("Zxx.aspx?ValtoSibs=" + Strvals);

That basically redirects me to the page where i open my serial port, write into int and then want to read any input i get from the device for example, if i click on a key on the device i want to read it in the application and proceed as i want to.

Here is the code that's running on Zxx.aspx

 protected void Page_LoadComplete(object sender, EventArgs e)

        {
            string ValtoSibs = Request.QueryString["ValtoSibs"];
            SIBS.SIBS ComTPA = new SIBS.SIBS();
            ComTPA.IniciaTPA();
            ComTPA.SendPaymentCmd(Int32.Parse(ValtoSibs));




                if (ComTPA.ComTPA.ReadExisting().Contains("M001909"))

                {
                    ComTPA.ComTPA.DiscardInBuffer();
                    ComTPA.ComTPA.DiscardOutBuffer();
                    ComTPA.CloseTPA();
                    Response.Redirect("Erro.aspx");

                }
        }

In the block above, i can't know if any button is pressed on the device , because i need a while loop encapsulating the block:

  if (ComTPA.ComTPA.ReadExisting().Contains("M001909"))

                {
                    ComTPA.ComTPA.DiscardInBuffer();
                    ComTPA.ComTPA.DiscardOutBuffer();
                    ComTPA.CloseTPA();
                    Response.Redirect("Erro.aspx");

                }

The problem is , when i try to put a while cycle , the page doesn't load zxx.aspx doesn't load, it locks on an infinite loop BUT when i press any key on the device, for example the one that sends the M001909 string , it acts as needed, closes the port and redirects the page.

My main problem is, i need to be in Zxx.aspx page , already fully loaded on client side and then have the while cycle working behind so that i can listen to the commands sent on the device.

Found the solution,

I created a new DataReceived Handler and inside it called a function that does the following and take as parameter ReadExisting ():

if (ComTPA.ComTPA.ReadExisting().Contains("M001909"))

                {
                    ComTPA.ComTPA.DiscardInBuffer();
                    ComTPA.ComTPA.DiscardOutBuffer();
                    ComTPA.CloseTPA();
                    Response.Redirect("Erro.aspx");

                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM