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

Terry Tyson On Best Solutions

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

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).

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

        while (true)
            string data_rx = myport.ReadLine();
            if (data_rx == "1\r")
            else if(data_rx == "2\r")
            else if (data_rx == "3\r")
            else if (data_rx == "4\r")