Example #1
0
        /// <summary>
        /// Creates a new Interface to communicate with
        /// </summary>
        /// <param name="ComPortName">The name of the COM port</param>
        public Interface(string ComPortName)
        {
            waitHandle = new AutoResetEvent(false);
            inDigital  = new SealedConcurrentDictionary <InputDigital, bool>((InputDigital[])Enum.GetValues(typeof(InputDigital)));
            inAnalog   = new SealedConcurrentDictionary <InputAnalog, int>((InputAnalog[])Enum.GetValues(typeof(InputAnalog)));
            outMotor   = new SealedConcurrentDictionary <OutputMotor, MotorState>((OutputMotor[])Enum.GetValues(typeof(OutputMotor)));



            com = new InterfaceCom(ComPortName, waitHandle, inDigital, inAnalog, outMotor);
        }
Example #2
0
        /// <summary>
        /// Creates a new Interface COM communication
        /// </summary>
        /// <param name="comName">Name of the COM port</param>
        /// <param name="inDigital">The dictionary that holds the digital state</param>
        /// <param name="inAnalog">The dictionary that holds the analog state</param>
        /// <param name="outMotor">The dictionary that holds the motor state</param>
        public InterfaceCom(string comName, AutoResetEvent waitHandle, SealedConcurrentDictionary <InputDigital, bool> inDigital, SealedConcurrentDictionary <InputAnalog, int> inAnalog, SealedConcurrentDictionary <OutputMotor, MotorState> outMotor)
        {
            this.inDigital  = inDigital;
            this.inAnalog   = inAnalog;
            this.outMotor   = outMotor;
            this.waitHandle = waitHandle;

            sendDelay = 50;//(int)Math.Ceiling((1000.0 / (InterfaceProperties.ComBaudRate / InterfaceProperties.ComBitsTotal * 10f))); // 10 = total packets for one readwrite (2 * 2 send + 2 * 3 receive)

            try
            {
                port = new SerialPort(comName, InterfaceProperties.ComBaudRate, InterfaceProperties.ComParityBit, InterfaceProperties.ComDataBits, InterfaceProperties.ComStopBits);
                port.WriteTimeout = ReadWriteTimeout;
                port.ReadTimeout  = ReadWriteTimeout;
            }
            catch (IOException e)
            {
                throw new InvalidDataException("The COM name is invalid, or COM port is blocked. \n\n" + e.StackTrace);
            }
        }