How to get value from the serial port?

0

With the code below I get four values ( 1,2,3,4 ) sent by the Arduino serial port / strong>, 3 or 4 values are not sent together, either 1 or 2 / p>

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

How do I get the value that is sent to the serial port to 1 and send it to the MySQL database and send it to another MySQL database?

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();
    
asked by anonymous 09.04.2017 / 23:00

2 answers

0

Friend, do the following. create a class: I called mine from " 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 this:

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

 if(entrada=="1")
 {
  Insert.InserirRegistros("seus parametros...");
 }
    
10.04.2017 / 15:35
1

I've created a method 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...")

  }
    
10.04.2017 / 13:50