public void ReceivedBytesThreshold_Rnd_MultipleWrite()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1);
                    SerialPortProperties serPortProp     = new SerialPortProperties();

                    Random rndGen = new Random(-55);
                    int    receivedBytesThreshold = rndGen.Next(MIN_RND_THRESHOLD, MAX_RND_THRESHOLD);

                    com1.ReceivedBytesThreshold = receivedBytesThreshold;
                    com1.Open();
                    com2.Open();
                    com1.DataReceived += rcvEventHandler.HandleEvent;

                    serPortProp.SetAllPropertiesToOpenDefaults();
                    serPortProp.SetProperty("ReceivedBytesThreshold", receivedBytesThreshold);
                    serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

                    Debug.WriteLine("Verifying writing the number of bytes of ReceivedBytesThreshold after several write calls");

                    com2.Write(new byte[(int)Math.Floor(com1.ReceivedBytesThreshold / 2.0)], 0,
                               (int)Math.Floor(com1.ReceivedBytesThreshold / 2.0));
                    com2.Write(new byte[(int)Math.Ceiling(com1.ReceivedBytesThreshold / 2.0)], 0,
                               (int)Math.Ceiling(com1.ReceivedBytesThreshold / 2.0));

                    rcvEventHandler.WaitForEvent(SerialData.Chars, MAX_TIME_WAIT);

                    com1.DiscardInBuffer();

                    serPortProp.VerifyPropertiesAndPrint(com1);
                    rcvEventHandler.Validate(SerialData.Chars, com1.ReceivedBytesThreshold, 0);
                }
        }
Exemple #2
0
        public void DsrHolding_true_false()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    SerialPortProperties serPortProp = new SerialPortProperties();

                    Debug.WriteLine("Verifying DsrHolding=true then false on com1 when com2.DtrEnable=true then false");

                    serPortProp.SetAllPropertiesToOpenDefaults();
                    serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
                    serPortProp.SetProperty("DsrHolding", true);

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

                    com2.DtrEnable = true;
                    serPortProp.SetProperty("CDHolding", com1.CDHolding);
                    //We dont care what this is set to since some serial cables loop CD to CTS
                    serPortProp.VerifyPropertiesAndPrint(com1);

                    com2.DtrEnable = false;
                    serPortProp.SetProperty("CDHolding", com1.CDHolding);
                    //We dont care what this is set to since some serial cables loop CD to CTS
                    serPortProp.SetProperty("DsrHolding", false);
                    serPortProp.VerifyPropertiesAndPrint(com1);
                }
        }
        public void ReceivedBytesThreshold_Default()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1);
                    SerialPortProperties serPortProp     = new SerialPortProperties();

                    com1.Open();
                    com2.Open();
                    com1.DataReceived += rcvEventHandler.HandleEvent;

                    serPortProp.SetAllPropertiesToOpenDefaults();
                    serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

                    Debug.WriteLine("Verifying default ReceivedBytesThreshold");

                    com2.Write(new byte[1], 0, 1);

                    rcvEventHandler.WaitForEvent(SerialData.Chars, MAX_TIME_WAIT);

                    com1.DiscardInBuffer();

                    serPortProp.VerifyPropertiesAndPrint(com1);
                    rcvEventHandler.Validate(SerialData.Chars, 1, 0);
                }
        }
Exemple #4
0
        public void CtsHolding_true_local_close()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    SerialPortProperties serPortProp = new SerialPortProperties();

                    Debug.WriteLine(
                        "Verifying CtsHolding=true then false on com1 when com2.RtsEnable=true then com1 is closed");

                    serPortProp.SetAllPropertiesToOpenDefaults();
                    serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
                    serPortProp.SetProperty("CtsHolding", true);

                    com1.Open();
                    com2.Open();
                    com2.RtsEnable = true;

                    serPortProp.VerifyPropertiesAndPrint(com1);

                    if (com1.IsOpen)
                    {
                        com1.Close();
                    }

                    serPortProp.SetAllPropertiesToDefaults();
                    serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
                    serPortProp.VerifyPropertiesAndPrint(com1);
                }
        }
Exemple #5
0
    public bool DiscardNull_false_flase_Default_ReadByte()
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

        Console.WriteLine("Verifying default DiscardNull with ReadByte");

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        com1.Open();
        com1.DiscardNull = false;
        com1.DiscardNull = false;

        serPortProp.SetProperty("DiscardNull", false);

        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);
        retValue &= VerifyDiscardNull(com1, new ReadMethodDelegate(ReadByte), false);
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        if (!retValue)
        {
            Console.WriteLine("Err_4088ahjied!!! Verifying default DiscardNull with ReadByte FAILED");
        }

        if (com1.IsOpen)
        {
            com1.Close();
        }

        return(retValue);
    }
Exemple #6
0
        private void VerifyBytesToRead(ReadMethodDelegate readMethod, int bufferSize, bool sendNewLine)
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    SerialPortProperties serPortProp = new SerialPortProperties();
                    int numNewLineBytes = 0;

                    serPortProp.SetAllPropertiesToOpenDefaults();
                    serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

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

                    com2.Write(new byte[bufferSize], 0, bufferSize);

                    if (sendNewLine)
                    {
                        com2.Write(com2.NewLine);
                        numNewLineBytes = com2.Encoding.GetByteCount(com2.NewLine.ToCharArray());
                    }

                    while (bufferSize + numNewLineBytes > com1.BytesToRead)
                    {
                        System.Threading.Thread.Sleep(10);
                    }

                    readMethod(com1, bufferSize + numNewLineBytes);
                    serPortProp.VerifyPropertiesAndPrint(com1);
                }
        }
Exemple #7
0
    private void VerifyBytesToWrite(WriteMethodDelegate writeMethod, int bufferSize, bool sendNewLine)
    {
        using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
            {
                SerialPortProperties serPortProp = new SerialPortProperties();
                int expectedBytesToWrite;
                int actualBytesToWrite;

                serPortProp.SetAllPropertiesToOpenDefaults();
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
                serPortProp.SetProperty("Handshake", Handshake.RequestToSend);
                serPortProp.SetProperty("WriteTimeout", 500);

                com1.Open();
                com1.WriteTimeout = 500;
                com1.Handshake    = Handshake.RequestToSend;

                Task <int> task = Task.Run(() => writeMethod(com1, bufferSize));

                System.Threading.Thread.Sleep(200);

                actualBytesToWrite   = com1.BytesToWrite;
                expectedBytesToWrite = task.Result;

                com2.Open();
                com2.RtsEnable = true;

                Assert.Equal(expectedBytesToWrite, actualBytesToWrite);

                com2.RtsEnable = false;
                serPortProp.VerifyPropertiesAndPrint(com1);
            }
    }
Exemple #8
0
    public void ReadBufferSize_Default()
    {
        using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
        {
            SerialPortProperties serPortProp = new SerialPortProperties();

            Debug.WriteLine("Verifying default ReadBufferSize before Open");

            serPortProp.SetAllPropertiesToDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
            serPortProp.VerifyPropertiesAndPrint(com1);

            Debug.WriteLine("Verifying default ReadBufferSize after Open");

            com1.Open();
            serPortProp = new SerialPortProperties();
            serPortProp.SetAllPropertiesToOpenDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
            serPortProp.VerifyPropertiesAndPrint(com1);

            Debug.WriteLine("Verifying default ReadBufferSize after Close");

            com1.Close();
            serPortProp = new SerialPortProperties();
            serPortProp.SetAllPropertiesToDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
            serPortProp.VerifyPropertiesAndPrint(com1);
        }
    }
Exemple #9
0
        private void VerifyExceptionAfterOpen(SerialPort com, string portName, Type expectedException)
        {
            SerialPortProperties serPortProp = new SerialPortProperties();

            com.Open();
            serPortProp.SetAllPropertiesToOpenDefaults();
            serPortProp.SetProperty("PortName", com.PortName);

            try
            {
                com.PortName = portName;
                if (null != expectedException)
                {
                    Fail("ERROR!!! Expected setting the PortName after Open() to throw {0} and nothing was thrown", expectedException);
                }
            }
            catch (Exception e)
            {
                if (null == expectedException)
                {
                    Fail("ERROR!!! Expected setting the PortName after Open() NOT to throw an exception and {0} was thrown", e.GetType());
                }
                else if (e.GetType() != expectedException)
                {
                    Fail("ERROR!!! Expected setting the PortName after Open() throw {0} and {1} was thrown", expectedException, e.GetType());
                }
            }

            serPortProp.VerifyPropertiesAndPrint(com);
        }
Exemple #10
0
        public void BreakState_true_false()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                SerialPortProperties serPortProp = new SerialPortProperties();

                Debug.WriteLine("Verifying setting BreakState to true then false");

                serPortProp.SetAllPropertiesToOpenDefaults();
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

                com1.Open();
                serPortProp.VerifyPropertiesAndPrint(com1);

                SetBreakStateandVerify(com1);
                serPortProp.SetProperty("BreakState", true);
                serPortProp.VerifyPropertiesAndPrint(com1);

                serPortProp.SetProperty("BreakState", false);
                com1.BreakState = false;
                serPortProp.VerifyPropertiesAndPrint(com1);
            }

            Assert.False(GetCurrentBreakState());
        }
Exemple #11
0
    public bool DsrHolding_Default_AfterOpen()
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

        Console.WriteLine("Verifying default DsrHolding after Open");

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        com1.Open();
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        if (!retValue)
        {
            Console.WriteLine("Err_002!!! Verifying default DsrHolding after Open FAILED");
        }

        if (com1.IsOpen)
        {
            com1.Close();
        }

        return(retValue);
    }
Exemple #12
0
        private void  VerifyExceptionAfterOpen(SerialPort com, int dataBits, Type expectedException)
        {
            SerialPortProperties serPortProp = new SerialPortProperties();

            com.Open();
            serPortProp.SetAllPropertiesToOpenDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

            try
            {
                com.DataBits = dataBits;
                if (null != expectedException)
                {
                    Fail("ERROR!!! Expected setting the DataBits after Open() to throw {0} and nothing was thrown", expectedException);
                }
            }
            catch (Exception e)
            {
                if (null == expectedException)
                {
                    Fail("ERROR!!! Expected setting the DataBits after Open() NOT to throw an exception and {0} was thrown", e.GetType());
                }
                else if (e.GetType() != expectedException)
                {
                    Fail("ERROR!!! Expected setting the DataBits after Open() throw {0} and {1} was thrown", expectedException, e.GetType());
                }
            }
            serPortProp.VerifyPropertiesAndPrint(com);
        }
Exemple #13
0
        private void VerifyExceptionAfterOpen(SerialPort com, int baudRate, Type expectedException)
        {
            SerialPortProperties serPortProp = new SerialPortProperties();

            com.Open();
            serPortProp.SetAllPropertiesToOpenDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

            try
            {
                com.BaudRate = baudRate;
                if (null != expectedException)
                {
                    Assert.True(false, $"ERROR!!! Expected setting the BaudRate after Open() to throw {expectedException} and nothing was thrown");
                }
                else
                {
                    serPortProp.SetProperty("BaudRate", baudRate);
                }
            }
            catch (Exception e)
            {
                if (null == expectedException)
                {
                    Assert.True(false, $"ERROR!!! Expected setting the BaudRate after Open() NOT to throw an exception and {e.GetType()} was thrown");
                }
                else if (e.GetType() != expectedException)
                {
                    Assert.True(false, $"ERROR!!! Expected setting the BaudRate after Open() throw {expectedException} and {e.GetType()} was thrown");
                }
            }

            serPortProp.VerifyPropertiesAndPrint(com);
        }
Exemple #14
0
    public bool Handshake_Default()
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

        Console.WriteLine("Verifying default Handshake");

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        com1.Open();

        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);
        retValue &= VerifyHandshake(com1);

        if (!retValue)
        {
            Console.WriteLine("Err_001!!! Verifying default Handshake FAILED");
        }

        com1.DiscardInBuffer();
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        if (com1.IsOpen)
        {
            com1.Close();
        }

        return(retValue);
    }
Exemple #15
0
        public void ParityReplace_After_Parity()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    SerialPortProperties serPortProp = new SerialPortProperties();

                    Debug.WriteLine("Verifying setting ParityReplace after Parity has been set");

                    serPortProp.SetAllPropertiesToOpenDefaults();
                    serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

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

                    com1.Parity        = Parity.Even;
                    com1.ParityReplace = 1;

                    serPortProp.SetProperty("Parity", Parity.Even);
                    serPortProp.SetProperty("ParityReplace", (byte)1);

                    serPortProp.VerifyPropertiesAndPrint(com1);
                    VerifyParityReplaceByte(com1, com2, Read_byte_int_int, false);
                }
        }
Exemple #16
0
        private void VerifyStopBitsAfterOpen(int stopBits, int dataBits)
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                SerialPortProperties serPortProp = new SerialPortProperties();

                serPortProp.SetAllPropertiesToOpenDefaults();
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

                com1.Open();
                com1.DataBits = dataBits;
                com1.StopBits = (StopBits)stopBits;

                serPortProp.SetProperty("DataBits", dataBits);
                serPortProp.SetProperty("StopBits", (StopBits)stopBits);

                serPortProp.VerifyPropertiesAndPrint(com1);
                VerifyStopBits(com1);
                serPortProp.VerifyPropertiesAndPrint(com1);

                if (com1.IsOpen)
                {
                    com1.Close();
                }
            }
        }
Exemple #17
0
    public bool VerifyZeroTimeout(SerialPort com, ReadMethodDelegate readMethod)
    {
        SerialPortProperties serPortProp = new SerialPortProperties();

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        bool retValue   = true;
        int  actualTime = 0;

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

        serPortProp.SetProperty("ReadTimeout", 0);
        serPortProp.SetProperty("WriteTimeout", 1000);

        com.WriteTimeout = 1000;

        System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;

        sw.Start();
        readMethod(com);
        sw.Stop();

        if (MAX_ACCEPTABLE_WARMUP_ZERO_TIMEOUT < sw.ElapsedMilliseconds)
        {
            Console.WriteLine("Err_2570ajdlkj!!! Read Method {0} timed out in {1}ms expected something less then {2}ms", readMethod.Method.Name, sw.ElapsedMilliseconds, MAX_ACCEPTABLE_WARMUP_ZERO_TIMEOUT);
            retValue = false;
        }

        sw.Reset();

        for (int i = 0; i < NUM_TRYS; i++)
        {
            sw.Start();
            readMethod(com);
            sw.Stop();

            actualTime += (int)sw.ElapsedMilliseconds;
            sw.Reset();
        }

        System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
        actualTime /= NUM_TRYS;

        if (MAX_ACCEPTABLE_ZERO_TIMEOUT < actualTime)
        {
            Console.WriteLine("ERROR!!! Read Method {0} timed out in {1}ms expected something less then {2}ms", readMethod.Method.Name, actualTime, MAX_ACCEPTABLE_ZERO_TIMEOUT);
            retValue = false;
        }

        retValue       &= serPortProp.VerifyPropertiesAndPrint(com);
        com.ReadTimeout = 0;

        if (com.IsOpen)
        {
            com.Close();
        }

        return(retValue);
    }
Exemple #18
0
    public bool VerifyInfiniteTimeout(WriteMethodDelegate readMethod, bool setInfiniteTimeout)
    {
        SerialPort          com1       = TCSupport.InitFirstSerialPort();
        SerialPort          com2       = null;
        WriteDelegateThread readThread = new WriteDelegateThread(com1, readMethod);

        System.Threading.Thread t           = new System.Threading.Thread(new System.Threading.ThreadStart(readThread.CallWrite));
        SerialPortProperties    serPortProp = new SerialPortProperties();
        bool retValue = true;

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

        com1.Handshake = Handshake.RequestToSend;

        serPortProp.SetProperty("ReadTimeout", 10);
        com1.ReadTimeout = 10;

        com1.Open();



        if (setInfiniteTimeout)
        {
            com1.WriteTimeout = 500;
            com1.WriteTimeout = SerialPort.InfiniteTimeout;
        }

        t.Start();
        System.Threading.Thread.Sleep(DEFAULT_WAIT_INFINITE_TIMEOUT);

        if (!t.IsAlive)
        {
            Console.WriteLine("ERROR!!! {0} terminated with infinite timeout", readMethod.Method.Name);
            retValue = false;
        }



        com1.Handshake = Handshake.None;

        while (t.IsAlive)
        {
            System.Threading.Thread.Sleep(10);
        }


        com1.DiscardOutBuffer();
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        if (com1.IsOpen)
        {
            com1.Close();
        }



        return(retValue);
    }
Exemple #19
0
    public bool VerifyInfiniteTimeout(ReadMethodDelegate readMethod, bool setInfiniteTimeout)
    {
        SerialPort         com1       = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort         com2       = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        ReadDelegateThread readThread = new ReadDelegateThread(com1, readMethod);

        System.Threading.Thread t           = new System.Threading.Thread(new System.Threading.ThreadStart(readThread.CallRead));
        SerialPortProperties    serPortProp = new SerialPortProperties();
        bool retValue = true;

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        serPortProp.SetProperty("WriteTimeout", 10);

        com1.WriteTimeout = 10;
        com1.Open();

        if (!com2.IsOpen)
        {
            com2.Open();
        }

        if (setInfiniteTimeout)
        {
            com1.ReadTimeout = 500;
            com1.ReadTimeout = SerialPort.InfiniteTimeout;
        }

        t.Start();
        System.Threading.Thread.Sleep(DEFAULT_WAIT_INFINITE_TIMEOUT);

        if (!t.IsAlive)
        {
            Console.WriteLine("ERROR!!! {0} terminated with infinite timeout", readMethod.Method.Name);
            retValue = false;
        }

        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        com2.WriteLine(String.Empty);

        while (t.IsAlive)
        {
            System.Threading.Thread.Sleep(10);
        }

        if (com1.IsOpen)
        {
            com1.Close();
        }

        if (com2.IsOpen)
        {
            com2.Close();
        }

        return(retValue);
    }
Exemple #20
0
    public void OpenFillBuffersClose()
    {
        using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
            {
                SerialPortProperties serPortProp = new SerialPortProperties();

                Debug.WriteLine("Calling Open(), fill both transmit and receive buffers, call Close()");
                serPortProp.SetAllPropertiesToOpenDefaults();
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

                // Setting com1 to use Handshake so we can fill read buffer
                com1.Handshake = Handshake.RequestToSend;
                com1.Open();
                com2.Open();

                // BeginWrite is used so we can fill the read buffer then go onto to verify
                com1.BaseStream.BeginWrite(new byte[numWriteBytes], 0, numWriteBytes, null, null);
                com2.Write(new byte[numReadBytes], 0, numReadBytes);
                Thread.Sleep(500);

                serPortProp.SetProperty("Handshake", Handshake.RequestToSend);
                serPortProp.SetProperty("BytesToWrite", numWriteBytes - TCSupport.HardwareTransmitBufferSize);
                serPortProp.SetProperty("BytesToRead", numReadBytes);

                Debug.WriteLine("Verifying properties after port is open and buffers have been filled");
                serPortProp.VerifyPropertiesAndPrint(com1);

                com1.Handshake = Handshake.None;
                com1.Close();

                serPortProp.SetAllPropertiesToDefaults();
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

                Debug.WriteLine("Verifying properties after port has been closed");
                serPortProp.VerifyPropertiesAndPrint(com1);

                com1.Open();
                serPortProp.SetAllPropertiesToOpenDefaults();
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

                Debug.WriteLine("Verifying properties after port has been opened again");
                serPortProp.VerifyPropertiesAndPrint(com1);
            }
    }
Exemple #21
0
    private bool VerifyExceptionAtOpen(SerialPort com, int baudRate, ThrowAt throwAt, System.Type expectedException)
    {
        int  origBaudRate = com.BaudRate;
        bool retValue     = true;
        SerialPortProperties serPortProp = new SerialPortProperties();

        if (null == expectedException && throwAt == ThrowAt.Open)
        {
            serPortProp.SetAllPropertiesToOpenDefaults();
            serPortProp.SetProperty("BaudRate", baudRate);
        }
        else
        {
            serPortProp.SetAllPropertiesToDefaults();
        }

        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

        if (ThrowAt.Open == throwAt)
        {
            serPortProp.SetProperty("BaudRate", baudRate);
        }

        try
        {
            com.BaudRate = baudRate;
            if (ThrowAt.Open == throwAt)
            {
                com.Open();
            }

            if (null != expectedException)
            {
                Console.WriteLine("ERROR!!! Expected Open() to throw {0} and nothing was thrown", expectedException);
                retValue = false;
            }
        }
        catch (System.Exception e)
        {
            if (null == expectedException)
            {
                Console.WriteLine("ERROR!!! Expected Open() NOT to throw an exception and {0} was thrown", e.GetType());
                retValue = false;
            }
            else if (e.GetType() != expectedException)
            {
                Console.WriteLine("ERROR!!! Expected Open() throw {0} and {1} was thrown", expectedException, e.GetType());
                retValue = false;
            }
        }

        retValue &= serPortProp.VerifyPropertiesAndPrint(com);

        com.BaudRate = origBaudRate;
        return(retValue);
    }
    public bool ReceivedBytesThreshold_Twice()
    {
        SerialPort           com1            = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort           com2            = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1);
        SerialPortProperties serPortProp     = new SerialPortProperties();
        bool   retValue = true;
        Random rndGen   = new Random(-55);
        int    receivedBytesThreshold = rndGen.Next(MIN_RND_THRESHOLD, MAX_RND_THRESHOLD);

        com1.ReceivedBytesThreshold = receivedBytesThreshold;
        com1.Open();
        com2.Open();
        com1.DataReceived += new SerialDataReceivedEventHandler(rcvEventHandler.HandleEvent);

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("ReceivedBytesThreshold", receivedBytesThreshold);
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

        Console.WriteLine("Verifying writing twice the number of bytes of ReceivedBytesThreshold and ReceivedEvent firered twice");

        com2.Write(new byte[com1.ReceivedBytesThreshold * 2], 0, com1.ReceivedBytesThreshold * 2);

        if (!rcvEventHandler.WaitForEvent(SerialData.Chars, 2, MAX_TIME_WAIT))
        {
            Console.WriteLine("ERROR!!!: Event never fired");
            retValue = false;
        }

        com1.DiscardInBuffer();

        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);
        retValue &= rcvEventHandler.Validate(SerialData.Chars, com1.ReceivedBytesThreshold, 0);
        retValue &= rcvEventHandler.Validate(SerialData.Chars, 2 * com1.ReceivedBytesThreshold, 1);

        if (!retValue)
        {
            Console.WriteLine("Err_007!!! Verifying writing twice the number of bytes of ReceivedBytesThreshold and ReceivedEvent firered twice FAILED");
        }

        if (com1.IsOpen)
        {
            com1.Close();
        }

        if (com2.IsOpen)
        {
            com2.Close();
        }

        return(retValue);
    }
Exemple #23
0
        private void VerifyExceptionAtOpen(SerialPort com, int baudRate, ThrowAt throwAt, Type expectedException)
        {
            int origBaudRate = com.BaudRate;
            SerialPortProperties serPortProp = new SerialPortProperties();

            if (null == expectedException && throwAt == ThrowAt.Open)
            {
                serPortProp.SetAllPropertiesToOpenDefaults();
                serPortProp.SetProperty("BaudRate", baudRate);
            }
            else
            {
                serPortProp.SetAllPropertiesToDefaults();
            }

            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

            if (ThrowAt.Open == throwAt)
            {
                serPortProp.SetProperty("BaudRate", baudRate);
            }

            try
            {
                com.BaudRate = baudRate;

                if (ThrowAt.Open == throwAt)
                {
                    com.Open();
                }

                if (null != expectedException)
                {
                    Assert.True(false, $"ERROR!!! Expected Open() to throw {expectedException} and nothing was thrown");
                }
            }
            catch (Exception e)
            {
                if (null == expectedException)
                {
                    Assert.True(false, $"ERROR!!! Expected Open() NOT to throw an exception and {e.GetType()} was thrown");
                }
                else if (e.GetType() != expectedException)
                {
                    Assert.True(false, $"ERROR!!! Expected Open() throw {expectedException} and {e.GetType()} was thrown: {e}");
                }
            }

            serPortProp.VerifyPropertiesAndPrint(com);

            com.BaudRate = origBaudRate;
        }
Exemple #24
0
    private bool VerifyBytesToWrite(WriteMethodDelegate writeMethod, int bufferSize, bool sendNewLine)
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort           com2        = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool         retValue            = true;
        IAsyncResult writeMethodResult;
        int          expectedlBytesToWrite;
        int          acutualBytesToWrite;

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        serPortProp.SetProperty("Handshake", Handshake.RequestToSend);
        serPortProp.SetProperty("WriteTimeout", 500);

        com1.Open();
        com1.WriteTimeout = 500;
        com1.Handshake    = Handshake.RequestToSend;

        writeMethodResult = writeMethod.BeginInvoke(com1, bufferSize, null, null);
        System.Threading.Thread.Sleep(200);

        acutualBytesToWrite = com1.BytesToWrite;
        writeMethodResult.AsyncWaitHandle.WaitOne();
        expectedlBytesToWrite = (int)writeMethod.EndInvoke(writeMethodResult);

        com2.Open();
        com2.RtsEnable = true;

        if (acutualBytesToWrite != expectedlBytesToWrite)
        {
            Console.WriteLine("ERRROR!!! Expected BytesToWrite={0} acutual BytesToWrite={1}", expectedlBytesToWrite, acutualBytesToWrite);
            retValue = true;
        }

        com2.RtsEnable = false;
        retValue      &= serPortProp.VerifyPropertiesAndPrint(com1);

        if (com1.IsOpen)
        {
            com1.Close();
        }

        if (com2.IsOpen)
        {
            com2.Close();
        }

        return(retValue);
    }
Exemple #25
0
        public void ParityReplace_Default_AfterOpen()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                SerialPortProperties serPortProp = new SerialPortProperties();

                Debug.WriteLine("Verifying default ParityReplace after Open");

                serPortProp.SetAllPropertiesToOpenDefaults();
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
                com1.Open();
                serPortProp.VerifyPropertiesAndPrint(com1);
            }
        }
Exemple #26
0
    private void VerifyRtsEnableWithHandshake(bool rtsEnable, Handshake handshake)
    {
        using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
        {
            SerialPortProperties serPortProp = new SerialPortProperties();
            Handshake            originalHandshake;
            bool expetectedRtsEnable;

            serPortProp.SetAllPropertiesToOpenDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

            com1.RtsEnable = rtsEnable;
            com1.Open();
            originalHandshake = com1.Handshake;
            serPortProp.SetProperty("RtsEnable", rtsEnable);

            serPortProp.VerifyPropertiesAndPrint(com1);

            VerifyRtsEnable(com1, rtsEnable);

            com1.Handshake = handshake;

            if (IsRequestToSend(com1))
            {
                try
                {
                    com1.RtsEnable = !rtsEnable;
                }
                catch (InvalidOperationException) { }
            }
            else
            {
                com1.RtsEnable = !rtsEnable;
                com1.RtsEnable = rtsEnable;
            }

            expetectedRtsEnable = handshake == Handshake.RequestToSend || handshake == Handshake.RequestToSendXOnXOff ?
                                  true : rtsEnable;

            VerifyRtsEnable(com1, expetectedRtsEnable);

            com1.Handshake = originalHandshake;

            expetectedRtsEnable = rtsEnable;

            VerifyRtsEnable(com1, expetectedRtsEnable);

            serPortProp.VerifyPropertiesAndPrint(com1);
        }
    }
Exemple #27
0
        private void VerifyZeroTimeout(SerialPort com, Action <SerialPort> readMethod)
        {
            SerialPortProperties serPortProp = new SerialPortProperties();
            Stopwatch            sw          = new Stopwatch();

            int actualTime = 0;

            serPortProp.SetAllPropertiesToOpenDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

            serPortProp.SetProperty("ReadTimeout", 0);
            serPortProp.SetProperty("WriteTimeout", 1000);

            com.WriteTimeout = 1000;

            System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;

            sw.Start();
            readMethod(com);
            sw.Stop();

            if (MAX_ACCEPTABLE_WARMUP_ZERO_TIMEOUT < sw.ElapsedMilliseconds)
            {
                Fail("Err_2570ajdlkj!!! Read Method {0} timed out in {1}ms expected something less then {2}ms", readMethod.Method.Name, sw.ElapsedMilliseconds, MAX_ACCEPTABLE_WARMUP_ZERO_TIMEOUT);
            }

            sw.Reset();

            for (int i = 0; i < NUM_TRYS; i++)
            {
                sw.Start();
                readMethod(com);
                sw.Stop();

                actualTime += (int)sw.ElapsedMilliseconds;
                sw.Reset();
            }

            System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
            actualTime /= NUM_TRYS;

            if (MAX_ACCEPTABLE_ZERO_TIMEOUT < actualTime)
            {
                Fail("ERROR!!! Read Method {0} timed out in {1}ms expected something less then {2}ms", readMethod.Method.Name, actualTime, MAX_ACCEPTABLE_ZERO_TIMEOUT);
            }

            serPortProp.VerifyPropertiesAndPrint(com);
            com.ReadTimeout = 0;
        }
Exemple #28
0
        public void OpenDefault()
        {
            using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                SerialPortProperties serPortProp = new SerialPortProperties();

                com.Open();
                serPortProp.SetAllPropertiesToOpenDefaults();
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

                Debug.WriteLine("BytesToWrite={0}", com.BytesToWrite);

                serPortProp.VerifyPropertiesAndPrint(com);
            }
        }
Exemple #29
0
    public void IsOpen_Open()
    {
        using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
        {
            SerialPortProperties serPortProp = new SerialPortProperties();

            Debug.WriteLine("Verifying IsOpen after Open() has been called");

            serPortProp.SetAllPropertiesToOpenDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
            com1.Open();

            serPortProp.VerifyPropertiesAndPrint(com1);
        }
    }
Exemple #30
0
    public bool DsrHolding_true_remote_close()
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort           com2        = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

        Console.WriteLine("Verifying DsrHolding=true then false on com1 when com2.DtrEnable=true then com2 is closed");

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        serPortProp.SetProperty("DsrHolding", true);

        com1.Open();
        com2.Open();
        com2.DtrEnable = true;

        serPortProp.SetProperty("CDHolding", com1.CDHolding);    //We dont care what this is set to since some serial cables loop CD to CTS
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        if (com2.IsOpen)
        {
            com2.Close();
        }

        System.Threading.Thread.Sleep(100);

        serPortProp.SetProperty("DsrHolding", false);
        serPortProp.SetProperty("CDHolding", com1.CDHolding);    //We dont care what this is set to since some serial cables loop CD to CTS
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        if (!retValue)
        {
            Console.WriteLine("Err_006!!! Verifying DsrHolding=true then false on com1 when com2.DtrEnable=true then com2 is closed FAILED");
        }

        if (com1.IsOpen)
        {
            com1.Close();
        }

        if (com2.IsOpen)
        {
            com2.Close();
        }

        return(retValue);
    }