コード例 #1
0
ファイル: ReadChar.cs プロジェクト: treesportrait/corefx
        public void GreedyRead()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    byte[] byteXmitBuffer = new byte[1024];
                    char   utf32Char      = TCSupport.GenerateRandomCharNonSurrogate();
                    byte[] utf32CharBytes = System.Text.Encoding.UTF32.GetBytes(new[] { utf32Char });
                    int    charRead;

                    Debug.WriteLine("Verifying that ReadChar() will read everything from internal buffer and drivers buffer");

                    //Put the first byte of the utf32 encoder char in the last byte of this buffer
                    //when we read this later the buffer will have to be resized
                    byteXmitBuffer[byteXmitBuffer.Length - 1] = utf32CharBytes[0];

                    TCSupport.SetHighSpeed(com1, com2);

                    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();
                    }

                    com2.Write(byteXmitBuffer, 0, byteXmitBuffer.Length);

                    while (com1.BytesToRead < byteXmitBuffer.Length)
                    {
                        System.Threading.Thread.Sleep(50);
                    }

                    //Read Every Byte except the last one. The last bye should be left in the last position of SerialPort's
                    //internal buffer. When we try to read this char as UTF32 the buffer should have to be resized so
                    //the other 3 bytes of the ut32 encoded char can be in the buffer
                    com1.Read(new char[1023], 0, 1023);

                    Assert.Equal(1, com1.BytesToRead);

                    com1.Encoding = System.Text.Encoding.UTF32;
                    com2.Write(utf32CharBytes, 1, 3);

                    while (com1.BytesToRead < 4)
                    {
                        System.Threading.Thread.Sleep(50);
                    }

                    if (utf32Char != (charRead = com1.ReadChar()))
                    {
                        Fail("Err_6481sfadw ReadChar() returned={0} expected={1}", charRead, utf32Char);
                    }

                    Assert.Equal(0, com1.BytesToRead);
                }
        }
コード例 #2
0
ファイル: ReadExisting.cs プロジェクト: pgovind/runtime
        private void VerifyRead(Encoding encoding, int numberOfBytesToRead, ReadDataFromEnum readDataFrom)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    Random rndGen       = new Random(-55);
                    char[] charsToWrite = new char[numberOfBytesToRead];
                    byte[] bytesToWrite = new byte[numberOfBytesToRead];

                    //Genrate random chars to send
                    for (int i = 0; i < bytesToWrite.Length; i++)
                    {
                        char randChar = (char)rndGen.Next(0, ushort.MaxValue);

                        charsToWrite[i] = randChar;
                    }

                    Debug.WriteLine("Verifying read method endocing={0} with {1} random chars", encoding.EncodingName,
                                    bytesToWrite.Length);

                    com1.ReadTimeout = 500;
                    com1.Encoding    = encoding;

                    TCSupport.SetHighSpeed(com1, com2);

                    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();
                    }

                    bytesToWrite = com1.Encoding.GetBytes(charsToWrite, 0, charsToWrite.Length);

                    switch (readDataFrom)
                    {
                    case ReadDataFromEnum.NonBuffered:
                        VerifyReadNonBuffered(com1, com2, bytesToWrite);
                        break;

                    case ReadDataFromEnum.Buffered:
                        VerifyReadBuffered(com1, com2, bytesToWrite);
                        break;

                    case ReadDataFromEnum.BufferedAndNonBuffered:
                        VerifyReadBufferedAndNonBuffered(com1, com2, bytesToWrite);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(readDataFrom), readDataFrom, null);
                    }
                }
        }
コード例 #3
0
        private void VerifyRead(Encoding encoding, ReadDataFromEnum readDataFrom)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    Random rndGen         = new Random(-55);
                    int    bufferSize     = numRndByte;
                    byte[] byteXmitBuffer = new byte[bufferSize];

                    //Genrate random bytes
                    for (int i = 0; i < byteXmitBuffer.Length; i++)
                    {
                        byteXmitBuffer[i] = (byte)rndGen.Next(0, 256);
                    }

                    com1.ReadTimeout = 500;
                    com1.Encoding    = encoding;

                    TCSupport.SetHighSpeed(com1, com2);

                    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();
                    }

                    switch (readDataFrom)
                    {
                    case ReadDataFromEnum.NonBuffered:
                        VerifyReadNonBuffered(com1, com2, byteXmitBuffer);
                        break;

                    case ReadDataFromEnum.Buffered:
                        VerifyReadBuffered(com1, com2, byteXmitBuffer);
                        break;

                    case ReadDataFromEnum.BufferedAndNonBuffered:
                        VerifyReadBufferedAndNonBuffered(com1, com2, byteXmitBuffer);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(readDataFrom));
                    }
                }
        }
コード例 #4
0
        private void VerifyWrite(System.Text.Encoding encoding, int strSize, string newLine, int numWrites)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    string stringToWrite = TCSupport.GetRandomString(NEWLINE_TESTING_STRING_SIZE, TCSupport.CharacterOptions.None);

                    TCSupport.SetHighSpeed(com1, com2);

                    com1.Encoding = encoding;
                    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();
                    }

                    VerifyWriteLine(com1, com2, stringToWrite, numWrites);
                }
        }
コード例 #5
0
        private void VerifyRead(Encoding encoding, ReadDataFromEnum readDataFrom, int bufferSize)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    char[] charXmitBuffer = TCSupport.GetRandomChars(bufferSize, false);
                    byte[] byteXmitBuffer = encoding.GetBytes(charXmitBuffer);

                    com1.ReadTimeout = 500;
                    com1.Encoding    = encoding;

                    TCSupport.SetHighSpeed(com1, com2);

                    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();
                    }

                    switch (readDataFrom)
                    {
                    case ReadDataFromEnum.NonBuffered:
                        VerifyReadNonBuffered(com1, com2, byteXmitBuffer);
                        break;

                    case ReadDataFromEnum.Buffered:
                        VerifyReadBuffered(com1, com2, byteXmitBuffer);
                        break;

                    case ReadDataFromEnum.BufferedAndNonBuffered:
                        VerifyReadBufferedAndNonBuffered(com1, com2, byteXmitBuffer);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(readDataFrom), readDataFrom, null);
                    }
                }
        }
コード例 #6
0
        private void VerifyReadExisting(Encoding encoding)
        {
            // TODO - this test text did not come across from legacy properly.
            string     text        = "????????????4??????????????????,?11????????????????????????????????????????????????,????????????,??????????";
            string     receivedstr = "";
            SerialPort com1;
            SerialPort com2;

            using (com1 = TCSupport.InitFirstSerialPort())
                using (com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    com1.ReadTimeout = 500;
                    com1.Encoding    = encoding;
                    com2.Encoding    = encoding;

                    TCSupport.SetHighSpeed(com1, com2);

                    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();
                    }

                    com2.DataReceived += (sender, args) =>
                    {
                        receivedstr += com2.ReadExisting();
                    };

                    com1.Write(text);

                    //3 seconds is more than enough time to write a few bytes to the other port
                    TCSupport.WaitForPredicate(() => receivedstr.Length >= text.Length, 3000,
                                               "Data was not returned in a timely fashion.  Timeout");

                    Assert.Equal(text, receivedstr);
                }
        }
コード例 #7
0
        private void VerifyReadExisting(Encoding encoding)
        {
            string text = "Za\u017C\u00F3\u0142\u0107 g\u0119\u015Bl\u0105 ja\u017A\u0144";

            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    com1.ReadTimeout = 500;
                    com1.Encoding    = encoding;
                    com2.Encoding    = encoding;

                    TCSupport.SetHighSpeed(com1, com2);

                    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();
                    }

                    string receivedstr = "";
                    com2.DataReceived += (sender, args) =>
                    {
                        receivedstr += com2.ReadExisting();
                    };

                    com1.Write(text);

                    //3 seconds is more than enough time to write a few bytes to the other port
                    TCSupport.WaitForPredicate(() => receivedstr.Length >= text.Length, 3000,
                                               "Data was not returned in a timely fashion.  Timeout");

                    Assert.Equal(text, receivedstr);
                }
        }
コード例 #8
0
ファイル: ReadTo.cs プロジェクト: vml-t/corefx
        private void VerifyRead(Encoding encoding, string newLine, int numBytesRead, int numNewLines, ReadDataFromEnum readDataFrom)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    Random        rndGen = new Random(-55);
                    StringBuilder strBldrToWrite;
                    int           numNewLineChars = newLine.ToCharArray().Length;
                    int           minLength       = (1 + numNewLineChars) * numNewLines;

                    if (minLength < numBytesRead)
                    {
                        strBldrToWrite = TCSupport.GetRandomStringBuilder(numBytesRead, TCSupport.CharacterOptions.None);
                    }
                    else
                    {
                        strBldrToWrite = TCSupport.GetRandomStringBuilder(rndGen.Next(minLength, minLength * 2),
                                                                          TCSupport.CharacterOptions.None);
                    }

                    //We need place the newLine so that they do not write over eachother
                    int divisionLength = strBldrToWrite.Length / numNewLines;
                    int range          = divisionLength - numNewLineChars;

                    for (int i = 0; i < numNewLines; i++)
                    {
                        int newLineIndex = rndGen.Next(0, range + 1);

                        strBldrToWrite.Insert(newLineIndex + (i * divisionLength) + (i * numNewLineChars), newLine);
                    }

                    Debug.WriteLine("Verifying ReadTo encoding={0}, newLine={1}, numBytesRead={2}, numNewLines={3}", encoding,
                                    newLine, numBytesRead, numNewLines);

                    com1.ReadTimeout = 500;
                    com1.Encoding    = encoding;

                    TCSupport.SetHighSpeed(com1, com2);

                    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();
                    }

                    switch (readDataFrom)
                    {
                    case ReadDataFromEnum.NonBuffered:
                        VerifyReadNonBuffered(com1, com2, strBldrToWrite.ToString(), newLine);
                        break;

                    case ReadDataFromEnum.Buffered:
                        VerifyReadBuffered(com1, com2, strBldrToWrite.ToString(), newLine);
                        break;

                    case ReadDataFromEnum.BufferedAndNonBuffered:
                        VerifyReadBufferedAndNonBuffered(com1, com2, strBldrToWrite.ToString(), newLine);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(readDataFrom), readDataFrom, null);
                    }
                }
        }
コード例 #9
0
        private void VerifyRead(char[] buffer, int offset, int count, Encoding encoding, int numberOfBytesToRead,
                                ReadDataFromEnum readDataFrom)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    Random rndGen = new Random(-55);
                    char[] charsToWrite;
                    char[] expectedChars = new char[numberOfBytesToRead];
                    byte[] bytesToWrite  = new byte[numberOfBytesToRead];

                    if (1 < count)
                    {
                        charsToWrite = TCSupport.GetRandomChars(numberOfBytesToRead, TCSupport.CharacterOptions.Surrogates);
                    }
                    else
                    {
                        charsToWrite = TCSupport.GetRandomChars(numberOfBytesToRead, TCSupport.CharacterOptions.None);
                    }

                    //Genrate some random chars in the buffer
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        char randChar = (char)rndGen.Next(0, ushort.MaxValue);

                        buffer[i] = randChar;
                    }

                    TCSupport.SetHighSpeed(com1, com2);

                    Debug.WriteLine(
                        "Verifying read method buffer.Length={0}, offset={1}, count={2}, endocing={3} with {4} random chars",
                        buffer.Length, offset, count, encoding.EncodingName, bytesToWrite.Length);
                    com1.ReadTimeout = 500;
                    com1.Encoding    = encoding;
                    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();
                    }

                    bytesToWrite  = com1.Encoding.GetBytes(charsToWrite, 0, charsToWrite.Length);
                    expectedChars = com1.Encoding.GetChars(bytesToWrite, 0, bytesToWrite.Length);

                    switch (readDataFrom)
                    {
                    case ReadDataFromEnum.NonBuffered:
                        VerifyReadNonBuffered(com1, com2, bytesToWrite, buffer, offset, count);
                        break;

                    case ReadDataFromEnum.Buffered:
                        VerifyReadBuffered(com1, com2, bytesToWrite, buffer, offset, count);
                        break;

                    case ReadDataFromEnum.BufferedAndNonBuffered:
                        VerifyReadBufferedAndNonBuffered(com1, com2, bytesToWrite, buffer, offset, count);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(readDataFrom), readDataFrom, null);
                    }
                }
        }
コード例 #10
0
        public void GreedyRead()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    Random rndGen         = new Random(-55);
                    char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.Surrogates);
                    byte[] byteXmitBuffer = new byte[1024];
                    char   utf32Char      = (char)8169;
                    byte[] utf32CharBytes = Encoding.UTF32.GetBytes(new[] { utf32Char });
                    int    numCharsRead;

                    Debug.WriteLine(
                        "Verifying that Read(char[], int, int) will read everything from internal buffer and drivers buffer");

                    for (int i = 0; i < byteXmitBuffer.Length; i++)
                    {
                        byteXmitBuffer[i] = (byte)rndGen.Next(0, 256);
                    }

                    //Put the first byte of the utf32 encoder char in the last byte of this buffer
                    //when we read this later the buffer will have to be resized
                    byteXmitBuffer[byteXmitBuffer.Length - 1] = utf32CharBytes[0];

                    TCSupport.SetHighSpeed(com1, com2);

                    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();
                    }

                    com2.Write(byteXmitBuffer, 0, byteXmitBuffer.Length);

                    TCSupport.WaitForReadBufferToLoad(com1, byteXmitBuffer.Length);

                    //Read Every Byte except the last one. The last bye should be left in the last position of SerialPort's
                    //internal buffer. When we try to read this char as UTF32 the buffer should have to be resized so
                    //the other 3 bytes of the ut32 encoded char can be in the buffer
                    com1.Read(new char[1023], 0, 1023);
                    Assert.Equal(1, com1.BytesToRead);

                    com1.Encoding = Encoding.UTF32;
                    com2.Encoding = Encoding.UTF32;

                    com2.Write(utf32CharBytes, 1, 3);
                    com2.Write(charXmitBuffer, 0, charXmitBuffer.Length);

                    int numBytes = Encoding.UTF32.GetByteCount(charXmitBuffer);

                    byte[] byteBuffer = Encoding.UTF32.GetBytes(charXmitBuffer);

                    var expectedChars = new char[1 + Encoding.UTF32.GetCharCount(byteBuffer)];
                    expectedChars[0] = utf32Char;

                    Encoding.UTF32.GetChars(byteBuffer, 0, byteBuffer.Length, expectedChars, 1);
                    var charRcvBuffer = new char[(int)(expectedChars.Length * 1.5)];

                    TCSupport.WaitForReadBufferToLoad(com1, 4 + numBytes);

                    if (expectedChars.Length != (numCharsRead = com1.Read(charRcvBuffer, 0, charRcvBuffer.Length)))
                    {
                        Fail("Err_6481sfadw Expected read to read {0} chars actually read {1}", expectedChars.Length,
                             numCharsRead);
                    }

                    Assert.Equal(expectedChars, charRcvBuffer.Take(expectedChars.Length).ToArray());

                    Assert.Equal(0, com1.BytesToRead);
                }
        }