Example #1
0
        public void EnqueueCommJob(Priority priority, ICommunicationJob job)
        {
            CommJobInfo info = new CommJobInfo();

            info.WaitHandle = null;
            info.Job        = job;

            // always enqueue this job and run the highest priority one instead
            EnqueueJob(priority, info);
        }
Example #2
0
            public override bool Equals(object o)
            {
                if (o == null || o.GetType() != GetType())
                {
                    return(false);
                }

                CommJobInfo op2 = o as CommJobInfo;

                return(Job == op2.Job);
            }
Example #3
0
        public void EnqueueCommJobAndWait(Priority priority, ICommunicationJob job)
        {
            CommJobInfo info = new CommJobInfo();

            info.WaitHandle = new AutoResetEvent(false);
            info.Job        = job;

            // always enqueue this job and run the highest priority one instead
            EnqueueJob(priority, info);
            info.WaitHandle.WaitOne();
        }
Example #4
0
        private async Task ProcessJob(CommJobInfo jobInfo, CancellationToken ct)
        {
            // generate the command string for this job
            string cmd  = jobInfo.Job.GenerateCommand();
            string resp = "";

            // send the command in a retry loop
            // in case we get back NACKs
            const uint tryThreshold = 3;
            uint       tryCnt;

            for (tryCnt = 1; tryCnt <= tryThreshold && !ct.IsCancellationRequested; tryCnt++)
            {
                try
                {
                    long loopStartTime = stopWatch.ElapsedMilliseconds;  // mark the start time of the cycle.
                    //Debug.WriteLine(String.Format("Comm: Try {0}: cmd: '{1}'", tryCnt, cmd));
                    await SerialPortWriteLine(cmd);

                    resp = await SerialPortReadLine();

                    long elapsedTime = stopWatch.ElapsedMilliseconds - loopStartTime;

                    //if(cmd.StartsWith("pwm"))
                    //    Debug.WriteLine(String.Format("Comm: Try {0}: '{1}' -> '{2}'  {3} ms", tryCnt, cmd, resp, elapsedTime));

                    // we usually get an ACK or a number (sensor reading) in response.
                    if (resp != "NACK")
                    {
                        break;
                    }
                }
                catch (TimeoutException e)
                {
                    Debug.WriteLine(String.Format("Element:CommunicationTask: CommErr: Try {0}: '{1}' -> (timeout) - {2}", tryCnt, cmd, e.Message));
                }
            }

            if (!ct.IsCancellationRequested)
            {
                if (tryCnt >= tryThreshold)
                {
                    //TODO: communication exception
                    Debug.WriteLine(String.Format(
                                        "#: aborting '{0}' after {1} tries", cmd, tryCnt - 1));
                }
                else
                {
                    // process the response
                    jobInfo.Job.ProcessResponse(resp);
                }
            }
        }
Example #5
0
        private void EnqueueJob(Priority priority, CommJobInfo job)
        {
            Queue <CommJobInfo> jobQueue = jobQueues[(int)priority];

            lock (jobQueue)
            {
                if (!jobQueue.Contains(job))
                {
                    jobQueue.Enqueue(job);
                    sem.Up();
                }
            }
        }
Example #6
0
        private CommJobInfo DequeueJob()
        {
            sem.Down();

            for (uint i = 0; i < numQueues; ++i)
            {
                Queue <CommJobInfo> jobQueue = jobQueues[i];
                lock (jobQueue)
                {
                    if (jobQueue.Count > 0)
                    {
                        CommJobInfo job = jobQueue.Dequeue();
                        return(job);
                    }
                }
            }

            return(null);
        }
Example #7
0
        private async void ThreadProc(CancellationToken ct)
        {
            while (!ct.IsCancellationRequested)
            {
                // select the next (highest priority) job
                CommJobInfo jobInfo = DequeueJob();
                if (!running)
                {
                    break;
                }

                if (jobInfo != null)
                {
                    try
                    {
                        await ProcessJob(jobInfo, ct);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                    finally
                    {
                        if (jobInfo.WaitHandle != null)
                        {
                            jobInfo.WaitHandle.Set();
                        }
                    }
                }
                else
                {
                    await Task.Delay(10);
                }
            }
            running = false;
            ClearJobQueues();
        }
        private async Task ProcessJob(CommJobInfo jobInfo, CancellationToken ct)
		{
			// generate the command string for this job
			string cmd = jobInfo.Job.GenerateCommand();
			string resp = "";

			// send the command in a retry loop
			// in case we get back NACKs
			const uint tryThreshold = 3;
			uint tryCnt;
			for (tryCnt = 1; tryCnt <= tryThreshold && !ct.IsCancellationRequested; tryCnt++)
			{
				try
				{
                    long loopStartTime = stopWatch.ElapsedMilliseconds;  // mark the start time of the cycle.
                    //Debug.WriteLine(String.Format("Comm: Try {0}: cmd: '{1}'", tryCnt, cmd));
                    await SerialPortWriteLine(cmd);
					resp = await SerialPortReadLine();
                    long elapsedTime = stopWatch.ElapsedMilliseconds - loopStartTime;

                    //if(cmd.StartsWith("pwm"))
                    //    Debug.WriteLine(String.Format("Comm: Try {0}: '{1}' -> '{2}'  {3} ms", tryCnt, cmd, resp, elapsedTime));

                    // we usually get an ACK or a number (sensor reading) in response.
					if (resp != "NACK")
					{
						break;
					}
				}
				catch (TimeoutException e)
				{
                    Debug.WriteLine(String.Format("Element:CommunicationTask: CommErr: Try {0}: '{1}' -> (timeout) - {2}", tryCnt, cmd, e.Message));
				}
			}

            if (!ct.IsCancellationRequested)
            {
                if (tryCnt >= tryThreshold)
                {
                    //TODO: communication exception
                    Debug.WriteLine(String.Format(
                        "#: aborting '{0}' after {1} tries", cmd, tryCnt - 1));
                }
                else
                {
                    // process the response
                    jobInfo.Job.ProcessResponse(resp);
                }
            }
		}
        private void EnqueueJob(Priority priority, CommJobInfo job)
		{
			Queue<CommJobInfo> jobQueue = jobQueues[(int)priority];
			lock (jobQueue)
			{
				if (!jobQueue.Contains(job))
				{
					jobQueue.Enqueue(job);
                    sem.Up();
                }
			}
		}
Example #10
0
		public void EnqueueCommJob(Priority priority, ICommunicationJob job)
		{
			CommJobInfo info = new CommJobInfo();
			info.WaitHandle = null;
			info.Job = job;

			// always enqueue this job and run the highest priority one instead
			EnqueueJob(priority, info);
		}
Example #11
0
		public void EnqueueCommJobAndWait(Priority priority, ICommunicationJob job)
		{
			CommJobInfo info = new CommJobInfo();
			info.WaitHandle = new AutoResetEvent(false);
			info.Job = job;

			// always enqueue this job and run the highest priority one instead
			EnqueueJob(priority, info);
			info.WaitHandle.WaitOne();
		}