Exemple #1
0
        private void VerifyExceptionAtOpen(SerialPort com, int dataBits, ThrowAt throwAt, Type expectedException)
        {
            int origDataBits = com.DataBits;
            SerialPortProperties serPortProp = new SerialPortProperties();

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

            if (ThrowAt.Open == throwAt)
            {
                serPortProp.SetProperty("DataBits", dataBits);
            }

            try
            {
                com.DataBits = dataBits;

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

                if (null != expectedException)
                {
                    Fail("ERROR!!! Expected Open() to throw {0} and nothing was thrown", expectedException);
                }
            }
            catch (Exception e)
            {
                if (null == expectedException)
                {
                    Fail("ERROR!!! Expected Open() NOT to throw an exception and {0} was thrown", e.GetType());
                }
                else if (e.GetType() != expectedException)
                {
                    Fail("ERROR!!! Expected Open() throw {0} and {1} was thrown", expectedException, e.GetType());
                }
            }
            serPortProp.VerifyPropertiesAndPrint(com);
            com.DataBits = origDataBits;
        }
Exemple #2
0
    private void VerifyCtor(string portName, Type expectedException, ThrowAt throwAt)
    {
        SerialPortProperties serPortProp = new SerialPortProperties();

        Debug.WriteLine($"Verifying properties where PortName={portName}");
        try
        {
            using (SerialPort com = new SerialPort(portName))
            {
                if (null != expectedException && throwAt == ThrowAt.Set)
                {
                    Assert.True(false, $"Err_7212ahsdj Expected Ctor to throw {expectedException}");
                }

                serPortProp.SetAllPropertiesToDefaults();
                serPortProp.SetProperty("PortName", portName);

                serPortProp.VerifyPropertiesAndPrint(com);
            }
        }
        catch (Xunit.Sdk.TrueException)
        {
            // This is an inner failure
            throw;
        }
        catch (Exception e)
        {
            if (null == expectedException)
            {
                Assert.True(false, $"Err_07081hadnh Did not expect exception to be thrown and the following was thrown: \n{e}");
            }
            else if (throwAt == ThrowAt.Open)
            {
                Assert.True(false, $"Err_88916adfa Expected {expectedException} to be thrown at Open and the following was thrown at Set: \n{e}");
            }
            else if (e.GetType() != expectedException)
            {
                Assert.True(false, $"Err_90282ahwhp Expected {expectedException} to be thrown and the following was thrown: \n{e}");
            }
        }
    }
        public void DiscardNull_false_flase_Default_ReadByte()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                SerialPortProperties serPortProp = new SerialPortProperties();

                Debug.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);

                serPortProp.VerifyPropertiesAndPrint(com1);
                VerifyDiscardNull(com1, ReadByte, false);
                serPortProp.VerifyPropertiesAndPrint(com1);
            }
        }
Exemple #4
0
    public void BreakState_false()
    {
        using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
        {
            SerialPortProperties serPortProp = new SerialPortProperties();

            Debug.WriteLine("Verifying false BreakState");

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

            SetBreakStateandVerify(com1);
            serPortProp.SetProperty("BreakState", false);

            com1.BreakState = false;
            serPortProp.VerifyPropertiesAndPrint(com1);

            Assert.False(GetCurrentBreakState());
        }
    }
Exemple #5
0
    public bool openDefault()
    {
        SerialPort           com         = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue;

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

        Console.WriteLine("Verifying after calling Open()");
        Console.WriteLine("BytesToWrite={0}", com.BytesToWrite);
        retValue = serPortProp.VerifyPropertiesAndPrint(com);

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

        return(retValue);
    }
Exemple #6
0
        private void VerifyStopBitsBeforeOpen(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.DataBits = dataBits;
                com1.StopBits = (StopBits)stopBits;
                com1.Open();

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

                serPortProp.VerifyPropertiesAndPrint(com1);
                VerifyStopBits(com1);
                serPortProp.VerifyPropertiesAndPrint(com1);
            }
        }
Exemple #7
0
        public void Parity_Odd_Mark()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                SerialPortProperties serPortProp = new SerialPortProperties();

                Debug.WriteLine("Verifying Parity Odd and then Mark");

                serPortProp.SetAllPropertiesToOpenDefaults();
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
                com1.Open();
                com1.Parity = Parity.Odd;
                com1.Parity = Parity.Mark;
                serPortProp.SetProperty("Parity", Parity.Mark);

                serPortProp.VerifyPropertiesAndPrint(com1);
                VerifyParity(com1, DEFAULT_BYTE_SIZE);

                serPortProp.VerifyPropertiesAndPrint(com1);
            }
        }
Exemple #8
0
        public void VerifyInfiniteTimeout(Action <SerialPort> readMethod, bool setInfiniteTimeout)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    SerialPortProperties serPortProp = new SerialPortProperties();

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

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

                    serPortProp.VerifyPropertiesAndPrint(com1);

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

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

                    Task task = Task.Run(() => readMethod(com1));

                    System.Threading.Thread.Sleep(DEFAULT_WAIT_INFINITE_TIMEOUT);

                    Assert.True(!task.IsCompleted);

                    serPortProp.VerifyPropertiesAndPrint(com1);

                    com2.WriteLine(string.Empty);

                    TCSupport.WaitForTaskCompletion(task);
                }
        }
Exemple #9
0
    public bool ReadBufferSize_Default()
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

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

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

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

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

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

        com1.Close();
        serPortProp = new SerialPortProperties();
        serPortProp.SetAllPropertiesToDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

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

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

        return(retValue);
    }
Exemple #10
0
        private void VerifyCtor(string portName, int baudRate, int parity, int dataBits, Type expectedException, ThrowAt throwAt)
        {
            SerialPortProperties serPortProp = new SerialPortProperties();

            Debug.WriteLine("Verifying properties where PortName={0},BaudRate={1},Parity={2},DatBits={3}", portName, baudRate, parity, dataBits);
            try
            {
                using (SerialPort com = new SerialPort(portName, baudRate, (Parity)parity, dataBits))
                {
                    if (null != expectedException && throwAt == ThrowAt.Set)
                    {
                        Fail("Err_7212ahsdj Expected Ctor to throw {0}", expectedException);
                    }

                    serPortProp.SetAllPropertiesToDefaults();

                    serPortProp.SetProperty("PortName", portName);
                    serPortProp.SetProperty("BaudRate", baudRate);
                    serPortProp.SetProperty("Parity", (Parity)parity);
                    serPortProp.SetProperty("DataBits", dataBits);

                    serPortProp.VerifyPropertiesAndPrint(com);
                }
            }
            catch (Exception e)
            {
                if (null == expectedException)
                {
                    Fail("Err_07081hadnh Did not expect exception to be thrown and the following was thrown: \n{0}", e);
                }
                else if (throwAt == ThrowAt.Open)
                {
                    Fail("Err_88916adfa Expected {0} to be thrown at Open and the following was thrown at Set: \n{1}", expectedException, e);
                }
                else if (e.GetType() != expectedException)
                {
                    Fail("Err_90282ahwhp Expected {0} to be thrown and the following was thrown: \n{1}", expectedException, e);
                }
            }
        }
Exemple #11
0
    public bool CtsHolding_true_false()
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort           com2        = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

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

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

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

        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        com2.RtsEnable = false;
        serPortProp.SetProperty("CtsHolding", false);
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        if (!retValue)
        {
            Console.WriteLine("Err_004!!! Verifying CtsHolding=true then false on com1 when com2.RtsEnable=true then false FAILED");
        }

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

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

        return(retValue);
    }
    private bool VerifyExceptionAtOpen(SerialPort com, int receivedBytesThreshold, System.Type expectedException)
    {
        int  origReceivedBytesThreshold = com.ReceivedBytesThreshold;
        bool retValue = true;
        SerialPortProperties serPortProp = new SerialPortProperties();

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

        try
        {
            com.ReceivedBytesThreshold = receivedBytesThreshold;

            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.ReceivedBytesThreshold = origReceivedBytesThreshold;

        return(retValue);
    }
Exemple #13
0
    public bool OpenClose_Properties()
    {
        SerialPort           com         = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        bool                 retValue    = true;
        SerialPortProperties serPortProp = new SerialPortProperties();

        Console.WriteLine("Verifying Properites after calling Open() and BaseStream.Close()");

        com.Open();
        com.BaseStream.Close();

        serPortProp.SetAllPropertiesToDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        retValue &= serPortProp.VerifyPropertiesAndPrint(com);

        if (!retValue)
        {
            Console.WriteLine("Err_001!!! erifying Properites after calling Open() and BaseStream.Close() FAILED");
        }

        return(retValue);
    }
Exemple #14
0
    public void DataBits_8_StopBitsOnePointFive()
    {
        using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
        {
            SerialPortProperties serPortProp = new SerialPortProperties();

            Debug.WriteLine("Verifying setting DataBits=8 from 5 with StopBits=1.5");

            com.DataBits = 5;
            com.StopBits = StopBits.OnePointFive;
            com.Open();

            serPortProp.SetAllPropertiesToOpenDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
            serPortProp.SetProperty("StopBits", StopBits.OnePointFive);
            serPortProp.SetProperty("DataBits", 5);

            Assert.Throws <IOException>(() => com.DataBits = 8);

            serPortProp.VerifyPropertiesAndPrint(com);
        }
    }
Exemple #15
0
    private bool VerifyExceptionAfterOpen(SerialPort com, int baudRate, System.Type expectedException)
    {
        bool retValue = true;
        SerialPortProperties serPortProp = new SerialPortProperties();

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

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

        retValue &= serPortProp.VerifyPropertiesAndPrint(com);
        return(retValue);
    }
Exemple #16
0
    public bool CDHolding_Default()
    {
        SerialPort           com1        = new SerialPort();
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

        Console.WriteLine("Verifying default CDHolding before Open");
        serPortProp.SetAllPropertiesToDefaults();
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

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

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

        return(retValue);
    }
Exemple #17
0
        public void OpenTwoInstances()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                {
                    SerialPortProperties serPortProp1 = new SerialPortProperties();
                    SerialPortProperties serPortProp2 = new SerialPortProperties();

                    Debug.WriteLine("Verifying calling Open() on two instances of SerialPort");
                    serPortProp1.SetAllPropertiesToOpenDefaults();
                    serPortProp1.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

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

                    com1.Open();
                    Assert.Throws <UnauthorizedAccessException>(() => com2.Open());

                    serPortProp1.VerifyPropertiesAndPrint(com1);
                    serPortProp2.VerifyPropertiesAndPrint(com2);
                }
        }
Exemple #18
0
        public void DsrHolding_true()
        {
            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 on com1 when com2.DtrEnable=true");

                    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);
                }
        }
Exemple #19
0
    public bool DataBits_8_StopBitsOnePointFive()
    {
        SerialPort           com         = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        bool                 retValue    = true;
        SerialPortProperties serPortProp = new SerialPortProperties();

        Console.WriteLine("Verifying setting DataBits=8 from 5 with StopBits=1.5");

        com.DataBits = 5;
        com.StopBits = StopBits.OnePointFive;
        com.Open();

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        serPortProp.SetProperty("StopBits", StopBits.OnePointFive);
        serPortProp.SetProperty("DataBits", 5);

        try
        {
            com.DataBits = 8;
            Console.WriteLine("ERROR!!! Setting DataBits did not thow an exception");
            retValue = false;
        }
        catch (System.IO.IOException) { }
        catch (System.Exception e)
        {
            Console.WriteLine("ERROR!!! Expected IOException and {0} was thrown", e.GetType());
            retValue = false;
        }

        retValue &= serPortProp.VerifyPropertiesAndPrint(com);

        if (!retValue)
        {
            Console.WriteLine("Err_018!!! Verifying setting DataBits=8 from 5 with StopBits=1.5 FAILED");
        }

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

                    Debug.WriteLine("Verifying BytesToRead after receiving a random number of bytes");
                    serPortProp.SetAllPropertiesToOpenDefaults();
                    serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

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

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

                    serPortProp.SetProperty("BytesToRead", DEFAULT_NUM_RND_BYTES);
                    Thread.Sleep(100); //Wait for com1 to get all of the bytes

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

            Debug.WriteLine("Verifying seting RtsEnable to true then false before open");

            com1.RtsEnable = true;

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

            com1.Open();

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

        Console.WriteLine("Verifying BytesToRead after receiving a random number of bytes");
        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

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

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

        serPortProp.SetProperty("BytesToRead", DEFUALT_NUM_RND_BYTES);
        System.Threading.Thread.Sleep(100);//Wait for com1 to get all of the bytes

        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        if (!retValue)
        {
            Console.WriteLine("Err_002!!! Verifying BytesToRead after receiving a random number of bytes FAILED");
        }

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

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

        return(retValue);
    }
Exemple #23
0
        public void OpenCloseNewInstanceOpen()
        {
            using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                SerialPortProperties serPortProp = new SerialPortProperties();

                Debug.WriteLine(
                    "Calling Close() after calling Open() then create a new instance of SerialPort and call Open() again");
                com.Open();
                com.BaseStream.Close();
                using (var newCom = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                {
                    try
                    {
                        newCom.Open();

                        serPortProp.SetAllPropertiesToOpenDefaults();
                        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
                        serPortProp.VerifyPropertiesAndPrint(newCom);
                    }
                    catch (Exception e)
                    {
                        Fail("ERROR!!! Open() threw {0}", e.GetType());
                    }

                    try
                    {
                        if (com.IsOpen)
                        {
                            com.Close();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
Exemple #24
0
    private bool VerifyBaudRateAfterOpen(int baudRate)
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

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

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

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

        return(retValue);
    }
Exemple #25
0
    public bool BaseStream_Default()
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

        Console.WriteLine("Verifying default BaseStream");
        serPortProp.SetAllPropertiesToDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

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

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

        return(retValue);
    }
        private void VerifyInfiniteTimeout(WriteMethodDelegate writeMethod, bool setInfiniteTimeout)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
            {
                SerialPortProperties serPortProp = new SerialPortProperties();

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

                Task task = Task.Run(() => writeMethod(com1));
                Thread.Sleep(DEFAULT_WAIT_INFINITE_TIMEOUT);

                Assert.False(task.IsCompleted, "Task should not have completed while tx is blocked by flow-control");

                com1.Handshake = Handshake.None;

                TCSupport.WaitForTaskCompletion(task);

                com1.DiscardOutBuffer();
                // If we're looped-back, then there will be data queued on the receive side which we need to discard
                com1.DiscardInBuffer();
                serPortProp.VerifyPropertiesAndPrint(com1);
            }
        }
Exemple #27
0
    public bool DsrHolding_true()
    {
        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 on com1 when com2.DtrEnable=true");

        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 (!retValue)
        {
            Console.WriteLine("Err_003!!! Verifying DsrHolding=true on com1 when com2.DtrEnable=true FAILED");
        }

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

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

        return(retValue);
    }
Exemple #28
0
    private bool VerifyRtsEnableBeforeOpen(bool rtsEnable)
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

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

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

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

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

        return(retValue);
    }
Exemple #29
0
        public void CtsHolding_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 CtsHolding=true then false on com1 when com2.RtsEnable=true then false");

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

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

                    serPortProp.VerifyPropertiesAndPrint(com1);

                    com2.RtsEnable = false;
                    serPortProp.SetProperty("CtsHolding", false);
                    serPortProp.VerifyPropertiesAndPrint(com1);
                }
        }
Exemple #30
0
    public bool BreakState_true_false()
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

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

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

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

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

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

        retValue &= !GetCurrentBreakState();

        if (!retValue)
        {
            Console.WriteLine("Err_004!!! Verifying setting BreakState to true then false FAILED");
        }

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

        return(retValue);
    }