⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Cypress Suite USB 3.4.6
💻 CS
📖 第 1 页 / 共 5 页
字号:
            int i = 1;
            foreach (string s in hexVals)
                if (i <= curHidReport.RptByteLen)
                    curHidReport.DataBuf[i++] = (byte)Convert.ToInt32(s, 16);

        }

        /* Summary
            Event handler for Transfer File button
        */
        private void FileXferBtn_Click(object sender, EventArgs e)
        {
            if (curHidReport != null)
            {
                DoHidXfer(sender, e);
                return;
            }

            //Code added to transfer small file: no buffering, only one transfer
            if (curEndpt == null)
            {
                MessageBox.Show("Select <bulk> <iso> <int> endpoint enabled in the device tree.", "No endpoint selected");
                return;
            }

            long flen = 0;

            string fname = "";

            if (bRecording && (script_stream != null))
            {
                Xaction.ConfigNum = fx2.Config;
                Xaction.IntfcNum = 0;
                Xaction.AltIntfc = fx2.AltIntfc;
                Xaction.EndPtAddr = curEndpt.Address;
                // Set the Tag (file xfer) to 1 only if reading data from the device
                Xaction.Tag = curEndpt.bIn ? (byte)1 : (byte)0;
            }

            bool success = false;

            switch (curEndpt.Attributes)
            {
                case 0:
                    CyControlEndPoint ctrlEpt = curEndpt as CyControlEndPoint;

                    if (TargetBox.Text.Equals("Device")) ctrlEpt.Target = CyConst.TGT_DEVICE;
                    else if (TargetBox.Text.Equals("Interface")) ctrlEpt.Target = CyConst.TGT_INTFC;
                    else if (TargetBox.Text.Equals("Endpoint")) ctrlEpt.Target = CyConst.TGT_ENDPT;
                    else if (TargetBox.Text.Equals("Other")) ctrlEpt.Target = CyConst.TGT_OTHER;

                    if (ReqTypeBox.Text.Equals("Standard")) ctrlEpt.ReqType = CyConst.REQ_STD;
                    else if (ReqTypeBox.Text.Equals("Class")) ctrlEpt.ReqType = CyConst.REQ_CLASS;
                    else if (ReqTypeBox.Text.Equals("Vendor")) ctrlEpt.ReqType = CyConst.REQ_VENDOR;

                    ctrlEpt.Direction = DirectionBox.Text.Equals("In") ? CyConst.DIR_FROM_DEVICE : CyConst.DIR_TO_DEVICE;

                    try
                    {
                        ctrlEpt.ReqCode = (byte)Convert.ToInt16(ReqCodeBox.Text, 16); //(byte)Util.HexToInt(ReqCodeBox.Text);
                        ctrlEpt.Value = (ushort)Convert.ToInt16(wValueBox.Text, 16); //(ushort)Util.HexToInt(wValueBox.Text);
                        ctrlEpt.Index = (ushort)Convert.ToInt16(wIndexBox.Text, 16); //(ushort)Util.HexToInt(wIndexBox.Text);
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message, "Input Error");
                        return;
                    }

                    if (ctrlEpt.Direction == CyConst.DIR_TO_DEVICE)
                    {
                        string tmpFilter = FOpenDialog.Filter;
                        string title = FOpenDialog.Title;
                        FOpenDialog.Title = "Select the file to send";
                        FOpenDialog.Filter = "All files (*.*) | *.*";

                        if (FOpenDialog.ShowDialog() == DialogResult.OK)
                        {
                            fname = FOpenDialog.FileName;
                            StatLabel.Text = "File selected.... " + FOpenDialog.FileName;
                            Refresh();
                        }
                        else
                        {
                            FOpenDialog.Filter = tmpFilter;
                            FOpenDialog.Title = title;
                            return;
                        }

                        FOpenDialog.FileName = "";
                        FOpenDialog.Filter = tmpFilter;
                        FOpenDialog.Title = title;

                        FileStream file = new FileStream(fname, FileMode.Open, FileAccess.Read);
                        flen = file.Length;
                        //file_bytes = (int)flen;

                        file_bytes = Convert.ToInt32(NumBytesBox.Text);
                        file_buffer = new byte[file_bytes];

                        file.Read(file_buffer, 0, file_bytes);
                        file.Close();

                        curEndpt.XferSize = Convert.ToInt32(NumBytesBox.Text);
                        success = ctrlEpt.XferData(ref file_buffer, ref file_bytes);

                        if (bRecording && (script_stream != null))
                        {
                            Xaction.Tag = 0;

                            Xaction.bReqType = (byte)(ctrlEpt.Direction | ctrlEpt.ReqType | ctrlEpt.Target);
                            Xaction.CtlReqCode = ctrlEpt.ReqCode;
                            Xaction.wValue = ctrlEpt.Value;
                            Xaction.wIndex = ctrlEpt.Index;

                            Xaction.DataLen = (uint)file_bytes;
                            Xaction.Timeout = ctrlEpt.TimeOut / 1000;
                            Xaction.RecordSize = (uint)file_bytes + TTransaction.TotalHeaderSize;

                            //Write xaction and buffer
                            Xaction.WriteToStream(script_stream);
                            Xaction.WriteFromBuffer(script_stream, ref file_buffer, ref file_bytes);
                        }

                        BuildDataCaption();
                        OutputBox.Text += dataCaption;
                        DisplayXferData(file_buffer, file_bytes, success);
                    }
                    else
                    {
                        file_bytes = Convert.ToInt32(NumBytesBox.Text);
                        byte[] To_file = new byte[file_bytes];

                        curEndpt.XferSize = Convert.ToInt32(NumBytesBox.Text);
                        success = ctrlEpt.XferData(ref To_file, ref file_bytes);
                        if (success)
                        {
                            string filename;

                            string tmpFilter = FSave.Filter;
                            string title = FSave.Title;
                            FSave.Title = "Save Data to file:";
                            FSave.Filter = "All files (*.*) | *.*";

                            if (FSave.ShowDialog() == DialogResult.OK)
                            {
                                filename = FSave.FileName;
                                Refresh();

                                FSave.Title = title;
                                FSave.Filter = tmpFilter;

                                try
                                {
                                    FileStream file = new FileStream(filename, FileMode.Create);
                                    file.Write(To_file, 0, file_bytes);
                                    file.Close();
                                }
                                catch (Exception exc)
                                {
                                    MessageBox.Show(exc.Message, "Check the File Attributes");
                                }
                            }
                            else
                            {
                                FSave.Title = title;
                                FSave.Filter = tmpFilter;
                                return;
                            }

                            FSave.FileName = "";
                            FSave.Title = title;
                            FSave.Filter = tmpFilter;
                        }

                        if (bRecording && (script_stream != null))
                        {
                            Xaction.Tag = 1;

                            Xaction.bReqType = (byte)(ctrlEpt.Direction | ctrlEpt.ReqType | ctrlEpt.Target);
                            Xaction.CtlReqCode = ctrlEpt.ReqCode;
                            Xaction.wValue = ctrlEpt.Value;
                            Xaction.wIndex = ctrlEpt.Index;

                            Xaction.DataLen = (uint)file_bytes;
                            Xaction.RecordSize = TTransaction.TotalHeaderSize;  // Don't save the data in script

                            //Write xaction
                            Xaction.WriteToStream(script_stream);
                        }

                        BuildDataCaption();
                        OutputBox.Text += dataCaption;
                        DisplayXferData(To_file, file_bytes, success);
                    }
                    break;
                default:
                    if (!curEndpt.bIn)
                    {
                        string tmpFilter = FOpenDialog.Filter;
                        string title = FOpenDialog.Title;
                        FOpenDialog.Title = "Select the file to send";
                        FOpenDialog.Filter = "All files (*.*) | *.*";

                        if (FOpenDialog.ShowDialog() == DialogResult.OK)
                        {
                            fname = FOpenDialog.FileName;
                            StatLabel.Text = "File selected.... " + FOpenDialog.FileName;
                            Refresh();
                        }
                        else
                        {
                            FOpenDialog.Filter = tmpFilter;
                            FOpenDialog.Title = title;
                            return;
                        }

                        FOpenDialog.FileName = "";
                        FOpenDialog.Filter = tmpFilter;
                        FOpenDialog.Title = title;

                        FileStream file = new FileStream(fname, FileMode.Open, FileAccess.Read);
                        flen = file.Length;
                        file_bytes = (int)flen;

                        //file_bytes = Convert.ToInt32(NumBytesBox.Text); // File-OUT transfer it will take file lenght as a number of bytes to transfer.
                        file_buffer = new byte[file_bytes];

                        file.Read(file_buffer, 0, file_bytes);
                        file.Close();

                        //curEndpt.XferSize = Convert.ToInt32(NumBytesBox.Text);//
                        curEndpt.XferSize = file_bytes;
                        success = curEndpt.XferData(ref file_buffer, ref file_bytes);

                        if (bRecording && (script_stream != null))
                        {
                            Xaction.DataLen = (uint)file_bytes;
                            Xaction.Timeout = curEndpt.TimeOut / 1000;
                            Xaction.RecordSize = (uint)file_bytes + TTransaction.TotalHeaderSize;

                            //Write xaction and buffer
                            Xaction.WriteToStream(script_stream);
                            Xaction.WriteFromBuffer(script_stream, ref file_buffer, ref file_bytes);
                        }

                        BuildDataCaption();
                        OutputBox.Text += dataCaption;
                        DisplayXferData(file_buffer, file_bytes, success);
                    }
                    else
                    {
                        if (NumBytesBox.Text == "")
                        {
                            MessageBox.Show("Please enter Number of Bytes to receive in Bytes to Transfer box.", "Invalid Input");
                            return;
                        }
                        
                        file_bytes = Convert.ToInt32(NumBytesBox.Text);
                        byte[] To_file = new byte[file_bytes];

                        curEndpt.XferSize = Convert.ToInt32(NumBytesBox.Text);
                        success = curEndpt.XferData(ref To_file, ref file_bytes);
                        if (success)
                        {
                            string filename;

                            string tmpFilter = FSave.Filter;
                            string title = FSave.Title;
                            FSave.Title = "Save Data to file:";
                            FSave.Filter = "All files (*.*) | *.*";

                            if (FSave.ShowDialog() == DialogResult.OK)
                            {
                                filename = FSave.FileName;
                                Refresh();

                                FSave.Title = title;
                                FSave.Filter = tmpFilter;

                                try
                                {
                                    FileStream file = new FileStream(filename, FileMode.Create);
                                    file.Write(To_file, 0, file_bytes);
                                    file.Close();
                                }
                                catch (Exception exc)
                                {
                                    MessageBox.Show(exc.Message, "Check the File Attributes");
                                }
                            }
                            else
                            {
                                FSave.Title = title;
                                FSave.Filter = tmpFilter;
                                return;
                            }

                            FSave.FileName = "";
                            FSave.Title = title;
                            FSave.Filter = tmpFilter;
                        }

                        if (bRecording && (script_stream != null))
                        {
                            Xaction.DataLen = (uint)file_bytes;
                            Xaction.RecordSize = TTransaction.TotalHeaderSize;  // Don't save the data in script

                            //Write xaction
                            Xaction.WriteToStream(script_stream);
                        }

                        BuildDataCaption();
                        OutputBox.Text += dataCaption;
                        DisplayXferData(To_file, file_bytes, success);
                    }
                    break;
            }

        }

        /* Summary
            Event handler for Transfer Data button
        */
        private void DataXferBtn_Click(object sender, EventArgs e)
        {
            if (curHidReport != null)
            {
                DoHidXfer(sender, e);
                return;
            }

            if (curHidDev != null)
            {
                MessageBox.Show("Select a HID feature, input or output in the device tree.", "No report selected");
                return;
            }

            if (curEndpt == null)
            {
                MessageBox.Show("Select <bulk> <iso> <int> endpoint enabled in the device tree.", "No endpoint selected");
                return;
            }

            int bytes = 0;

            try
            {
                bytes = Convert.ToInt32(NumBytesBox.Text);
            }
            catch (Exception exc)
            {
                if (bytes < 1)
                {
                    //Just to remove warning
                    exc.ToString();

                    MessageBox.Show("Enter a valid number of bytes to transfer.", "Invalid Byte Count");
                    return;
                }
            }

            byte[] buffer = new byte[bytes];
            bool bXferCompleted = false;

            // Setting control endpt direction needs to occur before BuildDataCaption call
            CyControlEndPoint ctrlEpt = curEndpt as CyControlEndPoint;
            if (ctrlEpt != null)
                ctrlEpt.Direction = DirectionBox.Text.Equals("In") ? CyConst.DIR_FROM_DEVICE : CyConst.DIR_TO_DEVICE;

            // Stuff the output buffer

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -