Method Similar to Readline () for Windows Form?

1

I want to pass a small program that I have created in Console which takes the string that a USB device sends and sends it via TCP to an IP and a specific port. Right now I pass the received chain to a string and treat it a posteriori but ... What function / method do I have to use to capture what I receive via USB?

It is not clear to me what would be the best way to do it ... I have the following ...

    private void btnConectar_Click(object sender, EventArgs e)
         {
                ActualizarStatus();
             try
            {
                envio.Connect(IPAddress.Parse(tbIP.Text), tbPuerto));
                ActualizarStatus();
                btnConectar.Enabled = false;

                while (true)
                {
                    Enviar(cadena);                
                }
            catch (Exception error)
            {
                 ActualizarStatus(error.Message);
            }


    public void Enviar(string cadena)
         {
             ActualizarStatus();
             /////////////////////////////////////
             byte[] mensaje = Encoding.Default.GetBytes(cadena);
             try
                {
                    int bytesSent = envio.Send(mensaje);
                    ActualizarStatus();
                }
                catch (Exception error)
                {
                    ActualizarStatus(error.Message);
                }
          }

If you notice, being inside the method Enviar() I need to stay waiting on the line where I have the ////////////////// to the USB buffer is filled, Once you have as you say the car return, I would continue with the following line, which prepares me the string cadena to send it right after. The problem I have is that I do not know how to do it in a way that is not a fudge. The truth is that in console with a simple Console.ReadLine () solved everything ...

P.D: I have omitted part of the code because it is irrelevant so that it is not heavy.

Thanks

    
asked by Edulon 11.10.2017 в 12:44
source

1 answer

1

I do not have a barcode reader right now, but basically they are exactly like a keyboard. What I would do is the following:

  • In the form, put the property KeyPreview to true

  • In the event handler KeyPress we are adding the characters to a variable of type string .

  • The end of the bar code must be detected. This is normally configurable in the reader, but it is usually a carriage return. At the moment that the carriage return is detected, you should call the function that treats the barcode.

    string cadena = "";
    
    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (char.IsLetter(e.KeyChar) || char.IsDigit(e.KeyChar))
        {
            cadena += e.KeyChar;
        }
        else
        {
            if (e.KeyChar == (char)13)
            {
                Enviar(cadena); //Como se llame tu método de procesado
                cadena = "";
            }
        }
    
    }
    
  • answered by 11.10.2017 / 13:25
    source