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

📄 form1.cs

📁 Cypress Suite USB 3.4.6
💻 CS
📖 第 1 页 / 共 5 页
字号:
            if (!curEndpt.bIn)
            {
                string[] hexTokens = XferDataBox.Text.Split(' ');

                int i = 0;

                //foreach (string tok in hexTokens)
                for (int j = 0; j < bytes; j++)
                {
                    string tok;

                    try
                    {
                        tok = hexTokens[j];
                    }
                    catch
                    {
                        tok = "";
                    }
 
                    if (tok.Length > 0)
                    {
                        try
                        {
                            buffer[i++] = (byte)Convert.ToInt32(tok, 16);
                        }
                        catch (Exception exc)
                        {
                            MessageBox.Show(exc.Message, "Input Error");
                            return;
                        }
                    }
                }
            }

            BuildDataCaption();
            OutputBox.Text += dataCaption;
            OutputBox.SelectionStart = OutputBox.Text.Length;
            OutputBox.ScrollToCaret();

            curEndpt.TimeOut = 2000;

            if (ctrlEpt != null)
            {
                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;
                }

                bXferCompleted = ctrlEpt.XferData(ref buffer, ref bytes);


                if (bRecording && (script_stream != null))
                {
                    Xaction.ConfigNum = fx2.Config;
                    Xaction.IntfcNum = 0;
                    Xaction.AltIntfc = fx2.AltIntfc;
                    Xaction.EndPtAddr = ctrlEpt.Address;
                    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)bytes;
                    Xaction.Timeout = ctrlEpt.TimeOut / 1000;
                    Xaction.RecordSize = (uint)bytes + TTransaction.TotalHeaderSize;

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

            bool IsPkt = IsPacket.Checked ? true : false;

            CyBulkEndPoint bulkEpt = curEndpt as CyBulkEndPoint;
            if (bulkEpt != null)
            {
                bXferCompleted = bulkEpt.XferData(ref buffer, ref bytes, IsPkt);
                CheckForScripting(ref buffer, ref bytes);
            }

            CyIsocEndPoint isocEpt = curEndpt as CyIsocEndPoint;
            if (isocEpt != null)
            {
                isocEpt.XferSize = Convert.ToInt32(NumBytesBox.Text);

                int pkts = bytes / isocEpt.MaxPktSize;
                if ((bytes % isocEpt.MaxPktSize) > 0) pkts++;
			    ISO_PKT_INFO[] Iskpt = new ISO_PKT_INFO[pkts];                 
                bXferCompleted = isocEpt.XferData(ref buffer, ref bytes, ref Iskpt);
                if (bXferCompleted)
                {
                    int MainBufOffset = 0;
                    int tmpBufOffset = 0;
                    byte[] tmpbuf = new byte[bytes];
                    // Check all packets and  if Iso in/out packet is not succeeded then don't update the buffer.
                    for (int i = 0; i < pkts; i++)
                    {
                        if (Iskpt[i].Status != 0)
                        {
                            //updated the buffer based on the status of the packets
                            //skip that buffer
                        }
                        else
                        {
                            for (int j = 0; j < Iskpt[i].Length; j++)
                            {
                                tmpbuf[tmpBufOffset] = buffer[MainBufOffset + j]; // get the received/transfered data in the temparary buffer
                                tmpBufOffset++;
                            }
                        }
                        MainBufOffset += isocEpt.MaxPktSize;
                    }
                    // Now copy the temparary buffer to main buffer to display
                    for (int x = 0; x < tmpBufOffset; x++)
                    {
                        buffer[x] = tmpbuf[x]; // Updated the main buffer with the whatever data has been received / transfered.
                    }
                }
                //bXferCompleted = isocEpt.XferData(ref buffer, ref bytes);                
                CheckForScripting(ref buffer, ref bytes);
            }

            CyInterruptEndPoint intEpt = curEndpt as CyInterruptEndPoint;
            if (intEpt != null)
            {
                bXferCompleted = intEpt.XferData(ref buffer, ref bytes, IsPkt);
                CheckForScripting(ref buffer, ref bytes);
            }

            DisplayXferData(buffer, bytes, bXferCompleted);
        }

        private void CheckForScripting(ref byte[] buffer, ref int bytes)
        {
            if (bRecording && (script_stream != null))
            {
                Xaction.ConfigNum = fx2.Config;
                Xaction.IntfcNum = 0;
                Xaction.AltIntfc = fx2.AltIntfc;
                Xaction.EndPtAddr = curEndpt.Address;

                Xaction.Tag = 0;
                Xaction.DataLen = (uint)bytes;
                Xaction.Timeout = curEndpt.TimeOut / 1000;
                Xaction.RecordSize = TTransaction.TotalHeaderSize + (uint)bytes;

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

        /* Summary
            Creates captions to display in the output box
        */
        private void BuildDataCaption()
        {
            StringBuilder dataStr = new StringBuilder();

            switch (curEndpt.Attributes)
            {
                case 0: dataStr.Append("CONTROL ");
                    break;
                case 1: dataStr.Append("ISOC ");
                    break;
                case 2: dataStr.Append("BULK ");
                    break;
                case 3: dataStr.Append("INTERRUPT ");
                    break;
            }

            if (curEndpt.bIn)
                dataStr.Append("IN transfer ");
            else
                dataStr.Append("OUT transfer ");

            dataCaption = dataStr.ToString();
        }

        /* Summary
            Just to print the values in formatted order in output box
        */
        private void DisplayXferData(byte[] buf, int bCnt, bool TransferStatus)
        {
            StringBuilder dataStr = new StringBuilder();

            string resultStr = "";
            if (bCnt > 0)
            {
                if (TransferStatus)
                {
                    resultStr = dataCaption + "completed\r\n";

                    for (int i = 0; i < bCnt; i++)
                    {
                        if ((i % 16) == 0) dataStr.Append(string.Format("\r\n{0:X4}", i));
                        dataStr.Append(string.Format(" {0:X2}", buf[i]));
                    }
                }
                else
                {
                    resultStr = dataCaption + "failed with Error Code:" + curEndpt.LastError + "\r\n";
                }

                OutputBox.Text += dataStr.ToString() + "\r\n" + resultStr + "\r\n";
            }
            else
            {
                if (TransferStatus)
                {
                    resultStr = "Zero-length data transfer completed\r\n";
                }
                else
                {
                    //if (buf.Length > 0)
                    //{
                    //    for (int i = 0; i < buf.Length; i++)
                    //    {
                    //        if ((i % 16) == 0) dataStr.Append(string.Format("\r\n{0:X4}", i));
                    //        dataStr.Append(string.Format(" {0:X2}", buf[i]));
                    //    }

                    //    resultStr = "\r\nPartial data Transferred\r\n";
                    //}

                    resultStr = dataCaption + "failed with Error Code:" + curEndpt.LastError + "\r\n";

                }

                OutputBox.Text += dataStr.ToString() + "\r\n" + resultStr + "\r\n";
            }



            OutputBox.SelectionStart = OutputBox.Text.Length;
            OutputBox.ScrollToCaret();
        }

        /* Summary
            To resize the Data Transfers TabPage in case Control endpoint is selected
        */
        private void Form1_Resize(object sender, EventArgs e)
        {
            bool bControlEpt = ((curEndpt != null) && (curEndpt.Attributes == 0));

            DirectionBox.Visible = bControlEpt;
            DirectionLabel.Visible = bControlEpt;
            ReqTypeBox.Visible = bControlEpt;
            ReqTypeLabel.Visible = bControlEpt;
            TargetBox.Visible = bControlEpt;
            TargetLabel.Visible = bControlEpt;
            ReqCodeBox.Visible = bControlEpt;
            ReqCodeLabel.Visible = bControlEpt;
            wValueBox.Visible = bControlEpt;
            wValueLabel.Visible = bControlEpt;
            wIndexBox.Visible = bControlEpt;
            wIndexLabel.Visible = bControlEpt;

            int oBoxAdj = bControlEpt ? 200 : 125;

            OutputBox.SetBounds(0, 0, 5, XferTab.Size.Height - oBoxAdj);

            if (Sync_Form_Resize == 1)
            {
                if (curCyUsbDev != null)
                {
                    if (bControlEpt)
                    {
                        this.XferTab.Enabled = true;
                    }
                    else if (curCyUsbDev.IntfcCount == 1 && curCyUsbDev.ConfigCount == 1)
                    {
                        this.XferTab.Enabled = true;
                    }
                    else
                    {
                        this.XferTab.Enabled = false;
                    }
                }
                else if (curHidDev != null)
                {
                    this.XferTab.Enabled = true;
                }

                Sync_Form_Resize = 0;
            }
        }

        /* Summary
            Event handler for the XferTextBox
        */
        private void XferTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            string txt = XferTextBox.Text;
            StringBuilder hexData = new StringBuilder();

            foreach (char c in txt)
            {
                hexData.Append(string.Format("{0:X2} ", Convert.ToByte(c)));
            }

            XferDataBox.Text = hexData.ToString();
            NumBytesBox.Text = XferTextBox.Text.Length.ToString();

            HidLimitXferLen();
        }

        /* Summary
            Event handler for the XferDataBox
        */
        private void XferDataBox_KeyUp(object sender, KeyEventArgs e)
        {
            string hexData = XferDataBox.Text;
            string[] hexTokens = hexData.Split(' ');

            StringBuilder txt = new StringBuilder();

            int nToks = 0;

            foreach (string tok in hexTokens)
                if (tok.Length > 0)
                {
                    nToks++;

                    try
                    {
                        int n = Convert.ToInt32(tok, 16);
                        txt.Append(Convert.ToChar(n));
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message, "Input Error");
                        return;
                    }
                }

            XferTextBox.Text = txt.ToString();
            NumBytesBox.Text = nToks.ToString();

            HidLimitXferLen();
        }

        private void HidLimitXferLen()
        {
            TreeNode selNode = DeviceTreeView.SelectedNode;

            if (curHidDev != null) NumBytesBox.Text = curHidReport.RptByteLen.ToString();
        }

        /* Summary

⌨️ 快捷键说明

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