Example #1
0
        /// <summary>
        /// Test method.
        /// </summary>
        public static void Main()
        {
            int  bytesBufferSize = 2048;
            bool isPrintWarnings = true;

            // Works on the PC, PPC
            //SerialPortController spc = new SerialPortController(SerialPortController.NO_THREADS, isPrintWarnings, bytesBufferSize);

            // Works on the PC, PPC
            SerialPortController spc = new SerialPortController(SerialPortController.USE_THREADS, isPrintWarnings, bytesBufferSize);

            Thread.Sleep(100);

            //PPC casing serial: 1, bluetooth 5
            //Smartphone serial: 6

            if (spc.SetPort(1))
            {
                Console.WriteLine("Set port successfully");
            }
            else
            {
                Console.WriteLine("Could not set port number");
                //Application.Exit();
            }

            //spc.SetBaudRate(57600);
            spc.SetBaudRate(9600);
            spc.SetParity(0);             //none
            spc.SetStopBits(1);
            spc.PortOpen();

            long time = Environment.TickCount;

            byte[] serialBytesBuffer = new byte[bytesBufferSize];
            int    bytesFound;
            byte   v = (byte)0;

            int total = 0;

            Console.WriteLine("Serial");
            spc.ClearPort();

            int ave   = 0;
            int count = 0;

            int sleepval = 10;

            time  = Environment.TickCount;
            ave   = 0;
            count = 0;
            Console.WriteLine("");
            Console.WriteLine("Testing sleep of " + sleepval);
            while ((Environment.TickCount - time) < 60000)
            {
                bytesFound = spc.FillBytesBuffer(serialBytesBuffer);

//				if ((bytesFound != 0) || ((count % 1000) == 0))
//					Console.WriteLine ("Bytes: " + bytesFound + "total: " + total + " Count: " + count);

                total += bytesFound;
                ave   += bytesFound;
                count++;

                for (int i = 0; i < bytesFound; i++)
                {
                    v = serialBytesBuffer[i];
                    Console.WriteLine(v);
                }
                Thread.Sleep(sleepval);
            }
            Console.WriteLine("Total: " + total);
            Console.WriteLine("Ave chunk: " + (ave / ((double)count)));
            Thread.Sleep(10000);

            spc.PortClose();
        }
		/// <summary>
		/// Test if a port has a receiver attached by trying to open and reading data.
		/// </summary>
		/// <param name="portNum">Port to try</param>
		/// <param name="maxBytesBuffer">Maximum buffer size</param>
		/// <returns></returns>
		public bool TestPort(int portNum, int maxBytesBuffer)
		{
			isRunning = true; 

			bool isValid = true;
			bool isOpen = false;

			Console.WriteLine ("Testing PORT: " + portNum);
//            MessageBox.Show("Testing PORT: " + portNum);
	
			// Setup serial port
			SerialPortController spcTest = new SerialPortController(SerialPortController.USE_THREADS,PRINT_WARNINGS,maxBytesBuffer);
			Thread.Sleep(1000);
			spcTest.SetBaudRate(57600);

			if (spcTest.SetPort(portNum))
			{
				spcTest.SetParity(0); //none
				spcTest.SetStopBits(1);
				try 
				{
					spcTest.PortOpen();
					isOpen = true; 
				}
				catch (ApplicationException e)
				{
					e.ToString ();
					isValid = false;
					isOpen = false;					
				}
			}
			else
			{
				isValid = false;
			}

			// If opened, then test if incoming data looks like MITes data for 1 second
			if (isValid)
			{
                //MessageBox.Show("Able to open " + portNum);
				isValid = false;
				byte[] someData = new byte[4000];

				int startTime = Environment.TickCount;
				// Loop for 1 second and wait for a DD 
				while ((Environment.TickCount-startTime) < 1000)
				{
					//					if (spcTest.isNewData())
					//					{
					int j = spcTest.FillBytesBuffer (someData);
					//Console.WriteLine ("Data: " + someData.Length);
					if (j>1)
						for (int i = 0; i < j-1; i++)
							if ((someData[i] == (int) 68) &&
								(someData[i+1] == (int) 68))
								isValid = true;
					//					}
					Thread.Sleep (100);
				}
			}


			// Shutdown
			//if (isOpen)
			//	spcTest.PortClose();
			//spcTest = null;

			//isRunning = false;
            spc = spcTest;
			return isValid;
		}
		/// <summary>
		/// Test method.
		/// </summary>
		public static void Main()
		{
			int bytesBufferSize = 2048; 
			bool isPrintWarnings = true; 

			// Works on the PC, PPC 
			//SerialPortController spc = new SerialPortController(SerialPortController.NO_THREADS, isPrintWarnings, bytesBufferSize);
		
			// Works on the PC, PPC
			SerialPortController spc = new SerialPortController(SerialPortController.USE_THREADS, isPrintWarnings, bytesBufferSize);

			Thread.Sleep(100);

			//PPC casing serial: 1, bluetooth 5
			//Smartphone serial: 6

			if (spc.SetPort(1))
				Console.WriteLine ("Set port successfully");
			else
			{
				Console.WriteLine ("Could not set port number");
				//Application.Exit();
			}

			//spc.SetBaudRate(57600);
			spc.SetBaudRate(9600);
			spc.SetParity(0); //none
			spc.SetStopBits(1);
			spc.PortOpen();

			long time = Environment.TickCount;

			byte[] serialBytesBuffer = new byte[bytesBufferSize];
			int bytesFound;
			byte v = (byte) 0;

			int total = 0; 
			Console.WriteLine("Serial");
			spc.ClearPort();

			int ave = 0; 
			int count = 0;

			int sleepval = 10; 
			time = Environment.TickCount;
			ave = 0; 
			count = 0;
			Console.WriteLine("");
			Console.WriteLine("Testing sleep of " + sleepval);
			while ((Environment.TickCount - time) < 60000)
			{
				bytesFound = spc.FillBytesBuffer(serialBytesBuffer);

//				if ((bytesFound != 0) || ((count % 1000) == 0))
//					Console.WriteLine ("Bytes: " + bytesFound + "total: " + total + " Count: " + count);

				total += bytesFound;
				ave += bytesFound;
				count++; 

				for (int i = 0; i < bytesFound; i++)
				{
					v = serialBytesBuffer[i];
					Console.WriteLine (v);
				}
				Thread.Sleep(sleepval);
			}
			Console.WriteLine("Total: " + total);
			Console.WriteLine("Ave chunk: " + (ave/((double)count)));
			Thread.Sleep(10000);
			
			spc.PortClose();
		}
        public void InitializeController(int portNumber, int maxBytesBuffer, bool printWarnings, bool isUsingThreads)
        {

            Console.WriteLine("Receiver found on COM " + portNumber + "!");

            // Checked that port is valid, so go ahead and reopen it. 
            spc = new SerialPortController(isUsingThreads, printWarnings, maxBytesBuffer);
            Thread.Sleep(1000);
            spc.SetBaudRate(57600);
            spc.SetPort(portNumber);
            spc.SetParity(0); //none
            spc.SetStopBits(1);
            isRunning = spc.PortOpen();

        }