-1

In my app I am sending data to microcontroller. I send data, microcontroller do program, and send character ("K"). My application should wait for this character.After receiving this char, it should send data again.

I got problem with receiving this character. Is function BytesToRead right to reading character? My program always fall when it reach this my function wait

static void wait()
    {
        SerialPort COMport = new SerialPort();
        int znak;

        COMport.PortName = "COM6"; // 
        COMport.BaudRate = 1200;
        COMport.DataBits = 8;
        COMport.Parity = Parity.None;
        COMport.StopBits = StopBits.One;

        COMport.Open();

        do
        {
            znak = COMport.BytesToRead;
        } while (znak != 75);   // ASCII K = 75

        COMport.Close();
        return;
    }
user5939530
  • 51
  • 1
  • 6
  • `BytesToRead` gives you the size of any data in the receive buffer - i.e. it does not give you the data itself. – Chris May 06 '16 at 16:24

2 Answers2

0

The BytesToRead property of the COMport returns the number of characters that have been received by the COMport so your loop will continue until there are exactly 75 characters read. Take a look at the documentation for the SerialPort class. It will show you a good example of how to read/write characters from/to your COMport.

dstroupe
  • 98
  • 5
0

Why not just use while(COMport.ReadChar() != 'K') { /* Do Stuff */ }?

Ingenioushax
  • 718
  • 5
  • 20