public void Read_ResizeBuffer() { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1)) { char[] charXmitBuffer = TCSupport.GetRandomChars(1023, TCSupport.CharacterOptions.ASCII); int readResult; Debug.WriteLine("Verifying that Read(char[], int, int) will compact data in the buffer"); com1.Encoding = Encoding.ASCII; com2.Encoding = Encoding.ASCII; com1.Open(); if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback { com2.Open(); } //[] Fill the buffer up then read in all but one of the chars var expectedChars = new char[charXmitBuffer.Length - 1]; var charRcvBuffer = new char[charXmitBuffer.Length - 1]; Array.Copy(charXmitBuffer, expectedChars, charXmitBuffer.Length - 1); com2.Write(charXmitBuffer, 0, charXmitBuffer.Length); TCSupport.WaitForPredicate(() => com1.BytesToRead == charXmitBuffer.Length, 2000, "Err_29829haie Expected to received {0} bytes actual={1}", charXmitBuffer.Length, com1.BytesToRead); if (charXmitBuffer.Length - 1 != (readResult = com1.Read(charRcvBuffer, 0, charXmitBuffer.Length - 1))) { Fail("Err_55084aheid Expected to read {0} chars actual {1}", charXmitBuffer.Length - 1, readResult); } TCSupport.VerifyArray(expectedChars, charRcvBuffer); //[] Write 16 more cahrs and read in 16 chars expectedChars = new char[16]; charRcvBuffer = new char[16]; expectedChars[0] = charXmitBuffer[charXmitBuffer.Length - 1]; Array.Copy(charXmitBuffer, 0, expectedChars, 1, 15); com2.Write(charXmitBuffer, 0, 16); TCSupport.WaitForPredicate(() => com1.BytesToRead == 17, 2000, "Err_0516848aied Expected to received {0} bytes actual={1}", 17, com1.BytesToRead); if (16 != (readResult = com1.Read(charRcvBuffer, 0, 16))) { Fail("Err_650848ahide Expected to read {0} chars actual {1}", 16, readResult); } Assert.Equal(expectedChars, charRcvBuffer); //[] Write more chars and read in all of the chars expectedChars = new char[charXmitBuffer.Length + 1]; charRcvBuffer = new char[charXmitBuffer.Length + 1]; expectedChars[0] = charXmitBuffer[15]; Array.Copy(charXmitBuffer, 0, expectedChars, 1, charXmitBuffer.Length); com2.Write(charXmitBuffer, 0, charXmitBuffer.Length); TCSupport.WaitForPredicate(() => com1.BytesToRead == charXmitBuffer.Length + 1, 2000, "Err_41515684 Expected to received {0} bytes actual={1}", charXmitBuffer.Length + 2, com1.BytesToRead); if (charXmitBuffer.Length + 1 != (readResult = com1.Read(charRcvBuffer, 0, charXmitBuffer.Length + 1))) { Fail("Err_460574ajied Expected to read {0} chars actual {1}", charXmitBuffer.Length + 1, readResult); } Assert.Equal(expectedChars, charRcvBuffer); } }