/// <summary>
 /// Initializes a new instance of the <see cref="SerialTimingUnit" /> class based with a specific serial channel.
 /// </summary>
 /// <param name="commInterface">The <see cref="ISerialCommunication"/> used for communicating with this Rider ID unit</param>
 /// <param name="unitId">The name of the timing unit</param>
 /// <param name="token">The cancellation token for this unit</param>
 public SerialTimingUnit(ISerialCommunication commInterface, string unitId, CancellationToken token, int startId, int endId) : base(commInterface, unitId, token)
 {
     this.timingEvents = new ConcurrentQueue <TimingTriggeredEventArgs>();
     this.StartId      = startId;
     this.EndId        = endId;
     this.commandQueue.Enqueue(new CommandData((ushort)SerialTimerCommands.UpdateOpMode, 0, new byte[] { 2 }));
 }
Пример #2
0
        /// <summary>
        /// Gets the serial communication device with the specified identifier.
        /// Prefix <c>directserial</c> (i.e. <c>directserial:COM1</c> or <c>directserial:/dev/ttyS0</c>) gets a direct serial connection.
        /// Prefix <c>xbee</c> followed by a serial port and a 64-bit <c>Xbee</c> address gets an <c>Xbee</c> connection. (e.g.: <c>xbee:COM4:0013A20041BB64A6</c> or <c>xbee:/dev/ttyS0:0013A20041BB64A6</c>)
        /// </summary>
        /// <param name="identifier">The device to get.</param>
        /// <returns>An ISerialCommunication device.</returns>
        public ISerialCommunication GetCommunicationDevice(string identifier)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new ArgumentNullException("identifier");
            }

            if (!Regex.IsMatch(identifier, IdentifierRegex))
            {
                throw new ArgumentException($"Channel identifier {identifier} does not match the format type:port(:address) or regex {IdentifierRegex}", "identifier");
            }

            GroupCollection identifierParts = Regex.Match(identifier, IdentifierRegex).Groups;

            ISerialCommunication communicationDevice = null;

            Log.Info($"Getting device for {identifier}");

            if (identifierParts.Count > 2)
            {
                string communicationType = identifierParts[1].Value;
                switch (communicationType)
                {
                case "xbee":
                    if (identifierParts.Count != 4)
                    {
                        throw new ArgumentException($"Not enough argument supplied for communication type xbee: {identifier}");
                    }
                    else
                    {
                        communicationDevice = this.GetXbeeDevice(identifierParts[2].Value, identifierParts[3].Value);
                    }

                    break;

                case "directserial":
                    communicationDevice = this.GetSerialDevice(identifierParts[2].Value);
                    break;

                default:
                    throw new ArgumentException($"Invalid communication type: {communicationType} in identifier {identifier}");
                }
            }

            Log.Info($"Got device {communicationDevice} for identifier {identifier}");

            return(communicationDevice);
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommunicationProtocol" /> class.
        /// </summary>
        /// <param name="channel">The <see cref="ISerialCommunication"/> for this instance to use.</param>
        /// <exception cref="ArgumentNullException">When <paramref name="channel"/> is null</exception>
        public CommunicationProtocol(ISerialCommunication channel)
        {
            if (channel == null)
            {
                throw new ArgumentNullException("channel");
            }

            this.communicationChannel = channel;
            this.communicationChannel.DataReceived           += this.CommunicationChannel_DataReceived;
            this.communicationChannel.ConnectionStateChanged += this.CommunicationChannel_ConnectionStateChanged;
            this.communicationChannel.Failure += this.CommunicationChannel_Failure;

            this.timeoutTimer                  = new System.Timers.Timer();
            this.timeoutTimer.Interval         = 200;
            this.timeoutTimer.Elapsed         += this.TimeoutTimer_Elapsed;
            this.responseTimeoutTimer          = new Timer(750);
            this.responseTimeoutTimer.Elapsed += this.ResponseTimeoutTimer_Elapsed;
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractCommunicatingUnit" /> class based with a specific serial channel.
        /// </summary>
        /// <param name="commInterface">The <see cref="ISerialCommunication"/> used for communicating with this Rider ID unit</param>
        /// <param name="unitId">The identifier for this unit</param>
        /// <param name="token">The cancellation token for this unit</param>
        public AbstractCommunicatingUnit(ISerialCommunication commInterface, string unitId, CancellationToken token)
        {
            if (commInterface == null)
            {
                throw new ArgumentNullException("commInterface");
            }

            this.protocolHandler = new CommunicationProtocol(commInterface);
            this.protocolHandler.ConnectionStateChanged += this.ProtocolHandler_ConnectionStateChanged;
            this.protocolHandler.NewDataArrived         += this.ProtocolHandler_NewDataArrived;
            this.eventThread = new Thread(this.RunEventThread)
            {
                IsBackground = true
            };
            this.commandQueue              = new ConcurrentQueue <CommandData>();
            this.commTimeoutTimer.Elapsed += this.CommTimeoutTimer_Elapsed;
            this.unitId = unitId;
            this.eventThread.Start();

            this.cancellationToken = token;
        }
Пример #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            CancellationTokenSource source = new CancellationTokenSource();

            CommunicationManager CommunicationManager = new CommunicationManager(source.Token);

            serialPort          = CommunicationManager.GetCommunicationDevice("xbee:COM4:0013A20041BB64A6");
            serialPort.Failure += SerialPort_Failure;
            serialPort.ConnectionStateChanged += SerialPort_ConnectionStateChanged;
            serialPort.DataReceived           += SerialPort_DataReceived;

            string line = Console.ReadLine();

            while (line != "Q")
            {
                serialPort.Write(Encoding.ASCII.GetBytes(line));
                line = Console.ReadLine();
            }

            Console.WriteLine("Goodbye World!");
        }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SerialTimingUnit" /> class based with a specific serial channel.
 /// </summary>
 /// <param name="commInterface">The <see cref="ISerialCommunication"/> used for communicating with this Rider ID unit</param>
 /// <param name="unitId">The name of the timing unit</param>
 /// <param name="token">The cancellation token for this unit</param>
 public SerialTimingUnit(ISerialCommunication commInterface, string unitId, CancellationToken token) : base(commInterface, unitId, token)
 {
     this.timingEvents = new ConcurrentQueue <TimingTriggeredEventArgs>();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="BLERiderIdUnit" /> class based with a specific serial channel.
 /// </summary>
 /// <param name="commInterface">The <see cref="ISerialCommunication"/> used for communicating with this Rider ID unit</param>
 /// <param name="unitId">The unit name</param>
 /// <param name="distanceLimit">The distance in meter within which a beacon must be to be considered in range.</param>
 /// <param name="token">The cancellation token for this unit</param>
 public BLERiderIdUnit(ISerialCommunication commInterface, string unitId, double distanceLimit, CancellationToken token) : base(commInterface, unitId, token)
 {
     this.knownRiders          = new List <Rider>();
     this.eventQueue           = new ConcurrentQueue <RiderIDQueuedEvent>();
     this.maxDetectionDistance = distanceLimit;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="BLERiderIdUnit" /> class based with a specific serial channel.
 /// Using a default max detection distance of 4 meter.
 /// </summary>
 /// <param name="commInterface">The <see cref="ISerialCommunication"/> used for communicating with this Rider ID unit</param>
 /// <param name="unitId">The unit name</param>
 /// <param name="token">The cancellation token for this unit</param>
 public BLERiderIdUnit(ISerialCommunication commInterface, string unitId, CancellationToken token) : this(commInterface, unitId, 4.0, token)
 {
 }
Пример #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="XbeeNetwork" /> class.
 /// </summary>
 /// <param name="communication">The serial communication channel utilized by this network</param>
 public XbeeNetwork(ISerialCommunication communication)
 {
     this.Initialize(communication);
 }
Пример #10
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            CancellationTokenSource source = new CancellationTokenSource();

            CommunicationManager CommunicationManager = new CommunicationManager(source.Token);

            string comPort = "directserial:COM5";

            if (args.Length == 1)
            {
                comPort = args[0];
            }

            serialPort = CommunicationManager.GetCommunicationDevice(comPort);

            proto = new CommunicationProtocol(serialPort);
            proto.NewDataArrived         += Proto_NewDataArrived;
            proto.ConnectionStateChanged += Proto_ConnectionStateChanged;

            string line = Console.ReadLine();

            while (line != "Q")
            {
                CommandData cmd = null;
                switch (line.ToLowerInvariant())
                {
                case "listallowed":
                    cmd = new CommandData(0x0003, 0x0000, new byte[2]);
                    break;

                case "detectall":
                    cmd = new CommandData(0x0004, 0x0000, new byte[2]);
                    break;

                case "addallowed":
                    cmd = new CommandData(0x0001, 0x0000, GetCommandData(Console.ReadLine()));
                    break;

                case "removeallowed":
                    cmd = new CommandData(0x0002, 0x0000, GetCommandData(Console.ReadLine()));
                    break;

                case "getclosest":
                    cmd = new CommandData(0x0005, 0x0000, new byte[2]);
                    break;

                default:
                    break;
                }

                if (cmd != null)
                {
                    if (proto.ReadyToSend())
                    {
                        proto.SendCommand(cmd);
                    }
                    else
                    {
                        WriteLineToConsole("Proto not ready to send, try again later");
                    }
                }

                line = Console.ReadLine();
            }

            Console.WriteLine("Goodbye World!");
        }