Convert 'int' to 'string'

-1

I am trying to communicate to my arduino with Visual studio C #, everything is fine, the only thing I can not get is that the program recognizes my int as a string, even though I already added the .tostring () to it. here the program that I have been doing so far.

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        SerialPort1.Open();

    }

    private void Port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {

        string Puerto = ("SerialPort1.ReadLine()").ToString();
        if (Puerto = 0) {
            MessageBox.Show("El valor actual es de 0");
        }
        else if (Puerto = 1) {
            MessageBox.Show("El valor actual es de 1");
        }
        else if (Puerto = 2) {
            MessageBox.Show("El valor actual es de 2");
        }
        else if (Puerto = 3) {
            MessageBox.Show("El valor actual es de 3");
        }

    }
}

}

thanks in advance.

    
asked by felipe coronado 17.04.2018 в 05:22
source

1 answer

8

This line is very rare

string Puerto = ("SerialPort1.ReadLine()").ToString();

the string is not evaluated in c #, I do not know why you wrote the ReadLine in quotation marks

private void Port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{

    string line = SerialPort1.ReadLine();

    int puerto = Convert.ToInt32(line);

    MessageBox.Show(string.Format("El valor actual es de {0}", line));

}

You also do not need to use a if to change the message

SerialPort.ReadLine ()

    
answered by 17.04.2018 в 05:41