COM serial port

Asked

Viewed 236 times

-1

I’m trying to execute a code every time, where it takes the value sent by the COM port of the Arduino, prints in a textbox and soon after generates an Insert in the mysql database.

Only that he read what was sent through the COM port and does not execute the Insert. Help me.

 private void timer1_Tick_1(object sender, EventArgs e)
    {
        serialPort1.Open();
        string entrada = serialPort1.ReadLine();
        serialPort1.Close();
        txtIDTag.Text = entrada;

        if (txtIDTag.Text == "Gol")
        {
            string sql = "insert into tcc (Carro,Placa,Fabricante,Ano,Cor,Status,Data) value ('" + txtIDTag.Text + "','SCD - 4365','VW','2010','PRETO','NOVO', NOW())";

            comando.CommandText = sql;

            conexao.Open();

            comando.ExecuteNonQuery();

            conexao.Close();

            MessageBox.Show("Dados cadastrados com sucesso.");

            txtIDTag.Text = "";
        }}

I tried to get the IF so he executed.

  • Why doesn’t it run? Because the text is different from Gol, because it can’t connect to the bank or because it is an error?

  • I have already tested the two tags I have. The one that is as goal and the other that define as Palio. The problem is that not Insert with either.

  • But in the textbox appears correctly? What appears? Are you sure there are no extra characters in the string? Why not if using the value of entrada?

  • This is correct yes. I did what colleague Rodrigo said. Put the different sign and he executed correctly. I can not explain the reason. Would anyone have any opinion on how to correct?

  • It’s not doing right no. ta jumping the if and executing what’s inside. I put two ifs now and it just executes what’s inside by jumping the if’s.

1 answer

1


Probably is pq the return of the serial port is done in byte, at least in Java is like this, try to find something to concatenate the information. For the return passes as follows

Byte G -> Byte O -> Byte L

so if you compare only the G maybe it works.

Browser other questions tagged

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