Esempio n. 1
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}");
            }
        }
    }
Esempio n. 2
0
        private void VerifyExceptionAtOpen(SerialPort com, int stopBits, ThrowAt throwAt, Type expectedException)
        {
            int origStopBits = (int)com.StopBits;
            SerialPortProperties serPortProp = new SerialPortProperties();

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

            if (ThrowAt.Open == throwAt)
            {
                serPortProp.SetProperty("StopBits", (StopBits)stopBits);
            }

            try
            {
                com.StopBits = (StopBits)stopBits;
                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.StopBits = (StopBits)origStopBits;
        }
Esempio n. 3
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());
        }
    }
Esempio n. 4
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);
    }
Esempio n. 5
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);
                }
            }
        }
Esempio n. 6
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);
    }
Esempio n. 7
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);
                }
        }
Esempio n. 8
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);
        }
    }
Esempio n. 9
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);
    }
Esempio n. 10
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);
    }
Esempio n. 11
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);
                }
        }
Esempio n. 12
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);
                }
        }
Esempio n. 13
0
    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);
    }
Esempio n. 14
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);
    }
Esempio n. 15
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);
    }
Esempio n. 16
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)
                    {
                    }
                }
            }
        }
Esempio n. 17
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);
    }
Esempio n. 18
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);
    }
Esempio n. 19
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);
    }
        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);
            }
        }
Esempio n. 21
0
    public bool CtsHolding_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 CtsHolding=true on com1 when com2.RtsEnable=true");

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

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

        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

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

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

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

        return(retValue);
    }
Esempio n. 22
0
    public bool OpenFillBuffersClose()
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort           com2        = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool retValue = true;

        Console.WriteLine("Calling Open(), fill both trasmit 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);
        System.Threading.Thread.Sleep(500);

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

        Console.WriteLine("Verifying properties after port is open and bufferes have been filled");
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        com1.Handshake = Handshake.None;
        com1.BaseStream.Close();
        serPortProp.SetAllPropertiesToDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

        Console.WriteLine("Verifying properties after port has been closed");
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        try
        {
            com1.Open();
        }
        catch (System.Exception e)
        {
            Console.WriteLine("ERROR!!! Open() threw {0}", e.GetType());
            retValue = false;
        }

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

        Console.WriteLine("Verifying properties after port has been opened again");
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        try
        {
            if (com1.IsOpen)
            {
                com1.Close();
            }
        }
        catch (Exception) { }

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

        if (!retValue)
        {
            Console.WriteLine("Err_002!!! Verifying calling Open(), fill both trasmit and receive buffers, call Close() FAILED");
        }

        return(retValue);
    }
Esempio n. 23
0
    private bool VerifyReadBufferSizeBeforeOpen(int newReadBufferSize)
    {
        SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);

        byte[] xmitBytes = new byte[newReadBufferSize];
        byte[] rcvBytes;
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool   retValue       = true;
        Random rndGen         = new Random(-55);
        int    newBytesToRead = 0;

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

        if (newReadBufferSize < 4096)
        {
            newBytesToRead = Math.Min(4096, xmitBytes.Length + (xmitBytes.Length / 2));
        }
        else
        {
            newBytesToRead = Math.Min(newReadBufferSize, xmitBytes.Length);
        }

        rcvBytes = new byte[newBytesToRead];

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        com1.ReadBufferSize = newReadBufferSize;
        serPortProp.SetProperty("ReadBufferSize", newReadBufferSize);

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

        int origBaudRate = com1.BaudRate;

        com2.BaudRate = 115200;
        com1.BaudRate = 115200;

        for (int j = 0; j < 1; j++)
        {
            com2.Write(xmitBytes, 0, xmitBytes.Length);
            com2.Write(xmitBytes, xmitBytes.Length / 2, xmitBytes.Length / 2);

            while (newBytesToRead > com1.BytesToRead)
            {
                System.Threading.Thread.Sleep(50);
            }

            System.Threading.Thread.Sleep(250); //This is to wait for the bytes to be received after the buffer is full

            serPortProp.SetProperty("BytesToRead", newBytesToRead);
            serPortProp.SetProperty("BaudRate", 115200);

            Console.WriteLine("Verifying properties after bytes have been written");
            retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

            com1.Read(rcvBytes, 0, newBytesToRead);

            for (int i = 0; i < newReadBufferSize; i++)
            {
                if (rcvBytes[i] != xmitBytes[i])
                {
                    Console.WriteLine("ERROR!!!: Expected to read byte {0} actual={1} at {2}", xmitBytes[i], rcvBytes[i], i);
                    retValue = false;
                }
            }

            Console.WriteLine("Verifying properties after bytes have been read");
            serPortProp.SetProperty("BytesToRead", 0);
            retValue &= serPortProp.VerifyPropertiesAndPrint(com1);
        }

        com2.Write(xmitBytes, 0, xmitBytes.Length);
        com2.Write(xmitBytes, xmitBytes.Length / 2, xmitBytes.Length / 2);

        while (newBytesToRead > com1.BytesToRead)
        {
            System.Threading.Thread.Sleep(50);
        }

        serPortProp.SetProperty("BytesToRead", newBytesToRead);
        Console.WriteLine("Verifying properties after writing bytes");
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        com2.BaudRate = origBaudRate;
        com1.BaudRate = origBaudRate;

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

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

        return(retValue);
    }
Esempio n. 24
0
    private void VerifyReadBufferSize(SerialPort com1)
    {
        using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
        {
            int    readBufferSize            = com1.ReadBufferSize;
            byte[] xmitBytes                 = new byte[1024];
            SerialPortProperties serPortProp = new SerialPortProperties();
            Random rndGen          = new Random(-55);
            int    bytesToRead     = readBufferSize < 4096 ? 4096 : readBufferSize;
            int    origBaudRate    = com1.BaudRate;
            int    origReadTimeout = com1.ReadTimeout;
            int    bytesRead;

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

            //bytesToRead = Math.Min(4096, xmitBytes.Length + (xmitBytes.Length / 2));

            serPortProp.SetAllPropertiesToOpenDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
            serPortProp.SetProperty("ReadBufferSize", readBufferSize);
            serPortProp.SetProperty("BaudRate", 115200);

            com2.Open();

            com2.BaudRate = 115200;
            com1.BaudRate = 115200;

            for (int i = 0; i < bytesToRead / xmitBytes.Length; i++)
            {
                com2.Write(xmitBytes, 0, xmitBytes.Length);
            }

            com2.Write(xmitBytes, 0, xmitBytes.Length / 2);

            while (bytesToRead > com1.BytesToRead)
            {
                System.Threading.Thread.Sleep(50);
            }

            System.Threading.Thread.Sleep(250); //This is to wait for the bytes to be received after the buffer is full

            var rcvBytes = new byte[(int)(bytesToRead * 1.5)];
            if (bytesToRead != (bytesRead = com1.Read(rcvBytes, 0, rcvBytes.Length)))
            {
                Fail("Err_2971ahius Did not read all expected bytes({0}) bytesRead={1} ReadBufferSize={2}", bytesToRead, bytesRead, com1.ReadBufferSize);
            }

            for (int i = 0; i < bytesToRead; i++)
            {
                if (rcvBytes[i] != xmitBytes[i % xmitBytes.Length])
                {
                    Fail("Err_70929apba!!!: Expected to read byte {0} actual={1} at {2}", xmitBytes[i % xmitBytes.Length], rcvBytes[i], i);
                }
            }

            serPortProp.SetProperty("BytesToRead", 0);
            serPortProp.VerifyPropertiesAndPrint(com1);

            com1.ReadTimeout = 250;

            // "Err_1707ahspb!!!: After reading all bytes from buffer ReadByte() did not timeout");
            Assert.Throws <TimeoutException>(() => com1.ReadByte());

            com1.ReadTimeout = origReadTimeout;
        }
    }
Esempio n. 25
0
    private void VerifyReadBufferSizeBeforeOpen(int newReadBufferSize)
    {
        using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
            {
                byte[] xmitBytes = new byte[newReadBufferSize];
                byte[] rcvBytes;
                SerialPortProperties serPortProp = new SerialPortProperties();
                Random rndGen = new Random(-55);
                int    newBytesToRead;

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

                if (newReadBufferSize < 4096)
                {
                    newBytesToRead = Math.Min(4096, xmitBytes.Length + (xmitBytes.Length / 2));
                }
                else
                {
                    newBytesToRead = Math.Min(newReadBufferSize, xmitBytes.Length);
                }

                rcvBytes = new byte[newBytesToRead];

                serPortProp.SetAllPropertiesToOpenDefaults();
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
                com1.ReadBufferSize = newReadBufferSize;
                serPortProp.SetProperty("ReadBufferSize", newReadBufferSize);

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

                int origBaudRate = com1.BaudRate;

                com2.BaudRate = 115200;
                com1.BaudRate = 115200;

                for (int j = 0; j < 1; j++)
                {
                    com2.Write(xmitBytes, 0, xmitBytes.Length);
                    com2.Write(xmitBytes, xmitBytes.Length / 2, xmitBytes.Length / 2);

                    while (newBytesToRead > com1.BytesToRead)
                    {
                        System.Threading.Thread.Sleep(50);
                    }

                    System.Threading.Thread.Sleep(250);
                    //This is to wait for the bytes to be received after the buffer is full

                    serPortProp.SetProperty("BytesToRead", newBytesToRead);
                    serPortProp.SetProperty("BaudRate", 115200);

                    Debug.WriteLine("Verifying properties after bytes have been written");
                    serPortProp.VerifyPropertiesAndPrint(com1);

                    com1.Read(rcvBytes, 0, newBytesToRead);

                    Assert.Equal(xmitBytes, rcvBytes);

                    Debug.WriteLine("Verifying properties after bytes have been read");
                    serPortProp.SetProperty("BytesToRead", 0);
                    serPortProp.VerifyPropertiesAndPrint(com1);
                }

                com2.Write(xmitBytes, 0, xmitBytes.Length);
                com2.Write(xmitBytes, xmitBytes.Length / 2, xmitBytes.Length / 2);

                while (newBytesToRead > com1.BytesToRead)
                {
                    System.Threading.Thread.Sleep(50);
                }

                serPortProp.SetProperty("BytesToRead", newBytesToRead);
                Debug.WriteLine("Verifying properties after writing bytes");
                serPortProp.VerifyPropertiesAndPrint(com1);

                com2.BaudRate = origBaudRate;
                com1.BaudRate = origBaudRate;
            }
    }
Esempio n. 26
0
    private bool VerifyRtsEnableWithHandshake(bool rtsEnable, Handshake handshake)
    {
        SerialPort           com1        = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPortProperties serPortProp = new SerialPortProperties();
        Handshake            originalHandshake;
        bool expetectedRtsEnable;
        bool retValue = true;

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

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

        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        if (!VerifyRtsEnable(com1, rtsEnable))
        {
            Console.WriteLine("Err_2198219ahied Verifying RtsEnable failed before setting Handshake");
            retValue = false;
        }

        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;

        if (!VerifyRtsEnable(com1, expetectedRtsEnable))
        {
            Console.WriteLine("Err_6648ajheid Verifying RtsEnable failed after setting Handshake={0}", handshake);
            retValue = false;
        }

        com1.Handshake = originalHandshake;

        expetectedRtsEnable = rtsEnable;

        if (!VerifyRtsEnable(com1, expetectedRtsEnable))
        {
            Console.WriteLine("Err_1250588ajied Verifying RtsEnable failed after setting Handshake back to the original value={0}", originalHandshake);
            retValue = false;
        }

        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

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

        return(retValue);
    }
Esempio n. 27
0
        private void VerifyExceptionAtOpen(SerialPort com, string portName, ThrowAt throwAt, Type[] expectedExceptions)
        {
            string origPortName = com.PortName;

            SerialPortProperties serPortProp = new SerialPortProperties();

            if (null != expectedExceptions && 0 < expectedExceptions.Length)
            {
                serPortProp.SetAllPropertiesToDefaults();
            }
            else
            {
                serPortProp.SetAllPropertiesToOpenDefaults();
            }

            if (ThrowAt.Open == throwAt)
            {
                serPortProp.SetProperty("PortName", portName);
            }
            else
            {
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
            }

            try
            {
                com.PortName = portName;

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

                if (null != expectedExceptions && 0 < expectedExceptions.Length)
                {
                    Fail("ERROR!!! Expected Open() to throw ");
                    for (int i = 0; i < expectedExceptions.Length; ++i)
                    {
                        Console.Write(expectedExceptions[i] + " ");
                    }
                    Debug.WriteLine(" and nothing was thrown");
                }
            }
            catch (Exception e)
            {
                if (null == expectedExceptions || 0 == expectedExceptions.Length)
                {
                    Fail("ERROR!!! Expected Open() NOT to throw an exception and the following was thrown:\n{0}", e);
                }
                else
                {
                    bool exceptionFound      = false;
                    Type actualExceptionType = e.GetType();

                    for (int i = 0; i < expectedExceptions.Length; ++i)
                    {
                        if (actualExceptionType == expectedExceptions[i])
                        {
                            exceptionFound = true;
                            break;
                        }
                    }

                    if (exceptionFound)
                    {
                        Debug.WriteLine("Caught expected exception:\n{0}", e.GetType());
                    }
                    else
                    {
                        Fail("ERROR!!! Expected Open() throw ");
                        for (int i = 0; i < expectedExceptions.Length; ++i)
                        {
                            Console.Write(expectedExceptions[i] + " ");
                        }
                        Debug.WriteLine(" and  the following was thrown:\n{0}", e);
                    }
                }
            }

            serPortProp.VerifyPropertiesAndPrint(com);
            com.PortName = origPortName;
        }
Esempio n. 28
0
    public bool ReceivedBytesThreshold_Above_1()
    {
        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 + 1;
        com1.Open();
        com2.Open();
        com1.DataReceived += new SerialDataReceivedEventHandler(rcvEventHandler.HandleEvent);

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

        Console.WriteLine("Verifying writing less then number of bytes of ReceivedBytesThreshold then " + "setting ReceivedBytesThreshold to 1");

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

        while (com1.BytesToRead < receivedBytesThreshold)
        {
            System.Threading.Thread.Sleep(100);
        }

        if (0 != rcvEventHandler.NumEventsHandled)
        {
            Console.WriteLine("ERROR!!! Unexpected ReceivedEvent was firered NumEventsHandled={0}", rcvEventHandler.NumEventsHandled);
            retValue = false;
        }
        else
        {
            com1.ReceivedBytesThreshold = 1;

            if (!rcvEventHandler.WaitForEvent(SerialData.Chars, 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);

        if (!retValue)
        {
            Console.WriteLine("Err_006!!! Verifying writing less then number of bytes of ReceivedBytesThreshold then " + "setting ReceivedBytesThreshold to 1 FAILED");
        }

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

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

        return(retValue);
    }
Esempio n. 29
0
    private bool VerifyWriteBufferSize(SerialPort com1, int expectedWriteBufferSize)
    {
        SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);

        byte[] xmitBytes = new byte[Math.Max(expectedWriteBufferSize, com1.WriteBufferSize)];
        byte[] rcvBytes  = new byte[xmitBytes.Length];
        SerialPortProperties serPortProp = new SerialPortProperties();
        bool   retValue = true;
        Random rndGen   = new Random(-55);


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

        serPortProp.SetAllPropertiesToOpenDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        com2.ReadBufferSize = expectedWriteBufferSize;
        serPortProp.SetProperty("WriteBufferSize", expectedWriteBufferSize);

        com2.Open();

        int origBaudRate = com1.BaudRate;

        com2.BaudRate = 115200;
        com1.BaudRate = 115200;
        serPortProp.SetProperty("BaudRate", 115200);

        com1.Write(xmitBytes, 0, xmitBytes.Length);

        while (xmitBytes.Length > com2.BytesToRead)
        {
            System.Threading.Thread.Sleep(50);
        }

        Console.WriteLine("Verifying properties after changing WriteBufferSize");
        retValue &= serPortProp.VerifyPropertiesAndPrint(com1);

        com2.Read(rcvBytes, 0, rcvBytes.Length);

        for (int i = 0; i < expectedWriteBufferSize; i++)
        {
            if (rcvBytes[i] != xmitBytes[i])
            {
                Console.WriteLine("ERROR!!!: Expected to read byte {0} actual={1} at {2}", xmitBytes[i], rcvBytes[i], i);
                retValue = false;
            }
        }

        com2.BaudRate = origBaudRate;
        com1.BaudRate = origBaudRate;

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

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

        return(retValue);
    }