private void buttonSendSample_Click(object sender, RoutedEventArgs e)
        {
            string data_test;
            int    i = 0;

            /* Get Activity and File */
            activity_current = byte.Parse(textBoxActivity.Text);
            if (activity_current != activity_previous)
            {
                activity_file = 0;
            }

            /* Open file if it isn't open */
            try
            {
                sample_file = File.OpenRead(openDialog.FileName);
            }
            catch (Exception ex) { }

            /* Search for "data" */
            sample_file.Read(read_buffer, 0, 3);
            do
            {
                sample_file.Read(read_buffer, 3 + i, 1);
                data_test = enc8.GetString(read_buffer, i++, 4);
            } while (data_test != "data");

            /* Now read next 4 bytes for file size */
            sample_file.Read(read_buffer, 0, 4);
            send_data_size = BitConverter.ToInt32(read_buffer, 0);

            /* Now read that many bytes into the buffer to send */
            int num_bytes = 4096;

            this.Dispatcher.Invoke((Action)(() =>
            {
                num_bytes = (int.Parse(textBoxSampleNumberOfBytes.Text) > 0) ? int.Parse(textBoxSampleNumberOfBytes.Text) : 4096;
            }));
            send_data_size = (send_data_size < num_bytes) ? send_data_size : num_bytes;


            /* Now send a FEED to the CDC */

            Array.Copy(BitConverter.GetBytes(send_data_size), 0, sendSample, 12, 4);
            sendSample[10] = activity_current;
            sendSample[11] = activity_file;

            event_type        = ProductCOMEvents.FeedDone;
            activity_previous = activity_current;
            data_buffers_sent = 0;
            _serialPort.Write(sendSample, 0, Constants.USB_EXTERNAL_BUFFER_SIZE);
        }
 private void onError(Exception ex)
 {
     _serialPort.Close();
     disableFields();
     this.Dispatcher.Invoke((Action)(() =>
     {
         label1.Content = "ERROR: " + ex.Message;
     }));
     event_type = ProductCOMEvents.None;
     if (sample_file != null)
     {
         sample_file.Close();
     }
 }
 private void buttonResetFiles_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (_serialPort.IsOpen)
         {
             event_type = ProductCOMEvents.resetFilesDone;
             _serialPort.Write(resetFiles, 0, Constants.USB_EXTERNAL_BUFFER_SIZE);
         }
     }
     catch (Exception ex)
     {
         _serialPort.Close();
         this.Dispatcher.Invoke((Action)(() =>
         {
             labelResetFiles.Content = ex.Message;
         }));
     }
 }
        private void Serial_DataReceived(object s, SerialDataReceivedEventArgs e)
        {
            switch (event_type)
            {
            case ProductCOMEvents.GetSerialNumberDone:
            {
                try
                {
                    readData(Constants.USB_EXTERNAL_BUFFER_SIZE, Constants.SERIAL_NUMBER_SIZE, labelSerialNumber);
                }
                catch (Exception ex)
                {
                    onError(ex);
                }
                event_type = ProductCOMEvents.None;
                break;
            }

            case ProductCOMEvents.getFSVersionDone:
            {
                try
                {
                    _serialPort.Read(read_buffer, 0, Constants.Files_VERSION_SIZE + 1);
                    string ret = "";
                    Files_version = 0;
                    for (int i = 0; i < Constants.Files_VERSION_SIZE; i++)
                    {
                        ret           += read_buffer[i].ToString("X") + " ";
                        Files_version += read_buffer[i];
                    }
                    this.Dispatcher.Invoke((Action)(() =>
                        {
                            labelFilesVersion.Content = ret;
                        }));
                    if (Files_version == 0xFFFFFFFF)
                    {
                        Files_version = 0;
                    }
                    else
                    {
                        ++Files_version;
                    }
                }
                catch (Exception ex)
                {
                    onError(ex);
                }
                event_type = ProductCOMEvents.None;
                break;
            }

            case ProductCOMEvents.setFSVersionDone:
            {
                try
                {
                    readData(Constants.USB_EXTERNAL_BUFFER_SIZE, Constants.RESET_Files_REPLY_SIZE, labelSetVersion);
                }
                catch (Exception ex)
                {
                    onError(ex);
                }
                event_type = ProductCOMEvents.None;
                break;
            }

            case ProductCOMEvents.resetFilesDone:
            {
                try
                {
                    readData(Constants.USB_EXTERNAL_BUFFER_SIZE, Constants.RESET_Files_REPLY_SIZE, labelResetFiles);
                    setFSVersion[10] = (byte)((Files_version >> 24) & 0xFF);
                    setFSVersion[11] = (byte)((Files_version >> 16) & 0xFF);
                    setFSVersion[12] = (byte)((Files_version >> 8) & 0xFF);
                    setFSVersion[13] = (byte)((Files_version >> 0) & 0xFF);
                    event_type       = ProductCOMEvents.setFSVersionDone;
                    _serialPort.Write(setFSVersion, 0, Constants.USB_EXTERNAL_BUFFER_SIZE);
                }
                catch (Exception ex)
                {
                    onError(ex);
                }
                break;
            }

            case ProductCOMEvents.FeedDone:
            {
                try
                {
                    if (data_buffers_sent == 0)
                    {
                        readData(Constants.USB_EXTERNAL_BUFFER_SIZE, Constants.FEED_REPLY_SIZE, labelSendSample);

                        /* Send dummy frame to clear double buffer */
                        byte[] dummy_buffer = new byte[64];
                        _serialPort.Write(dummy_buffer, 0, Constants.USB_EXTERNAL_BUFFER_SIZE);



                        sample_file.Read(send_buffer, 0, send_data_size);

                        /* Now send to CDC */
                        _serialPort.Write(send_buffer, data_buffers_sent++, Constants.USB_EXTERNAL_BUFFER_SIZE);
                        sample_file.Close();
                    }
                    else if (send_data_size >= data_buffers_sent * Constants.USB_EXTERNAL_BUFFER_SIZE)
                    {
                        readData(Constants.USB_EXTERNAL_BUFFER_SIZE, Constants.FEED_REPLY_SIZE, labelB469);
                        _serialPort.Write(send_buffer, data_buffers_sent++, Constants.USB_EXTERNAL_BUFFER_SIZE);
                    }
                    else
                    {
                        event_type = ProductCOMEvents.None;
                        sample_file.Close();
                        this.Dispatcher.Invoke((Action)(() =>
                            {
                                textBoxSampleFile.Text = "";
                            }));
                    }
                }
                catch (Exception ex)
                {
                    onError(ex);
                }

                break;
            }

            default:
            {
                try
                {
                    readData(Constants.USB_EXTERNAL_BUFFER_SIZE, Constants.USB_EXTERNAL_BUFFER_SIZE, label1);
                }
                catch (Exception ex)
                {
                    onError(ex);
                }
                event_type = ProductCOMEvents.None;
                break;
            }
            }
        }