I am sending data from Arduino to console. After receiving the data trigger event. But I am facing this error right now "cannot implicitly convert type 'char' to 'string'"

enter image description here

3 Answers

5
Terry Tyson On Best Solutions

Change your single quotes to double quotes. Single quotes are for characters and double quotes are for strings.

1
Wilbur Omae On

Cannot implicitly convert type 'char' to 'string'

You may want to check out casting and type conventions. See docs. You may also want to check out the reasons given for the C# design team not implementing char to string implicit conversion in this other question on SO (particularly check Eric Lippert's answer).

Another flow that is evident in your code is the confusion between the assignment operator = and the equality check ==. It's a common source of bugs. Always check it out when doing conditionals.

I also notice you have an infinite loop i.e. while (true). Just ensure it does not become an infinite loop (if you know what I mean :D).

0
Subham Samir On
        SerialPort myport = new SerialPort();
        myport.BaudRate = 115200;
        myport.PortName = "COM14";
        myport.Open();

        while (true)
        {
            string data_rx = myport.ReadLine();
            Console.WriteLine(data_rx);
            if (data_rx == "1\r")
            {
                Console.WriteLine("up");
            }
            else if(data_rx == "2\r")
            {
                Console.WriteLine("Down");
            }
            else if (data_rx == "3\r")
            {
                Console.WriteLine("Left");
            }
            else if (data_rx == "4\r")
            {
                Console.WriteLine("right");
            }