Solved:
In the arduino code, after the 'for' loop instead of
Serial.println()
(which tells the program to do a carriage return and a new line) you have to put
Serial.print("\n");
which only creates the new line, problema solved.
So the arduino code should end up like this
#include <spi.h>
#include <mfrc522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 nfc(SS_PIN, RST_PIN); // Create nfc instance.
MFRC522::MIFARE_Key key;
String uid;
byte try1[5];
String dato;
int number;
byte readCard[1];
void setup()
{
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
nfc.PCD_Init(); // Init nfc car
}
void loop()
{
if(Serial.available()){
delay(100);
while(Serial.available() > 0){
// statement
dato = Serial.readString();
}
}
if(dato == "ok"){
admited();
dato = "";
}
if(dato == "no"){
noAdmited();
dato = "";
}
if ( ! nfc.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! nfc.PICC_ReadCardSerial())
return;
// Show some details of the PICC (that is: the tag/card)
int contador = 0;
for (byte i = 0; i < nfc.uid.size; i++) {
contador++;
Serial.print(nfc.uid.uidByte[i] < 0x10 ? "0" : "");
Serial.print(nfc.uid.uidByte[i], HEX);
if(contador == 4){
Serial.print("\n");
}
}
delay(500);
}
void admited()
{
Serial.println("La tarjeta ha sido admitida");
}
void noAdmited()
{
Serial.println("La tarjeta no ha sido admitida");
}
And the c# Sharp code is ok but you can remove some uneccesary things... The c# code would end up like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using try1.Properties;
using System.Threading;
namespace try1
{
public partial class MainWindow : Window
{
SerialPort port1 = new SerialPort();
SerialPort port2 = new SerialPort();
string port1_com = Settings.Default.com_port_1;
int port1_baud_rate = Settings.Default.baud_port_1;
private delegate void SetTextDeleg(string text);
string data_saved;
string button_1_data = Settings.Default.button1_data;
string button_2_data = Settings.Default.button2_data;
string button_3_data = Settings.Default.button3_data;
string button_4_data = Settings.Default.button4_data;
string uid = Settings.Default.uid;
string choosen_button;
string completed_uid;
public MainWindow()
{
InitializeComponent();
loadSetup();
loadPort1();
proveUID();
loadButtonsOnComboxAndloadButtons();
serialPortReceiving();
serialPortSending();
}
private void proveUID()
{
if (port1.IsOpen == true)
{
pb_sending.IsIndeterminate = false;
pb_sending.Value = 10;
if (uid == "239A0701")
{
port1.Write("ok");
lb_serial_sending.Content = "";
lb_serial_sending.Content = "Sended 'ok' to serial";
}
else
{
}
}
}
private void loadButtonsOnComboxAndloadButtons()
{
cb_choose_button.Text = choosen_button;
cb_choose_button.Items.Add("Button1");
cb_choose_button.Items.Add("Button2");
cb_choose_button.Items.Add("Button3");
cb_choose_button.Items.Add("Button4");
if (button_1_data == "")
{
button1.Content = "Button1";
}
else
{
button1.Content = button_1_data;
}
if (button_2_data == "")
{
button2.Content = "Button2";
}
else
{
button2.Content = button_2_data;
}
if (button_3_data == "")
{
button3.Content = "Button3";
}
else
{
button3.Content = button_3_data;
}
if (button_4_data == "")
{
button4.Content = "Button4";
}
else
{
button4.Content = button_4_data;
}
}
private void serialPortSending()
{
if (port1.IsOpen == true)
{
pb_sending.Foreground = new SolidColorBrush(System.Windows.Media.Colors.Violet);
pb_sending.IsIndeterminate = true;
}
if (port1.IsOpen == false)
{
pb_sending.Foreground = new SolidColorBrush(System.Windows.Media.Colors.Red);
pb_sending.IsIndeterminate = true;
}
}
private void serialPortReceiving()
{
pb_receiving.Foreground = new SolidColorBrush(System.Windows.Media.Colors.Turquoise);
pb_receiving.IsIndeterminate = true;
if (port1.IsOpen == true)
{
port1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
}
if (port1.IsOpen == false)
{
pb_receiving.Foreground = new SolidColorBrush(System.Windows.Media.Colors.Red);
pb_receiving.IsIndeterminate = true;
}
}
private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(100);
try
{
string data = port1.ReadLine();
Dispatcher.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void si_DataReceived(string data)
{
uid = "";
data_saved = "";
data_saved = data;
tb_data.Text = data_saved;
uid = data;
lb_serial_port.Content = "";
lb_serial_port.Content = data_saved;
pb_receiving.IsIndeterminate = true;
proveUID();
}
private void loadSetup()
{
if (port1.IsOpen == false)
{
pb_connection_1.IsIndeterminate = true;
}
cb_port_1.Text = port1_com;
cb_baud_rate_1.Text = port1_baud_rate.ToString();
lb_status_1.Content = "";
lb_status_1.Content = "Last save port '" + port1_com + "' and baud rate '" + port1_baud_rate + "'";
bt_close_port1.IsEnabled = false;
lb_serial_sending.Content = "";
lb_serial_port.Content = "";
}
private void loadPort1()
{
cb_port_1.Items.Clear();
cb_baud_rate_1.Items.Clear();
foreach (string ports_1 in SerialPort.GetPortNames())
{
cb_port_1.Items.Add(ports_1);
}
cb_baud_rate_1.Items.Add("300");
cb_baud_rate_1.Items.Add("1200");
cb_baud_rate_1.Items.Add("2400");
cb_baud_rate_1.Items.Add("4800");
cb_baud_rate_1.Items.Add("9600");
cb_baud_rate_1.Items.Add("19200");
cb_baud_rate_1.Items.Add("38400");
cb_baud_rate_1.Items.Add("57600");
cb_baud_rate_1.Items.Add("115200");
cb_baud_rate_1.Items.Add("230400");
cb_baud_rate_1.Items.Add("250000");
}
private void cb_port_1_DropDownClosed(object sender, EventArgs e)
{
if (cb_port_1.SelectedItem != null)
{
port1_com = cb_port_1.SelectedItem.ToString();
Settings.Default.com_port_1 = port1_com;
Settings.Default.Save();
lb_status_1.Content = "";
lb_status_1.Content = "You selected " + port1_com;
cb_port_1.Text = port1_com;
}
else
{
MessageBox.Show("You hav to choose something");
}
}
private void cb_baud_rate_1_DropDownClosed(object sender, EventArgs e)
{
if (cb_baud_rate_1.SelectedItem != null)
{
port1_baud_rate = int.Parse(cb_baud_rate_1.Text);
Settings.Default.baud_port_1 = port1_baud_rate;
Settings.Default.Save();
lb_status_1.Content = "";
lb_status_1.Content = "You selected " + port1_baud_rate;
cb_baud_rate_1.Text = port1_baud_rate.ToString();
}
else
{
MessageBox.Show("You have to select something");
}
}
private void bt_connect_1_Click(object sender, RoutedEventArgs e)
{
conneciont_on_port1();
}
private void conneciont_on_port1()
{
pb_connection_1.Foreground = new SolidColorBrush(System.Windows.Media.Colors.Green);
pb_connection_1.IsIndeterminate = true;
if (port1_com != "")
{
if (port1_baud_rate.ToString() != "")
{
try
{
lb_status_1.Content = "";
lb_status_1.Content = "Trying to connect " + port1_com + " at " + port1_baud_rate + " baud rate";
port1.BaudRate = port1_baud_rate;
port1.PortName = port1_com;
port1.Open();
serialPortSending();
serialPortReceiving();
pb_connection_1.IsIndeterminate = false;
pb_connection_1.Value = 10;
lb_status_1.Content = "";
lb_status_1.Content = "Connected succesfully";
bt_connect_1.IsEnabled = false;
bt_close_port1.IsEnabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
pb_connection_1.IsIndeterminate = false;
pb_connection_1.Foreground = new SolidColorBrush(System.Windows.Media.Colors.Red);
pb_connection_1.Value = 10;
lb_status_1.Content = "";
lb_status_1.Content = ex.Message;
bt_connect_1.IsEnabled = true;
bt_close_port1.IsEnabled = false;
}
}
}
else
{
if (port1_com == "")
{
MessageBox.Show("You didn´t select a 'COM' port, please select one");
}
else if (port1_baud_rate.ToString() == "")
{
MessageBox.Show("You have to select a 'BAUD' rate");
}
}
}
private void bt_close_port1_Click(object sender, RoutedEventArgs e)
{
close_port_1();
}
private void close_port_1()
{
if (port1.IsOpen == true)
{
try
{
port1.Close();
lb_status_1.Content = "";
lb_status_1.Content = "The port has been closed succesfully";
pb_connection_1.IsIndeterminate = false;
pb_connection_1.Foreground = new SolidColorBrush(System.Windows.Media.Colors.DodgerBlue);
pb_connection_1.Value = 10;
bt_connect_1.IsEnabled = true;
bt_close_port1.IsEnabled = false;
serialPortReceiving();
serialPortSending();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
pb_connection_1.Foreground = new SolidColorBrush(System.Windows.Media.Colors.Red);
pb_connection_1.Value = 10;
lb_status_1.Content = "";
lb_status_1.Content = ex.Message;
bt_connect_1.IsEnabled = false;
bt_close_port1.IsEnabled = true;
}
}
}
private void bt_refresh_port_1_Click(object sender, RoutedEventArgs e)
{
loadPort1();
pb_connection_1.IsIndeterminate = false;
pb_connection_1.Foreground = new SolidColorBrush(System.Windows.Media.Colors.Coral);
pb_connection_1.Value = 10;
}
private void bt_send_Click(object sender, RoutedEventArgs e)
{
if (port1.IsOpen == true)
{
if (tb_cmd.Text != "")
{
try
{
pb_sending.IsIndeterminate = false;
pb_sending.Foreground = new SolidColorBrush(System.Windows.Media.Colors.Violet);
pb_sending.Value = 10;
port1.Write(tb_cmd.Text);
lb_serial_sending.Content = "";
lb_serial_sending.Content = "Sended " + tb_cmd.Text + " to serial port";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
lb_port1.Content = "";
lb_port1.Content = ex.Message;
pb_sending.IsIndeterminate = false;
pb_sending.Foreground = new SolidColorBrush(System.Windows.Media.Colors.Red);
pb_sending.Value = 10;
}
}
}
else
{
lb_port1.Content = "";
lb_port1.Content = "You are not connected to any port";
}
}
private void tb_cmd_GotMouseCapture(object sender, MouseEventArgs e)
{
pb_sending.IsIndeterminate = true;
}
private void cb_choose_button_DropDownClosed(object sender, EventArgs e)
{
if (cb_choose_button.SelectedItem != null)
{
choosen_button = cb_choose_button.Text;
lb_serial_port.Content = "";
lb_serial_port.Content = choosen_button + " was selected in the comboBox";
}
else
{
MessageBox.Show("You have to select something");
}
}
private void bt_set_cmd_as_Click(object sender, RoutedEventArgs e)
{
choosen_buttonVoid();
}
private void choosen_buttonVoid()
{
if (choosen_button != "")
{
if (choosen_button == "Button1")
{
if (tb_cmd.Text != "")
{
button_1_data = tb_cmd.Text;
lb_serial_port.Content = "Guardando " + button_1_data;
button1.Content = button_1_data;
Settings.Default.button1_data = button_1_data;
Settings.Default.Save();
}
}
if (choosen_button == "Button2")
{
if (tb_cmd.Text != "")
{
button_2_data = tb_cmd.Text;
lb_serial_port.Content = "Guardando " + button_2_data;
button2.Content = button_2_data;
Settings.Default.button2_data = button_2_data;
Settings.Default.Save();
}
}
if (choosen_button == "Button3")
{
if (tb_cmd.Text != "")
{
button_3_data = tb_cmd.Text;
lb_serial_port.Content = "Guardando " + button_3_data;
button3.Content = button_3_data;
Settings.Default.button3_data = button_3_data;
Settings.Default.Save();
}
}
if (choosen_button == "Button4")
{
if (tb_cmd.Text != "")
{
button_4_data = tb_cmd.Text;
lb_serial_port.Content = "Guardando " + button_4_data;
button4.Content = button_4_data;
Settings.Default.button4_data = button_4_data;
Settings.Default.Save();
}
}
}
else
{
MessageBox.Show("Please, write the code on the textBox");
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (port1.IsOpen == true)
{
if (button_1_data != "")
{
port1.Write(button_1_data);
lb_serial_sending.Content = "";
lb_serial_sending.Content = "Sended " + button_1_data + " to serial";
}
}
else
{
lb_serial_sending.Content = "";
lb_serial_sending.Content = "You have to connect to a port";
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (port1.IsOpen == true)
{
if (button_2_data != "")
{
port1.Write(button_2_data);
lb_serial_sending.Content = "";
lb_serial_sending.Content = "Sended " + button_2_data + " to serial";
}
}
else
{
lb_serial_sending.Content = "";
lb_serial_sending.Content = "You have to connect to a port";
}
}
private void button3_Click(object sender, RoutedEventArgs e)
{
if (port1.IsOpen == true)
{
if (button_3_data != "")
{
port1.Write(button_3_data);
lb_serial_sending.Content = "";
lb_serial_sending.Content = "Sended " + button_3_data + " to serial";
}
}
else
{
lb_serial_sending.Content = "";
lb_serial_sending.Content = "You have to connect to a port";
}
}
private void button4_Click(object sender, RoutedEventArgs e)
{
if (port1.IsOpen == true)
{
if (button_4_data != "")
{
port1.Write(button_4_data);
lb_serial_sending.Content = "";
lb_serial_sending.Content = "Sended " + button_4_data + " to serial";
}
}
else
{
lb_serial_sending.Content = "";
lb_serial_sending.Content = "You have to connect to a port";
}
}
}
}
by kardexx1 (the guy who asked the question) :).