Ejemplo n.º 1
0
        public void QuadPacketInitialzation()
        {
            MultiWiiPacket quadPacket = new MultiWiiPacket();

             Assert.AreEqual(MultiWiiPacket.MiltiWiiPacketState.IDLE, quadPacket.State);
             Assert.AreEqual(0, quadPacket.PacketLength);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Realtime_GetVersion_Click(object sender, EventArgs e)
        {
            string command = "$M<64";
             MultiWiiPacket quadPacket = new MultiWiiPacket();

             if (DataTransfer.sendMultiWiiCommand(command, ref quadPacket, 200) == true)
             {
            tb_realtimeMessage.Text += "MultiWii Packet State: " + quadPacket.State.ToString() + Environment.NewLine;

            for (int index = 0; index < quadPacket.DataLength; index++)
            {
               tb_realtimeMessage.Text += Support.binary_to_HEX(quadPacket.PacketData[quadPacket.DataOffset + index]) + "  ";
            }

            tb_realtimeMessage.Text += Environment.NewLine;
            tb_realtimeMessage.Text += "Checksum: " + Support.binary_to_HEX(quadPacket.Checksum) + Environment.NewLine;
             }
             else
            tb_realtimeMessage.Text += "Time-out." + Environment.NewLine;
        }
Ejemplo n.º 3
0
        public void QuadPacketInvalidChecksumNoData()
        {
            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] = 99;

             // send the test MultiWii packet
             checkValidCharacter(quadPacket, multiWiiData[0], MultiWiiPacket.MiltiWiiPacketState.HEADER_M,       1);
             checkValidCharacter(quadPacket, multiWiiData[1], MultiWiiPacket.MiltiWiiPacketState.HEADER_ARROW,   2);
             checkValidCharacter(quadPacket, multiWiiData[2], MultiWiiPacket.MiltiWiiPacketState.DATA_SIZE,      3);
             checkValidCharacter(quadPacket, multiWiiData[3], MultiWiiPacket.MiltiWiiPacketState.HEADER_COMMAND, 4);
             checkValidCharacter(quadPacket, multiWiiData[4], MultiWiiPacket.MiltiWiiPacketState.CHECKSUM,       5);
             checkInvalidCharacter(quadPacket, multiWiiData[5]);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="quadPacket"></param>
 /// <param name="multiWiiData"></param>
 /// <param name="state"></param>
 /// <param name="packetLength"></param>
 void checkValidCharacter(MultiWiiPacket quadPacket, byte multiWiiData,
                        MultiWiiPacket.MiltiWiiPacketState state, int packetLength)
 {
     quadPacket.BuildMultiWiiPacket(multiWiiData);
      Assert.AreEqual(state, quadPacket.State);
      Assert.AreEqual(packetLength, quadPacket.PacketLength);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="quadPacket"></param>
 /// <param name="multiWiiData"></param>
 void checkInvalidCharacter(MultiWiiPacket quadPacket, byte multiWiiData)
 {
     quadPacket.BuildMultiWiiPacket(multiWiiData);
      Assert.AreEqual(MultiWiiPacket.MiltiWiiPacketState.IDLE, quadPacket.State);
      Assert.AreEqual(0, quadPacket.PacketLength);
 }
Ejemplo n.º 6
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));
        }
Ejemplo n.º 7
0
        public void QuadPacketPacketValidChecksumData()
        {
            byte checksum;
             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

             Assert.AreEqual(true, quadPacket.getMiltiWiiPacketChecksum(multiWiiData, out checksum));
             Assert.AreEqual(checksum, 152);
        }
Ejemplo n.º 8
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));
        }
Ejemplo n.º 9
0
        public void QuadPacketInvalidTransmit()
        {
            byte[] multiWiiData = new byte[100];
             MultiWiiPacket quadPacket = new MultiWiiPacket();

             // setup the MultiWii packet
             multiWiiData[0] = (byte) '$';
             multiWiiData[1] = (byte) 'M';
             multiWiiData[2] = (byte) 'n';

             // send the test MultiWii packet
             checkValidCharacter(quadPacket, multiWiiData[0], MultiWiiPacket.MiltiWiiPacketState.HEADER_M, 1);
             checkValidCharacter(quadPacket, multiWiiData[1], MultiWiiPacket.MiltiWiiPacketState.HEADER_ARROW, 2);
             checkInvalidCharacter(quadPacket, multiWiiData[2]);
        }
Ejemplo n.º 10
0
        public void QuadPacketInvalidStartCharacter()
        {
            byte [] multiWiiData = new byte[100];
             MultiWiiPacket quadPacket = new MultiWiiPacket();

             // setup the MultiWii packet
             multiWiiData[0] = 0;

             // check the packet for still in IDLE state (invalid header)
             checkInvalidCharacter(quadPacket, multiWiiData[1]);
        }
Ejemplo n.º 11
0
        public void QuadPacketInvalidDataSize()
        {
            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] = 65;

             // send the test MultiWii packet
             checkValidCharacter(quadPacket, multiWiiData[0], MultiWiiPacket.MiltiWiiPacketState.HEADER_M, 1);
             checkValidCharacter(quadPacket, multiWiiData[1], MultiWiiPacket.MiltiWiiPacketState.HEADER_ARROW, 2);
             checkValidCharacter(quadPacket, multiWiiData[2], MultiWiiPacket.MiltiWiiPacketState.DATA_SIZE, 3);
             checkInvalidCharacter(quadPacket, multiWiiData[3]);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        public static bool sendMultiWiiCommand(string command, ref MultiWiiPacket response, int timeout)
        {
            byte[] multiWiiCommand = new byte[100];
             int length;

             // convert the command string to a MultiWii binary command
             length = MultiWiiPacket.convertCommandToMultiWiiBinary(command, ref multiWiiCommand);

             // always wrap up working with Comm Ports in exception handlers
             try
             {
            // clear the communications receive buffer
            clearSerialPortInputCharacters();

            // write the command to the port
            Send(multiWiiCommand, length);
             }
             catch
             {
            // warn the user that the serial port is not functioning
            MessageBox.Show("Unable to communicate through the serial port\r", "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
            response = null;
            return (false);
             }

             // read the serial response
             if (readSerialPortMultiWii(ref response, timeout) == false)
            return (false);

             // characters received from the serial port
             return (true);
        }
Ejemplo n.º 13
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);
        }