C# Arduino Serial read....I dont know how to read it oustide a while loop, it wont work with the rest of the code outside a while loop

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).

➜ please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)


this seems more a windows programming question than an Arduino question...

try

using System;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApp1
{
    class Program
    {
        static SerialPort arduino;
        public static void Main()
        {
            arduino = new SerialPort();
            arduino.PortName = "COM4";//Set your board COM
            arduino.BaudRate = 9600;
            arduino.Open();
            while (true) // you'll be stuck in there
            {
                string a = arduino.ReadExisting();
                Console.WriteLine(a);
                Thread.Sleep(10);
            }
        }
    }
}

if you don't want to be stuck in the while loop then check if something is available for reading and build up a message (up until some end marker) and then parse the message once you got it. ➜ asynchronous code