Get the selected entry to only update this when serial scanning

ElixiumElixium Member ✭✭
edited December 2018 in Xamarin.Forms

Hello,

I have been working with scanner devices, only using keyboard interfaces and i had no problems, because you only have to select the entry where you want to "paste" the value of the scanned barcode, and scan. Nothing more, the scanned value actually updates the entry value where you are pointing to.

But now that i have to use the serial interface to use COM ports, and i have a problem.

What i have succesfully done since now is:

1- open the serial port where my barcode scanner device is connected,

{
            serialPort = new SerialPort();

            Close();

            serialPort.PortName = portName;
            serialPort.BaudRate = baudRate;
            serialPort.Parity = parity;
            serialPort.DataBits = dataBits;
            serialPort.StopBits = stopBits;

            // Set the read/write timeouts
            serialPort.ReadTimeout = 500;
            serialPort.WriteTimeout = 500;

            serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            try
            {

                serialPort.Open();
                this.IsOpen = true;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            return true;
        }

2- Listen to the scan event and update the value of the "ScannedText" variable (located in BaseViewModel.cs) to show the barcode value on the xaml:

private void DataReceivedHandler(object sender, EventArgs e) //SerialDataReceivedEventArgs
        {
            SerialPort sp = (SerialPort)sender;
            text = sp.ReadExisting();

            //Send the value to the ViewModel
            BaseViewModel.Current.ScannedText = text;
        }

Xaml:

<Entry x:Name="BarcodeEntry" Text="{Binding ScannedText, Source={x:Static base:BaseViewModel.Current}}"

THIS WORKS, the value of the scanned barcode is printed on the xaml entries where the binding variable is the "ScannedText" variable.

But what if i have more than one entry on my page and i want to update ONLY the entry value where i'm actually pointing, and do nothing to the other one? I guess i don't should use variables for this and send the entry to the event, but don't know how. I'm really stucked.

Thanks!

Sign In or Register to comment.