How do you get value from the serial port?

Asked

Viewed 548 times

0

With the code below I get four values (1,2,3,4) sent by the serial port of Arduous, values are not sent together or will come out 1 or 2 or 3 or 4.

serialPort1.Open();
string entrada = serialPort1.ReadLine();
txtIDTag.Text = entrada;
serialPort1.Close();

How do I so that if the value that is sent to the serial port is 1 I send information to the Mysql database and if 2 is sent another?

Mysql code

 caminho = "server=192.168.0.17; user id=RFID; pwd=1234; database=roubo_furto";
        conexao = new MySqlConnection(caminho);
        comando = new MySqlCommand();
        comando.Connection = conexao;


string sql = " insert into tcc (Carro,Placa,Fabricante,Ano,Cor,Status,Data) ";
sql += " value ('GOL','SCD - 4365','VW','2010','PRETO','ROUBO / FURTO', NOW())";
comando.CommandText = sql;
conexao.Open();
comando.ExecuteNonQuery();
conexao.Close();

2 answers

1

I created a way to save the data:

  public void InserirRegistros(string Carro, string Placa, string Fabricante, string Ano, string Status, string Cor, string Data)
  {

    try
    {
        conn.Open();
        NpgsqlTransaction transacao = conn.BeginTransaction();
        StringBuilder sb = new StringBuilder();
        String sqlTcc = "INSERT INTO tcc (Carro,Placa,Fabricante,Ano,Cor,Status,Data) VALUES (@Carro,@Placa,@Fabricante,@Ano,@Cor,@Status,@Data)";
        NpgsqlCommand cmdTcc = new NpgsqlCommand(sqlTcc, conn, transacao);
        cmdTcc.Parameters.AddWithValue("@Carro", Carro);
        cmdTcc.Parameters.AddWithValue("@Placa", Placa);
        cmdTcc.Parameters.AddWithValue("@Fabricante", Fabricante);
        cmdTcc.Parameters.AddWithValue("@Ano", Ano);
        cmdTcc.Parameters.AddWithValue("@Cor", Cor);
        cmdTcc.Parameters.AddWithValue("@Status", Status);
        cmdTcc.Parameters.AddWithValue("@Data", Data);

        cmdTcc.ExecuteNonQuery();

        transacao.Commit();
        conn.Close();

    }
    catch (NpgsqlException ex)
    {
        throw ex;

    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        conn.Close();
    }
}

then you just call the method:

 serialPort1.Open();
 string entrada = serialPort1.ReadLine();
 txtIDTag.Text = entrada;
 serialPort1.Close();


  if(entrada=="1")
  {
    InserirRegistros("Seus parametros...")

  }
  • I have error in the "Npgsqltransaction" section. I added more information in the post

0


Friend, do the following. create a class: I called mine "Dal" mine is like this>

 using Npgsql;
 using System;
 using System.Collections.Generic;
 using System.Data;
 using System.Text;

public class Dal
{

static string serverName = "10.1.1.2";
static string port = "5438";                                                          
static string userName = "username";                                              
static string password = "suasenha";                                           
static string databaseName = "seubanco";

NpgsqlConnection conn = null;
string connString = null;
public Dal()
{
    connString = String.Format("Host={0};Port={1};User Id={2};Password={3};Database={4}; Pooling=false; Timeout=100",
                                           serverName, port, userName, password, databaseName);

    conn = new NpgsqlConnection(connString);
    conn.Close();
}

public void InserirRegistros(string Carro, string Placa, string Fabricante, string Ano, string Cor, string Status, string Data)
{
    try
    {
        conn.Open();
        NpgsqlTransaction transacao = conn.BeginTransaction();
        StringBuilder sb = new StringBuilder();
        String sqlTcc = "INSERT INTO tcc (Carro,Placa,Fabricante,Ano,Cor,Status,Data) VALUES (@Carro,@Placa,@Fabricante,@Ano,@Cor,@Status,@Data)";
        NpgsqlCommand cmdTcc = new NpgsqlCommand(sqlTcc, conn, transacao);
        cmdTcc.Parameters.AddWithValue("@Carro", Carro);
        cmdTcc.Parameters.AddWithValue("@Placa", Placa);
        cmdTcc.Parameters.AddWithValue("@Fabricante", Fabricante);
        cmdTcc.Parameters.AddWithValue("@Ano", Ano);
        cmdTcc.Parameters.AddWithValue("@Cor", Cor);
        cmdTcc.Parameters.AddWithValue("@Status", Status);
        cmdTcc.Parameters.AddWithValue("@Data", Data);

        cmdTcc.ExecuteNonQuery();

        transacao.Commit();
        conn.Close();

    }
    catch (NpgsqlException ex)
    {
        throw ex;

    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        conn.Close();
    }
}
}

And then do it:

serialPort1.Open();
string entrada = serialPort1.ReadLine();
txtIDTag.Text = entrada;
serialPort1.Close();
Dal Insert = new Dal();

 if(entrada=="1")
 {
  Insert.InserirRegistros("seus parametros...");
 }
  • Sorry @Anderson Machado. I’m new to programming and would like something less complex. I appreciate the intention to help

  • So, young man... all you have to do is create the class, pass on the information connected to your bank. and then out of the class you instance it Dal Insert = new Dal(); and then Insert.Inserting records("your parameters..."); Hugs!

Browser other questions tagged

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