示例#1
0
        public void QuadPacketPacketInvalidChecksumData()
        {
            byte[] multiWiiData = new byte[100];
             MultiWiiPacket quadPacket = new MultiWiiPacket();

             // setup the MultiWii packet
             multiWiiData[0] = (byte)'$';
             multiWiiData[1] = (byte)'M';
             multiWiiData[2] = (byte)'<';
             multiWiiData[3] = 2;         // data size
             multiWiiData[4] = 100;       // command
             multiWiiData[5] =   4;       // data 1
             multiWiiData[6] = 250;       // data 2
             multiWiiData[7] =  45;       // checksum

             Assert.AreEqual(false, quadPacket.checkMultiWiiPacket(multiWiiData));
        }
示例#2
0
        public void QuadPacketPacketValidChecksumNoData()
        {
            byte[] multiWiiData = new byte[100];
             MultiWiiPacket quadPacket = new MultiWiiPacket();

             // setup the MultiWii packet
             multiWiiData[0] = (byte)'$';
             multiWiiData[1] = (byte)'M';
             multiWiiData[2] = (byte)'<';
             multiWiiData[3] = 0;         // data size
             multiWiiData[4] = 100;       // command
             multiWiiData[5] = 100;       // checksum

             Assert.AreEqual(true, quadPacket.checkMultiWiiPacket(multiWiiData));
        }
示例#3
0
        /// <summary>
        /// Routine Summary:
        ///
        ///    public bool readSerialPortMultiWii(out string response, int timeout_time);
        ///
        ///       response      - Characters received from the serial
        ///                       communications port.
        ///       timeout_time  - Time to allow between received characters.
        ///
        /// Description:
        ///
        ///    Routine to read characters from the serial communications port.
        ///
        /// Return Value:
        ///
        ///    true  - A carriage return was received in the serial string.
        ///    false - Did not receive a carriage return.
        ///
        /// Modification History:
        ///
        ///    12/08/05 JWY - First version of routine.
        ///    10/22/06 JWY - Removed the COM_carriage_return_received boolean
        ///                   since it was not being used.
        /// </summary>
        public static bool readSerialPortMultiWii(ref MultiWiiPacket quadPacket, int timeout_time)
        {
            byte[] buffer = new byte[16384];
             byte[] tempBuffer = new byte[16384];
             int serialBytesRead;
             string tempResponse;
             string response = null;

             // try to get info from the Comm Port.
             try
             {
            // check the serial port for characters, but time-out if no characters
            // are recieved
            // Note: start at 1 to ensure 100 passes before the 1 ms sleep
            // Note: divided by 10000 for seconds
            for (int timeout = 1; timeout < (timeout_time * 100); timeout++)
            {
               // check for serial port receive characters
               if (m_serialPort.BytesToRead > 0)
               {
                  // reset the time out timer
                  timeout = 0;

                  // read the bytes from the serial port
                  serialBytesRead = m_serialPort.Read(buffer, 0, m_serialPort.BytesToRead);

                  // copy the serial port characters to the temp buffer and check for a carriage return
                  for (int index = 0; index < serialBytesRead; index++)
                     tempBuffer[index] = buffer[index];

                  // parset the MultiWii packet
                  quadPacket.checkMultiWiiPacket(tempBuffer);

                  // check for a valid packet
                  if (quadPacket.State == MultiWiiPacket.MiltiWiiPacketState.VALID_RX_CMD)
                  {
                     // convert the byte array to a string
                     tempResponse = ASCIIEncoding.ASCII.GetString(tempBuffer, 0, quadPacket.PacketLength);

                     // copy the seral port characters to the response
                     response += tempResponse;
                     return (true);
                  }

                  // completed checking each character for a carriage return so copy the
                  // received characters into the repsonse string
                  tempResponse = ASCIIEncoding.ASCII.GetString(tempBuffer, 0, serialBytesRead);

                  // copy the seral port characters to the response
                  response += tempResponse;
               }

               // sleep for one-milisecond every 100th loop if a carriage return
               // has not been received
               if ((timeout % 100) == 0)
                  Thread.Sleep(1);
            }
             }
             catch (Exception ex)
             {
            // exception if a serial port failure
            MessageBox.Show("Exception: " + ex.ToString(), "Expection",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
             }

             // indicate that the command was received
             return (false);
        }