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

📄 form1.cs

📁 Cypress Suite USB 3.4.6
💻 CS
📖 第 1 页 / 共 5 页
字号:
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using CyUSB;

namespace CyControl
{
    public partial class Form1 : Form
    {
        CyFX2Device fx2;
        USBDeviceList usbDevices;
        CyUSBEndPoint curEndpt;

        CyUSBDevice curCyUsbDev;
        CyHidDevice curHidDev;
        CyHidReport curHidReport;

        string dataCaption;
        string scriptfile;
        string playscriptfile;
        string monitorfile;
        string fname;
        string Datastring;
        static int file_bytes;
        bool bRecording;
        byte[] file_buffer;

        ArrayList list;
        ArrayList list1;

        TTransaction Xaction;
        FileStream stream;
        StreamWriter sw;
        FileStream script_stream;

        byte Reqcode;
        ushort wvalue;
        ushort windex;

        ushort Resetreg;
        ushort Maxaddr;
        int Sync_Form_Resize = 0;
        long Max_Ctlxfer_size;

        /* Summary
            Main entry to the application through Constructor
        */
        public Form1()
        {
            Initialize();

            //Initializes form resources
            InitializeComponent();

            //Set the customer class GUID(vendor specific) and Driver Guid(vendor specific)
            //CyConst.SetCustomerGUID("{CDBF8987-75F1-468e-8217-97197F88F773}", "{C955D74D-0430-44f2-B120-276D94492D2D}");
            CyConst.SetClassGuid("{CDBF8987-75F1-468e-8217-97197F88F773}");

            // This call instantiates usbDevices per the driver classes selected
            CyUSBDeviceBox_CheckedChanged(this, null);

            Sync_Form_Resize = 1;
            Form1_Resize(this, null);


        }

        /* Summary
            Initialize global variables defined
        */
        private void Initialize()
        {
            scriptfile = "";
            playscriptfile = "";
            monitorfile = "";
            Resetreg = 0xE600;
            Maxaddr = 0x4000;
            Max_Ctlxfer_size = 0x1000;
            bRecording = false;
            Xaction = new TTransaction();
            list = new ArrayList();
            list1 = new ArrayList();

            curEndpt = null;
            curCyUsbDev = null;
            curHidDev = null;
            curHidReport = null;

        }

        /* Summary
            Called while closing the form for Garbage collection
        */
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (usbDevices != null)
            {
                usbDevices.DeviceRemoved -= usbDevices_DeviceRemoved;
                usbDevices.DeviceAttached -= usbDevices_DeviceAttached;
                usbDevices.Dispose();
            }
        }

        /* Summary
            Refreshes the Device tree of control center to update the recent changes like device removal, new device
        */
        private void RefreshDeviceTree()
        {
            DeviceTreeView.Nodes.Clear();
            DescText.Text = "";

            foreach (USBDevice dev in usbDevices)
                DeviceTreeView.Nodes.Add(dev.Tree);

        }

        /* Summary
            Any selection changes in the tree view will trigger this function "at start the first device is always selected leading into this function"
        */
        private void DeviceTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            XferTextBox.Text = "";
            XferDataBox.Text = "";

            TreeNode selNode = DeviceTreeView.SelectedNode;
            string nodeText = selNode.Text;

            CyUSBInterfaceContainer curIntfcContainer = selNode.Tag as CyUSBInterfaceContainer;
            CyUSBInterface curIntfc = selNode.Tag as CyUSBInterface;
            CyUSBConfig curConfig = selNode.Tag as CyUSBConfig;

            curEndpt = selNode.Tag as CyUSBEndPoint;
            curCyUsbDev = selNode.Tag as CyUSBDevice;

            curHidDev = null;
            curHidReport = null;

            if (curConfig != null)
            {
                curCyUsbDev = selNode.Parent.Tag as CyUSBDevice;
            }
            else if (curIntfcContainer != null)
            {
                curCyUsbDev = selNode.Parent.Parent.Tag as CyUSBDevice;
            }
            else if (curIntfc != null)
            {
                curCyUsbDev = selNode.Parent.Parent.Parent.Tag as CyUSBDevice;
                curCyUsbDev.AltIntfc = curIntfc.bAlternateSetting;
                if (curCyUsbDev.AltIntfc != curIntfc.bAlternateSetting)
                {
                    //set interface failed
                    MessageBox.Show("Please select another alternate interface.", "Select Alternate Interface Failed");
                }
            }
            else if (curEndpt != null)
            {
                int minXfer = curEndpt.MaxPktSize;
                if (curEndpt.Attributes == 1) minXfer *= 8;
                NumBytesBox.Text = minXfer.ToString();

                // Set the AltSetting
                if (curEndpt.Address != 0) // Only if we're not on the Control Endpoint
                {
                    curCyUsbDev = selNode.Parent.Parent.Parent.Parent.Tag as CyUSBDevice;
                    curIntfc = selNode.Parent.Tag as CyUSBInterface;
                    curCyUsbDev.AltIntfc = curIntfc.bAlternateSetting;
                    if (curCyUsbDev.AltIntfc != curIntfc.bAlternateSetting)
                    {                        
                        MessageBox.Show("Please select another alternate interface.", "Select Alternate Interface Failed");
                        return;
                    }
                }
                else
                {
                    curCyUsbDev = selNode.Parent.Parent.Tag as CyUSBDevice;
                }
            }
            else if ((selNode.Tag is CyHidButton) || (selNode.Tag is CyHidValue))
            {
                curHidDev = selNode.Parent.Parent.Tag as CyHidDevice;
                curHidReport = selNode.Parent.Tag as CyHidReport;

                NumBytesBox.Text = curHidReport.RptByteLen.ToString();
                nodeText = selNode.Parent.Text;
            }
            else if (selNode.Tag is CyHidReport)
            {
                curHidDev = selNode.Parent.Tag as CyHidDevice;
                curHidReport = selNode.Tag as CyHidReport;

                NumBytesBox.Text = curHidReport.RptByteLen.ToString();
            }
            else if (selNode.Tag is CyHidDevice)
                curHidDev = selNode.Tag as CyHidDevice;

            ConfigDataXferBtn(nodeText);

            DescText.Text = selNode.Tag.ToString();

            Sync_Form_Resize = 1;
            Form1_Resize(sender, null);
        }

        /* Summary
            Transfer file button should be visible only for certain conditions which is done here.
        */
        private void ConfigDataXferBtn(string nodeTxt)
        {
            FileXferBtn.Visible = false;
            FileXferBtn.Enabled = false;

            XferTextBox.Enabled = true;
            XferDataBox.Enabled = true;

            //DataXferBtn.Enabled = true;

            //Changed here
            fx2 = Fx2DeviceSelected_forusage();
            if (fx2 != null)
            {
                FileXferBtn.Visible = true;
                FileXferBtn.Enabled = true;
            }
            //
            if (nodeTxt.Contains("Feature"))
            {
                DataXferBtn.Text = "Get Feature";
                FileXferBtn.Text = "Set Feature";
                FileXferBtn.Visible = true;
                FileXferBtn.Enabled = true;
            }
            else if (nodeTxt.Contains("Input"))
            {
                DataXferBtn.Text = "Get Input";

                string os = GetOSName();
                
                //GetInput is only supported under WindowsXP and newer.
                if (!((os == "Windows XP") || (os == "Windows Vista")))
                    DataXferBtn.Enabled = false;
            }
            else if (nodeTxt.Contains("Output"))
                DataXferBtn.Text = "Set Output";
            else
            {
                if (curEndpt != null)
                {
                    if (curEndpt.Attributes != 0)
                    {
                        if (curEndpt.bIn)
                        {
                            DataXferBtn.Text = "Transfer Data-IN";
                            FileXferBtn.Text = "Transfer File-IN";
                        }
                        else
                        {
                            DataXferBtn.Text = "Transfer Data-OUT";
                            FileXferBtn.Text = "Transfer File-OUT";
                        }
                    }
                    else
                    {
                        DataXferBtn.Text = "Transfer Data";
                        FileXferBtn.Text = "Transfer File";
                    }
                }
                else
                {
                    DataXferBtn.Text = "Transfer Data";
                    FileXferBtn.Text = "Transfer File";
                }

            }
            if (curHidDev != null)
            {
                TreeNode selNode = DeviceTreeView.SelectedNode;
                DataXferBtn.Enabled = curHidDev.RwAccessible;

                string os = GetOSName();
                XferTextBox.Enabled = false;
                XferDataBox.Enabled = false;
                //GetInput is only supported under WindowsXP and newer.
                if (!((os == "Windows XP") || (os == "Windows Vista")))
                 DataXferBtn.Enabled = false;
            }
            else
                DataXferBtn.Enabled = true;

        }

        /* Summary
            Event handler of About Menu Item
        */
        private void AboutMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Util.Assemblies + "\nCopyright (c) Cypress Semiconductor 2011", Text);
        }

        /* Summary
            For HID devices called from transfer data button and transfer file button
        */
        private void DoHidXfer(object sender, EventArgs e)
        {
            bool bResult = false;

            if (DataXferBtn.Text.Contains("Feature"))
            {
                if (sender == FileXferBtn)
                {
                    dataCaption = "Set feature ";
                    OutputBox.Text += dataCaption;

                    LoadHidReport();
                    bResult = curHidDev.SetFeature(curHidReport.ID);
                }
                else
                {
                    dataCaption = "Get feature ";
                    OutputBox.Text += dataCaption;
                    bResult = curHidDev.GetFeature(curHidReport.ID);
                }
            }

            else if (DataXferBtn.Text.Contains("Input"))
            {
                dataCaption = "Get input ";
                OutputBox.Text += dataCaption;
                bResult = curHidDev.GetInput(curHidReport.ID);
            }

            else if (DataXferBtn.Text.Contains("Output"))
            {
                dataCaption = "Set output ";
                OutputBox.Text += dataCaption;

                LoadHidReport();
                bResult = curHidDev.SetOutput(curHidReport.ID);
            }

            if (bResult)
                DisplayXferData(curHidReport.DataBuf, curHidReport.RptByteLen, true);
            else
            {
                OutputBox.Text += string.Format("\r\n{0}failed\r\n\r\n", dataCaption);
                OutputBox.SelectionStart = OutputBox.Text.Length;
                OutputBox.ScrollToCaret();
            }
        }

        /* Summary
            Called from DoHidXfer
        */
        private void LoadHidReport()
        {
            if (curHidReport == null) return;

            curHidReport.Clear();

            // Load the report buffer with the hex bytes in XferDataBox
            string[] separators = { " " };
            string[] hexVals = XferDataBox.Text.Split(separators, StringSplitOptions.RemoveEmptyEntries);

⌨️ 快捷键说明

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