Send more than one string to serial

Asked

Viewed 44 times

1

I’m creating a string through two values, one read through a slider and the other through a check box. Put the two together in a string called a and send at the same time in the serial.

Follows 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 to send two strings, one in sequence of the other. But with a however, for example the string with the value of check box is only sent when its value is changed. Someone has a tooltip on how to?

  • I don’t understand what you probably want because information is missing, but I can see problems in this code, and I don’t think any of it is related to the question.

1 answer

1


You will have to save the previous value to compare with the current one, the Previousdirectionrotation variable will have to be set within a Scope in which the value is not "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()}";
            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.