// /// <summary> // /// Check if the MITes receiver has sent new data since the last time FillBytesBuffer was called. // /// </summary> // /// <returns>True if there is new data to grab.</returns> // public bool IsNewData() // { // return spc.isNewData(); // } /// <summary> /// If there is new data (which should have been checked with a prior call to IsNewData) /// then fill the given buffer with the data. /// </summary> /// <param name="serialBytesBuffer">The buffer to fill.</param> /// <returns>The number of new data bytes.</returns> public int FillBytesBuffer(byte[] serialBytesBuffer) { if (isRunning) { return(spc.FillBytesBuffer(serialBytesBuffer)); } else { return(0); } }
/// <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); }