예제 #1
0
		static void Main(string[] args)
		{
			Console.WriteLine("Start");
			scheduler = new SpringCardIWM2_Serial_Scheduler("COM11");
			scheduler.SetReaderFoundCallback(new SpringCardIWM2_Serial_Scheduler.ReaderFoundCallback(OnReaderFound));
			scheduler.Start((byte) 1, (byte) 10);
		}
		void CbCOMPortsSelectedIndexChanged(object sender, EventArgs e)
		{
			if (IsPortAlreadyScheduled(cbCOMPorts.Text))
			{
				/* There already is a scheduler managing this COM port */
				/* --------------------------------------------------- */
				
				/* Retrieve it from the list ... */
				selectedScheduler = RetrieveScheduler(cbCOMPorts.Text);
				
				/* ... and set the apprpriate callbacks (they may have been set from another form, which has been previously closed) */
				SetCallbacks(selectedScheduler);
				
				/* Enable the "Stop" and disable the "Start" buttons */
				btStart.Enabled = false;
				btStop.Enabled = true;

				/* This already-running scheduler may already have installed readers: */
 				/* if this is the case, get them and print them in the DataGridView		*/
				if (selectedScheduler.GetInstalledReaders() != null)
				{
					foreach (SpringCardIWM2_Serial_Reader reader in selectedScheduler.GetInstalledReaders())
						OnReaderFound(cbCOMPorts.Text, "MK2", reader.GetAddress(), selectedScheduler.IsMonitored(reader.GetAddress()) );
				
				} else
				{
					lbClick.Text = "";
				}
					
			} else
			{
				/* There is no scheduler managing this COM port */
				/* -------------------------------------------- */

				/* Remove the potential readers from the DataGridView */ 
				dataGridView.Rows.Clear();
				dataGridView.Refresh();
				
				/* Enable the "Start" and disable the "Stop" buttons */
				btStart.Enabled = true;
				btStop.Enabled = false;				
				lbClick.Text = "";
			}			
		
		}
		void BtStopClick(object sender, EventArgs e)
		{
			if (selectedScheduler != null)
			{
				/* Stop and realease the specified scheduler */
				selectedScheduler.Stop();
				selectedScheduler = null;
				dataGridView.Rows.Clear();
				dataGridView.Refresh();
				lbClick.Text = "";
			}
			
			/* Enable the "Start" and disable the "Stop" buttons */
			btStart.Enabled = true;
			btStop.Enabled = false;
			
		}
		void BtStartClick(object sender, EventArgs e)
		{
			/* Create a new SpringCardIWM2_Serial_Scheduler object on this port, ... */
			selectedScheduler = new SpringCardIWM2_Serial_Scheduler(cbCOMPorts.Text);
			
			/* ... indicate whether all sent/received frames must be printed ...   */
			selectedScheduler.SetShowFrames(show_frames);
			
			/* ... sets all the callcacks to be informed of its activities ... */
			SetCallbacks(selectedScheduler);
			
			/* ... and launch it ! */ 
			selectedScheduler.Start((byte) nudAddMin.Value, (byte) nudAddMax.Value);

			/* If the user switches in the future to another port, remember this scheduler is active and running */ 
			MF.RefineSchedulersList(schedulers, cbCOMPorts.Text);
			schedulers.Add(selectedScheduler);
			
			/* Finally, enable the "Stop" and disable the "start" buttons */
			btStart.Enabled = false;
			btStop.Enabled = true;
			
		}
		/* This method sets all the callbacks (methods defined here) that may be called from a scheduler (SpringCardIWM2_Serial_Scheduler object) */
		private void SetCallbacks(SpringCardIWM2_Serial_Scheduler selectedScheduler)
		{
			selectedScheduler.SetReaderFoundCallback(new SpringCardIWM2_Serial_Scheduler.ReaderFoundCallback(OnReaderFound));			
			selectedScheduler.SetReaderDroppedCallback(new SpringCardIWM2_Serial_Scheduler.ReaderDroppedCallback(OnReaderDropped));
			selectedScheduler.SetSchedulerStoppedCallback(new SpringCardIWM2_Serial_Scheduler.SchedulerStoppedCallback(OnSchedulerStopped));			
		}
		public SpringCardIWM2_Serial_Reader(SpringCardIWM2_Serial_Scheduler _sched, byte _address)
		{
			sched 					= _sched;
			address 				= _address;
			block_number 		= 0;
			com_errors			= 0;
			is_scheduled		= true;
			keep_cool_delay	= new TimeSpan(0, 0, 0, 0, 500); 		/* 500 milliseconds */
			keep_cool_until = DateTime.Now;
			queue						= new BlockingCollection<SpringCardIWM2_Serial_Reader.NextMessage>();

		}