CfgTransfersForm.cs
上传用户:huajielb
上传日期:2022-07-29
资源大小:626k
文件大小:13k
源码类别:

驱动编程

开发平台:

Visual C++

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Runtime.InteropServices;
  7. using Jungo.wdapi_dotnet;
  8. using Jungo.plx_lib;
  9. using wdc_err = Jungo.wdapi_dotnet.WD_ERROR_CODES;
  10. using DWORD = System.UInt32;
  11. using BOOL = System.Boolean;
  12. using BYTE = System.Byte;
  13. namespace Jungo.PLX_Sample
  14. {
  15.     public class CfgTransfersForm : System.Windows.Forms.Form
  16.     {
  17.         private Exception m_excp;
  18.         private PLX_Device m_device;
  19.         private RW m_direction;
  20.         private DWORD m_dwOffset;
  21.         private DWORD m_dwBytes;
  22.         private IntPtr m_pData;
  23.         private byte[] m_buff;
  24.         private System.Windows.Forms.TextBox txtOffset;
  25.         private System.Windows.Forms.TextBox txtData;
  26.         private System.Windows.Forms.Label lblOffset;
  27.         private System.Windows.Forms.Label lblData;
  28.         private System.Windows.Forms.TextBox txtBytes;
  29.         private System.Windows.Forms.Label lblBytes;
  30.         private System.Windows.Forms.Button btExit;
  31.         private System.Windows.Forms.Label label1;
  32.         private System.Windows.Forms.TextBox txtInput;
  33.         private System.Windows.Forms.Button btWrite;
  34.         private System.Windows.Forms.Button btLog;
  35.         private System.Windows.Forms.Button btRead;
  36.         private System.Windows.Forms.Label label2;
  37.         private System.Windows.Forms.Label label3;
  38.         private System.Windows.Forms.Label label4;
  39.         private System.ComponentModel.Container components = null;
  40.         public CfgTransfersForm(PLX_Device dev)
  41.         {
  42.             InitializeComponent();
  43.             m_device = dev;                                                
  44.         }
  45.         protected override void Dispose( bool disposing )
  46.         {
  47.             if( disposing )
  48.             {
  49.                 if(components != null)
  50.                 {
  51.                     components.Dispose();
  52.                 }
  53.             }
  54.             base.Dispose( disposing );
  55.         }
  56. #region Windows Form Designer generated code
  57.         private void InitializeComponent()
  58.         {
  59.             this.txtOffset = new System.Windows.Forms.TextBox();
  60.             this.txtData = new System.Windows.Forms.TextBox();
  61.             this.lblOffset = new System.Windows.Forms.Label();
  62.             this.lblData = new System.Windows.Forms.Label();
  63.             this.btRead = new System.Windows.Forms.Button();
  64.             this.btExit = new System.Windows.Forms.Button();
  65.             this.txtBytes = new System.Windows.Forms.TextBox();
  66.             this.lblBytes = new System.Windows.Forms.Label();
  67.             this.txtInput = new System.Windows.Forms.TextBox();
  68.             this.label1 = new System.Windows.Forms.Label();
  69.             this.btWrite = new System.Windows.Forms.Button();
  70.             this.btLog = new System.Windows.Forms.Button();
  71.             this.label2 = new System.Windows.Forms.Label();
  72.             this.label3 = new System.Windows.Forms.Label();
  73.             this.label4 = new System.Windows.Forms.Label();
  74.             this.SuspendLayout();
  75.             // txtOffset
  76.             this.txtOffset.Location = new System.Drawing.Point(24, 32);
  77.             this.txtOffset.Name = "txtOffset";
  78.             this.txtOffset.Size = new System.Drawing.Size(96, 20);
  79.             this.txtOffset.TabIndex = 0;
  80.             this.txtOffset.Text = "";
  81.             // txtData
  82.             this.txtData.AutoSize = false;
  83.             this.txtData.Location = new System.Drawing.Point(16, 136);
  84.             this.txtData.Multiline = true;
  85.             this.txtData.Name = "txtData";
  86.             this.txtData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
  87.             this.txtData.Size = new System.Drawing.Size(208, 88);
  88.             this.txtData.TabIndex = 8;
  89.             this.txtData.Text = "";
  90.             // lblOffset
  91.             this.lblOffset.Location = new System.Drawing.Point(16, 8);
  92.             this.lblOffset.Name = "lblOffset";
  93.             this.lblOffset.Size = new System.Drawing.Size(88, 16);
  94.             this.lblOffset.TabIndex = 2;
  95.             this.lblOffset.Text = "Offset (hex):";
  96.             // lblData
  97.             this.lblData.Location = new System.Drawing.Point(16, 112);
  98.             this.lblData.Name = "lblData";
  99.             this.lblData.TabIndex = 3;
  100.             this.lblData.Text = "Data:";
  101.             // btRead
  102.             this.btRead.Location = new System.Drawing.Point(24, 240);
  103.             this.btRead.Name = "btRead";
  104.             this.btRead.TabIndex = 4;
  105.             this.btRead.Text = "Read";
  106.             this.btRead.Click += new System.EventHandler(this.btRead_Click);
  107.             // btExit
  108.             this.btExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  109.             this.btExit.Location = new System.Drawing.Point(232, 192);
  110.             this.btExit.Name = "btExit";
  111.             this.btExit.TabIndex = 7;
  112.             this.btExit.Text = "Exit";
  113.             // txtBytes
  114.             this.txtBytes.Location = new System.Drawing.Point(152, 32);
  115.             this.txtBytes.Name = "txtBytes";
  116.             this.txtBytes.TabIndex = 2;
  117.             this.txtBytes.Text = "";
  118.             // lblBytes
  119.             this.lblBytes.Location = new System.Drawing.Point(152, 8);
  120.             this.lblBytes.Name = "lblBytes";
  121.             this.lblBytes.Size = new System.Drawing.Size(120, 16);
  122.             this.lblBytes.TabIndex = 6;
  123.             this.lblBytes.Text = "Number of Bytes (hex):";
  124.             // txtInput
  125.             this.txtInput.Location = new System.Drawing.Point(168, 72);
  126.             this.txtInput.Name = "txtInput";
  127.             this.txtInput.Size = new System.Drawing.Size(128, 20);
  128.             this.txtInput.TabIndex = 3;
  129.             this.txtInput.Text = "";
  130.             // label1
  131.             this.label1.Location = new System.Drawing.Point(16, 72);
  132.             this.label1.Name = "label1";
  133.             this.label1.Size = new System.Drawing.Size(136, 24);
  134.             this.label1.TabIndex = 8;
  135.             this.label1.Text = "Input for Write Transactions (hex):";
  136.             // btWrite
  137.             this.btWrite.Location = new System.Drawing.Point(128, 240);
  138.             this.btWrite.Name = "btWrite";
  139.             this.btWrite.TabIndex = 5;
  140.             this.btWrite.Text = "Write";
  141.             this.btWrite.Click += new System.EventHandler(this.btWrite_Click);
  142.             // btLog
  143.             this.btLog.Location = new System.Drawing.Point(232, 152);
  144.             this.btLog.Name = "btLog";
  145.             this.btLog.TabIndex = 6;
  146.             this.btLog.Text = "Clear Log";
  147.             this.btLog.Click += new System.EventHandler(this.btLog_Click);
  148.             // label2
  149.             this.label2.Location = new System.Drawing.Point(8, 32);
  150.             this.label2.Name = "label2";
  151.             this.label2.Size = new System.Drawing.Size(16, 23);
  152.             this.label2.TabIndex = 15;
  153.             this.label2.Text = "0x";
  154.             // label3
  155.             this.label3.Location = new System.Drawing.Point(136, 32);
  156.             this.label3.Name = "label3";
  157.             this.label3.Size = new System.Drawing.Size(16, 23);
  158.             this.label3.TabIndex = 16;
  159.             this.label3.Text = "0x";
  160.             // label4
  161.             this.label4.Location = new System.Drawing.Point(152, 72);
  162.             this.label4.Name = "label4";
  163.             this.label4.Size = new System.Drawing.Size(16, 23);
  164.             this.label4.TabIndex = 17;
  165.             this.label4.Text = "0x";
  166.             // CfgTransfersForm
  167.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  168.             this.CancelButton = this.btExit;
  169.             this.ClientSize = new System.Drawing.Size(328, 277);
  170.             this.Controls.Add(this.label4);
  171.             this.Controls.Add(this.label3);
  172.             this.Controls.Add(this.label2);
  173.             this.Controls.Add(this.btLog);
  174.             this.Controls.Add(this.btWrite);
  175.             this.Controls.Add(this.txtInput);
  176.             this.Controls.Add(this.label1);
  177.             this.Controls.Add(this.txtBytes);
  178.             this.Controls.Add(this.lblBytes);
  179.             this.Controls.Add(this.btExit);
  180.             this.Controls.Add(this.btRead);
  181.             this.Controls.Add(this.lblData);
  182.             this.Controls.Add(this.lblOffset);
  183.             this.Controls.Add(this.txtData);
  184.             this.Controls.Add(this.txtOffset);
  185.             this.Name = "CfgTransfersForm";
  186.             this.StartPosition = 
  187.                 System.Windows.Forms.FormStartPosition.CenterScreen;
  188.             this.Text = "Read/Write Cfg Space By Offset";
  189.             this.ResumeLayout(false);
  190.         }
  191. #endregion
  192.         public bool GetInput()
  193.         {
  194.             DialogResult result = DialogResult.Retry;
  195.             while((result = ShowDialog()) == DialogResult.Retry);
  196.             return true;                
  197.         }
  198.         private void btClick(object sender, System.EventArgs e)
  199.         {
  200.             DialogResult = DialogResult.None;
  201.             try
  202.             {
  203.                 TranslateInput();
  204.             }
  205.             catch
  206.             {
  207.                 MessageBox.Show(m_excp.Message,"Input Entry Error",
  208.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  209.                 DialogResult = DialogResult.Retry;
  210.             }    
  211.         }
  212.         private void btRead_Click(object sender, System.EventArgs e)
  213.         {
  214.             m_direction = RW.READ;
  215.             btClick(sender, e);                        
  216.         }
  217.         private void btWrite_Click(object sender, System.EventArgs e)
  218.         {
  219.             m_direction = RW.WRITE;
  220.             btClick(sender, e);
  221.         }        
  222.         private void TranslateInput()
  223.         {
  224.             DWORD dwStatus;
  225.             BOOL bIsRead = m_direction == RW.READ;
  226.             m_excp = new Exception("Enter the offset. Entered value should " +
  227.                 "be a hex number");
  228.             m_dwOffset = (DWORD)Convert.ToInt32(txtOffset.Text,16);
  229.             m_excp = new Exception("Enter the number of bytes. " + 
  230.                 "Entered value should be a hex number");
  231.             m_dwBytes = (DWORD)Convert.ToInt32(txtBytes.Text,16);
  232.             m_pData = Marshal.AllocHGlobal((int)m_dwBytes);
  233.             if(m_pData == IntPtr.Zero)
  234.                 return;                        
  235.             m_buff = new byte[m_dwBytes];
  236.             if(!bIsRead)
  237.             {
  238.                 if(txtInput.Text == "")
  239.                 {
  240.                     m_excp = new Exception("You must enter the data to be " +
  241.                         "written");
  242.                     throw m_excp;
  243.                 }
  244.                 string str = diag_lib.PadBuffer(txtInput.Text, 
  245.                     (DWORD)txtInput.Text.Length,(DWORD)2*m_dwBytes);
  246.                 m_excp = new Exception("The data you've entered is invalid. "
  247.                     + "please try again (hex)");
  248.                 for(int i=0; i<m_dwBytes; ++i)
  249.                     m_buff[i] = Convert.ToByte(str.Substring(2*i,2),16);
  250.                 Marshal.Copy(m_buff, 0 , m_pData, (int)m_dwBytes);
  251.                 dwStatus = wdc_lib_decl.WDC_PciWriteCfg(m_device.Handle, 
  252.                     m_dwOffset, m_pData, m_dwBytes);                                 
  253.             }
  254.             else //READ
  255.             {
  256.                 dwStatus = wdc_lib_decl.WDC_PciReadCfg(m_device.Handle, 
  257.                     m_dwOffset, m_pData, m_dwBytes);
  258.                 if(dwStatus == (DWORD)wdc_err.WD_STATUS_SUCCESS)
  259.                     Marshal.Copy(m_pData, m_buff, 0, (int)m_dwBytes);
  260.             }
  261.             TraceLog(bIsRead, (wdc_err)dwStatus);
  262.         }
  263.         private void btLog_Click(object sender, System.EventArgs e)
  264.         {
  265.             txtData.Clear();
  266.         }
  267.         private void TraceLog(BOOL bIsRead, wdc_err status)
  268.         {
  269.             string sData = "";
  270.             string sInfo = "";
  271.             if(status == wdc_err.WD_STATUS_SUCCESS)
  272.             {
  273.                 sData = (bIsRead? "R: " : "W: ") +
  274.                     diag_lib.DisplayHexBuffer(m_buff, m_dwBytes);
  275.                 sInfo = (bIsRead? " from " : " to ") + "offset " +
  276.                     m_dwOffset.ToString("X") + "(" + m_device.ToString(false)
  277.                     + ")";
  278.                 Log.TraceLog("CfgTransfersForm: " + sData + sInfo);
  279.             }
  280.             else
  281.             {
  282.                 sData = "failed to " + (bIsRead? "read from" : "write to") +
  283.                     " offset " + m_dwOffset.ToString("X") + " status 0x" +
  284.                     status.ToString("X") + ": " + utils.Stat2Str((DWORD)status);
  285.                 sInfo = "(" + m_device.ToString(false) + ")";
  286.                 Log.ErrLog("CfgTransfersForm: " + sData + sInfo);
  287.             }
  288.             txtData.Text += sData + Environment.NewLine;            
  289.         }
  290.     }
  291. }