Framework 2.0 Serial Port problem.

I cannot for the life of me figure out why my little bit of code doesn't work.

Problem I'm seeing is ReadExisting() returns 1 single character followed by (BytesRemainingInBuffer-1) 0's. OnReceive events fire properly, but each call to ReadExisting returns only that one character.

data = comport.ReadExisting();

bytes = comport.BytesToRead;

status.Text = "READ: " + data.Length.ToString() + " EXPECTING: " + bytes.ToString();

For instance, suppose the buffer has "12345":

  1. returns "1\0\0\0\0"
  2. returns "2\0\0\0"
  3. returns "3\0\0"
  4. returns "4\0"
  5. returns "5"

Anyone else see a similar problem

Don Lafontaine

 

 




Answer this question

Framework 2.0 Serial Port problem.

  • Antony Nguyen

    I was unable to reproduce the problem that you are seeing. I used the following code to send "12345" over a null modem cable between COM1 and COM2 and ReadExisting returned the full string "12345".

    SerialPort com1 = new SerialPort("COM1");
    SerialPort com2 = new SerialPort("COM2");
    string receivedString;

    com1.Open();
    com2.Open();

    com1.Write("12345");

    while(com2.BytesToRead != 5) {
        System.Threading.Thread.Sleep(100);
    }

    receivedString = com2.ReadExisting();

    Console.WriteLine(receivedString);
    Console.WriteLine(receivedString.Length);

    Please let me know what you are doing differently.

    Ryan Byingtion [MS]



  • Dave Elliott Msdn

    Punt.

  • Framework 2.0 Serial Port problem.