示例#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);
        }
示例#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);
            }
示例#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();
        }
示例#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);
                }
            }
        }
示例#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();
                }
            }
        }
示例#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);
        }
示例#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();
        }
示例#8
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);
                }
            }
		}
示例#9
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();
                }
			}
		}
示例#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);
		}
示例#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();
		}