Untitled

 avatar
unknown
plain_text
4 months ago
10 kB
2
Indexable

using AnoleAPI;
using System;
using System.IO;
using System.Management;
using System.Security.Policy;
using System.Text;
using System.Windows.Forms;

namespace EvalBoard
{
    public partial class Form1 : Form
    {
        // Log file Path
        private string logDirectory = "D:\\Software\\Software\\WINIC\\AnoleTestAPP-BMD\\AnoleTestAPP-BMD\\EvalBoard\\LOG";
        private string logFilePath;

        // Declaration Of the input variables
        public int Offset_write_input;
        public int data_write_input;
        public int offset_read_input;
        public int data_read_output;

        public Form1()
        {
            InitializeComponent();
            CreateLog();
        }

        public void CreateLog()
        {
            try
            {
                Directory.CreateDirectory(logDirectory);
                logFilePath = Path.Combine(logDirectory, $"Log_{DateTime.Now:yyyyMMdd_HHmmss}.txt");

                using (StreamWriter logFile = new StreamWriter(logFilePath, true))
                {
                    logFile.WriteLine("Log created at: " + DateTime.Now);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error creating log file: " + ex.Message);
            }
        }

        public void WriteToLog(string message)
        {
            try
            {
                using (StreamWriter logFile = new StreamWriter(logFilePath, true))
                {
                    logFile.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + message);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error writing to log: " + ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WriteToLog("PCI Connect Button Clicked ");
            bool driver_status = FPGACommunicationHandler.FPGAInstance.InitializeWinDriver();
            MessageBox.Show("Driver status " + driver_status);
            WriteToLog("Driver Open Status is " + driver_status);

            bool Device_status = FPGACommunicationHandler.FPGAInstance.DeviceOpen();
            MessageBox.Show("Device open " + Device_status);
            WriteToLog("Device Open Status is " + Device_status);

            if (driver_status)
            {
                string vendorId = "10EE"; // Replace with user-provided vendor ID
                string deviceId = "9038"; // Replace with user-provided device ID
                Check_Device_Info(vendorId, deviceId);
            }
            else
            {
                WriteToLog("Device is not present.");
            }
        }

        private void Eval_Read_Click(object sender, EventArgs e)
        {
            FPGACommunicationHandler.FPGAInstance.ReadFromFPGARegister(0, offset_read_input, ref data_read_output);
            Read_Value_FromPCI.Text = data_read_output.ToString();

        }

        private void Offset_User_Input_TextChanged(object sender, EventArgs e)
        {
            //offset_read_input = int.Parse(Offset_User_Input.Text);
        }

        private void Data_User_Input_TextChanged(object sender, EventArgs e)
        {
            data_write_input = int.Parse(Data_User_Input.Text);
        }

        private void Read_Value_FromPCI_TextChanged(object sender, EventArgs e)
        {
            Read_Value_FromPCI.Text = data_read_output.ToString();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            // Placeholder for future implementation
        }

        private void label1_Click(object sender, EventArgs e)
        {
            // Placeholder for future implementation
        }

        private void label2_Click(object sender, EventArgs e)
        {
            // Placeholder for future implementation
        }

        private void EvalBoard_Write_Click(object sender, EventArgs e)
        {
            FPGACommunicationHandler.FPGAInstance.WriteToFPGARegister(0, Offset_write_input, data_write_input);
        }

        /// <summary>
        /// Check device information and log it.
        /// </summary>
        private void Check_Device_Info(string vendorId, string deviceId)
        {
            try
            {
                ManagementObjectSearcher deviceSearcher = new ManagementObjectSearcher(
                    $"SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID LIKE '%{vendorId}%' AND PNPDeviceID LIKE '%{deviceId}%'");

                bool deviceFound = false;

                using (StreamWriter logFile = new StreamWriter(logFilePath, true))
                {
                    foreach (ManagementObject device in deviceSearcher.Get())
                    {
                        deviceFound = true;
                        string deviceName = (string)device["Name"];
                        Device_Info.Text = deviceName;

                        logFile.WriteLine("Device Name: " + device["Name"]);
                        logFile.WriteLine("Device ID: " + device["DeviceID"]);
                        logFile.WriteLine("Description: " + device["Description"]);
                        logFile.WriteLine("Manufacturer: " + device["Manufacturer"]);
                        logFile.WriteLine();

                        string deviceIdEscaped = device["DeviceID"].ToString().Replace("\\", "\\\\");
                        ManagementObjectSearcher driverSearcher = new ManagementObjectSearcher(
                            $"SELECT * FROM Win32_PnPSignedDriver WHERE DeviceID = '{deviceIdEscaped}'");

                        foreach (ManagementObject driver in driverSearcher.Get())
                        {
                            logFile.WriteLine("Driver Name: " + driver["DeviceName"]);
                            logFile.WriteLine("Driver Version: " + driver["DriverVersion"]);
                            logFile.WriteLine("Driver Provider: " + driver["DriverProviderName"]);
                            logFile.WriteLine("Driver Date: " + driver["DriverDate"]);
                            logFile.WriteLine();
                        }
                    }

                    if (!deviceFound)
                    {
                        logFile.WriteLine("Device not found.");
                    }
                }
            }
            catch (Exception e)
            {
                File.AppendAllText(logFilePath, "Error occurred while querying WMI data: " + e.Message + Environment.NewLine);
            }
        }

        /// <summary>
        /// Reads 8 KB of data from the FPGA registers.
        /// </summary>
        public void Read8KData()
        {
            const int startAddress = 11;      // Starting address (offset)
            const int endAddress = 8203;    // 8 KB = 8192 bytes
            const int step = 4;             // Increment by 4 bytes (DWORD size)
            StringBuilder dataLog = new StringBuilder();

            try
            {
                for (int address = startAddress; address < endAddress; address += step)
                {
                    int data = 0;
                    FPGACommunicationHandler.FPGAInstance.ReadFromFPGARegister(0, address, ref data);
                    dataLog.AppendLine($"Address: 0x{address:X8}, Data: 0x{data:X8}");
                }

                WriteToLog("8 KB Data Read:\n" + dataLog.ToString());
                MessageBox.Show("8 KB data read successfully. Check the log for details.");
            }
            catch (Exception ex)
            {
                WriteToLog("Error reading 8 KB data: " + ex.Message);
                MessageBox.Show("Error occurred while reading 8 KB data.");
            }
        }



        private void read_8kb_data_Click(object sender, EventArgs e)
        {
            WriteToLog("8 KB Data Read Button Clicked");
            Read8KData();
        }

        private void Reinitialize_PCI_Click(object sender, EventArgs e)
        {
            WriteToLog("Reinitializing PCI device...");

            try
            {
                // Step 1: Close the PCI device and driver
                bool closeStatus = FPGACommunicationHandler.FPGAInstance.CloseDevice();
                WriteToLog($"Device close status: {closeStatus}");

                // Step 2: Reinitialize the driver
                bool driverStatus = FPGACommunicationHandler.FPGAInstance.InitializeWinDriver();
                WriteToLog($"Driver reinitialization status: {driverStatus}");

                if (!driverStatus)
                {
                    MessageBox.Show("Failed to reinitialize the driver. Check the logs for details.");
                    return;
                }

                // Step 3: Reopen the device
                bool deviceStatus = FPGACommunicationHandler.FPGAInstance.DeviceOpen();
                WriteToLog($"Device reopen status: {deviceStatus}");

                if (deviceStatus)
                {
                    MessageBox.Show("PCI device reinitialized successfully.");
                }
                else
                {
                    MessageBox.Show("Failed to reopen the device. Check the logs for details.");
                }
            }
            catch (Exception ex)
            {
                WriteToLog("Error during PCI device reinitialization: " + ex.Message);
                MessageBox.Show("An error occurred during PCI device reinitialization. Check the logs for details.");
            }
        }

        private void Read_Offset_input_TextChanged(object sender, EventArgs e)
        {
            offset_read_input = int.Parse(Read_Offset_input.Text);
        }

        /// <summary>
        /// Converts a hexadecimal string to an integer.
        /// </summary>
        /// <param name="hexString">The hexadecimal string (e.g., "1A3").</param>
        /// <returns>The integer value of the hexadecimal string.</returns>
        //public int ConvertHexToInt(string hexString)
        //{
        //    // Remove '0x' prefix if it exists, then convert the hex string to an integer


        //    return Convert.ToInt32(hexString, 16);
        //}

    }
}
Editor is loading...
Leave a Comment