Send more than one string to serial

1

I am creating a string through two values, one of them read through a slider and another of a check box. Together the two in a string called a and sending at the same time in the serial.

Follow the code:

private async void SendSerial()
        {

             DirectionRotation = (bool)RotateInverter.IsChecked ? 1 : 0;// if ternario, check box marcado envia 1, check box desmarcado envia 0
            string a = _dataSent1 = $"{Pwm_Control.Value.ToString()},{DirectionRotation}";//cria string de envia para a serial
            if (sp.IsOpen)
            {
                sp.Write(a);//escreve na serial
            }
            else
            {

                await this.ShowMessageAsync("AVISO", "Porta desconectada");
            }

            this.Dispatcher.Invoke(() => { StatusSerialSent.Text = $"StatusSerialSent : {a}"; });// mostra na tela valor que esta sendo enviado

        }

I would like to know how I could send two strings, one in sequence. But with a however, for example the string with the value of check box is only sent when the value of it is changed. Does anyone have a clue how to do it?

    
asked by anonymous 29.11.2017 / 01:29

1 answer

1

You will have to save the previous value to compare with the current one, the PreviousDirectionRotation variable will have to be defined within a scope in which the value does not get lost between requests to the SendSerial () function

var PreviousDirectionRotation; // Variável para guardar o vaor anterior
            string a; // definir a var a fora do if
            DirectionRotation = (bool)RotateInverter.IsChecked ? 1 : 0;

            if (PreviousDirectionRotation != DirectionRotation)//Caso o valor anterior seja diferente do atual envia o valor atual e guarda o novo
            {

                a = _dataSent1 = $"{Pwm_Control.Value.ToString()},{DirectionRotation}";
                    PreviousDirectionRotation = DirectionRotation;
            }
            else // caso seja igual envia apenas o outro valor!
            {
                a = _dataSent1 = $"{Pwm_Control.Value.ToString()}";
            }
    
11.12.2017 / 20:18