C#基于inpoutx64读写ECRAM硬件信息

inpoutx64.dll分享路径:

链接:https://pan.baidu.com/s/1rOt0xtt9EcsrFQtf7S91ag 
提取码:7om1 
 

1.InpOutManager:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace TestLEDWinFrm
{
    public class InpOutManager
    {
        public bool IsInpOutDriverOpen { get; set; }//端口是否已打开
        public short PORT_INDEX { get; } = 0x66;//端口号

        public short EC_COMMAND_WRITE { get; } = 0X81;//EC命令写入

        public short PORT_DATA { get; } = 0X62;//端口数据

        public string Err { get; set; }//错误信息

        [DllImport("inpoutx64.dll", EntryPoint = "IsInpOutDriverOpen")]
        public static extern uint isInpOutDriverOpen();

        [DllImport("inpoutx64.dll", EntryPoint = "Out32")]
        public static extern void Out32(short PortAddress, short Data);

        [DllImport("inpoutx64.dll", EntryPoint = "Inp32")]
        public static extern byte Inp32(short PortAddress);

        //[DllImport("inpout32.dll")]
        //private static extern void DlPortWritePortUshort(short PortAddress, ushort Data);
        //[DllImport("inpout32.dll")]
        //private static extern ushort DlPortReadPortUshort(short PortAddress);
        //[DllImport("inpout32.dll")]
        //private static extern void DlPortWritePortUlong(int PortAddress, uint Data);
        //[DllImport("inpout32.dll")]
        //private static extern uint DlPortReadPortUlong(int PortAddress);

        //[DllImport("inpoutx64.dll")]
        //private static extern bool GetPhysLong(ref int PortAddress, ref uint Data);
        //[DllImport("inpoutx64.dll")]
        //private static extern bool SetPhysLong(ref int PortAddress, ref uint Data);
        //[DllImport("inpoutx64.dll", EntryPoint = "IsInpOutDriverOpen")]
        //private static extern UInt32 IsInpOutDriverOpen_x64();
        //[DllImport("inpoutx64.dll", EntryPoint = "Out32")]
        //private static extern void Out32_x64(short PortAddress, short Data);
        //[DllImport("inpoutx64.dll", EntryPoint = "Inp32")]
        //private static extern char Inp32_x64(short PortAddress);

        //[DllImport("inpoutx64.dll", EntryPoint = "DlPortWritePortUshort")]
        //private static extern void DlPortWritePortUshort_x64(short PortAddress, ushort Data);
        //[DllImport("inpoutx64.dll", EntryPoint = "DlPortReadPortUshort")]
        //private static extern ushort DlPortReadPortUshort_x64(short PortAddress);
        //[DllImport("inpoutx64.dll", EntryPoint = "DlPortWritePortUlong")]
        //private static extern void DlPortWritePortUlong_x64(int PortAddress, uint Data);
        //[DllImport("inpoutx64.dll", EntryPoint = "DlPortReadPortUlong")]
        //private static extern uint DlPortReadPortUlong_x64(int PortAddress);

        //[DllImport("inpoutx64.dll", EntryPoint = "GetPhysLong")]
        //private static extern bool GetPhysLong_x64(ref int PortAddress, ref uint Data);
        //[DllImport("inpoutx64.dll", EntryPoint = "SetPhysLong")]
        //private static extern bool SetPhysLong_x64(ref int PortAddress, ref uint Data);


        public InpOutManager()
        {
            this.IsInpOutDriverOpen=isInpOutDriverOpen()>0?true:false;
        }

        /// 
        /// 读取端口数据
        /// 
        /// 
        /// 
        public bool InputPortData(short PortAddress)
        {
            try
            {
                Inp32(PortAddress);
                return true;
            }
            catch(Exception ex)
            {
                this.Err = ex.Message;
                return false;
            }
        }
        //Out32(short PortAddress, short Data)
        public bool OutPortData(short PortAddress,short Data)
        {
            try
            {
                WaitECInputBufferEmpty();
                Out32(0x66,0x81);
                //Out32(PORT_INDEX,EC_COMMAND_WRITE);
                WaitECInputBufferEmpty();
                Out32(0x62,PortAddress);
                //Out32(PORT_DATA,PortAddress);
                WaitECInputBufferEmpty();
                Out32(0x62,Data);
                //Out32(PORT_DATA,Data);
                return true;
            }
            catch(Exception ex)
            {
                this.Err=ex.Message;
                return false;
            }
        }

        private void WaitECInputBufferEmpty()
        {
            var IBF = 2;
            do
            {
                IBF = Inp32(0x66)&2;
                //IBF = (PORT_INDEX)&2;
            }
            while (IBF == 2);
        }

    }
}
namespace TestLEDWinFrm
{
    public partial class MainWinFrm : Form
    {
        InpOutManager inpOutManager;
        private System.Windows.Forms.Timer timer;
        private int randcount = 0;
        public MainWinFrm()
        {
            InitializeComponent();
            inpOutManager = new InpOutManager();

            //返还权限给EC
            //inpOutManager.OutPortData(0X32, 0X04);
            //inpOutManager.OutPortData(0X30, 0X21);
        }

        #region 所有LED灯点亮测试
        /// 
        /// 所有LED灯点亮测试
        /// 
        /// 次数
        public void AllLEDIllumeTest(int count)
        {
            int i = 0;
            while (i < count)
            {
                //点亮所有LED灯
                inpOutManager.OutPortData(0X32, 0X01);
                inpOutManager.OutPortData(0X30, 0X21);
                Thread.Sleep(1000);
                //熄灭所有LED
                inpOutManager.OutPortData(0X32, 0X02);
                inpOutManager.OutPortData(0X30, 0X21);
                Thread.Sleep(1000);
                //返还权限给EC
                inpOutManager.OutPortData(0X32, 0X04);
                inpOutManager.OutPortData(0X30, 0X21);

                i++;
            }

        }
        #endregion

        #region 电源LED点亮测试
        /// 
        /// 电源LED点亮测试
        /// 
        /// 
        public void BatteryLedIllumTest(int count)
        {
            int i = 0;
            while (i < count)
            {
                //点亮Battery指示灯
                inpOutManager.OutPortData(0X32, 0X03);
                inpOutManager.OutPortData(0X30, 0X21);

                Thread.Sleep(1000);
                //熄灭所有指示灯
                inpOutManager.OutPortData(0X32, 0X02);
                inpOutManager.OutPortData(0X30, 0X21);

                Thread.Sleep(1000);
                //返还权限给EC
                inpOutManager.OutPortData(0X32, 0X04);
                inpOutManager.OutPortData(0X30, 0X21);
                i++;
            }

        }
        #endregion

        #region 历史记录
        private void btn_StartTest_Click(object sender, EventArgs e)
        {
            //inpOutManager.InputPortData(0X32);
            inpOutManager.OutPortData(0X32, 0X01);
            //inpOutManager.InputPortData(0X30);
            inpOutManager.OutPortData(0X30, 0X21);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //熄灭所有LED

            //inpOutManager.InputPortData(0X32);
            inpOutManager.OutPortData(0X32, 0X02);
            //inpOutManager.InputPortData(0X30);
            inpOutManager.OutPortData(0X30, 0X21);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //点亮第二个battery led

            //inpOutManager.InputPortData(0X32);
            inpOutManager.OutPortData(0X32, 0X03);

            //inpOutManager.InputPortData(0X30);
            inpOutManager.OutPortData(0X30, 0X21);
            //WinIoFunction.SetPhysValue("0X32", "0X03");
            //WinIoFunction.SetPhysValue("0X30", "0X21");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //点亮第二个battery led
            //inpOutManager.InputPortData(0X32);
            inpOutManager.OutPortData(0X32, 0X04);

            //inpOutManager.InputPortData(0X30);
            inpOutManager.OutPortData(0X30, 0X21);
            //WinIoFunction.SetPhysValue("0X32", "0X04");
            //WinIoFunction.SetPhysValue("0X30", "0X21");
        }
        #endregion

        #region 关闭
        private void btn_Close_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(1);//程式退出返回1
        }
        #endregion

        #region 窗体移动
        private Point mouseOff;//鼠标移动位置变量
        private bool leftFlag;//标签是否为左键

        private void Frm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }

        private void Frm_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void Frm_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }
        #endregion

        #region 时间同步
        private void Timer_Tick(object sender, EventArgs e)
        {
            ts_DateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region 桌面加载
        private void MainWinFrm_Load(object sender, EventArgs e)
        {
            timer = new System.Windows.Forms.Timer();
            timer.Interval = 1000;
            timer.Tick += Timer_Tick!;
            timer.Enabled = true;
        }
        #endregion

        #region 移动鼠标坐标
        private void MainFrm_Move(object sender, EventArgs e)
        {
            // 获取当前鼠标的坐标
            Point cursorPosition = Cursor.Position;
            TS_X.Text = cursorPosition.X.ToString();
            TS_Y.Text = cursorPosition.Y.ToString();
        }
        #endregion

        #region 日志信息
        private void Loginfo(string log, bool isPass, int item = 0)
        {
            Invoke(() =>
            {
                ListViewItem li_er = new ListViewItem();
                li_er.SubItems[0].Text = log;
                li_er.SubItems.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                li_er.ForeColor = isPass ? Color.Green : Color.Red;
                lv_log.Items.Add(li_er);

                if (item == 1)
                {
                    //this.txt_ScanSn.Enabled = true;
                    //this.Focus();
                    btn_StartTest.Enabled = false;
                    btn_Restart.Enabled = false;
                }
                else if (item == 2)//启动重测
                {
                    //this.txt_ScanSn.Enabled = false;
                    btn_Restart.Enabled = true;
                    btn_StartTest.Enabled = false;
                }
            });
        }
        #endregion

        private void btn_StartTest_Click_1(object sender, EventArgs e)
        {
            btn_StartTest.Enabled = false;
            this.StartTest();//开始测试
        }

        #region 开始测试
        public void StartTest()
        {
            //lbl_TestItem.Text = "开始所有LED指示灯,随机闪烁次数测试!!";
            //lbl_TestImage.Image = Properties.Resources._1;
            //Random random = new Random();
            //this.randcount = random.Next(1, 5);
            //this.AllLEDIllumeTest(this.randcount);//随机数

            lbl_TestItem.Text = "开始电源充电指示灯,随机闪烁次数测试!!";
            lbl_TestImage.Image = Properties.Resources._2;
            Random random = new Random();
            this.randcount = random.Next(1, 5);
            this.BatteryLedIllumTest(this.randcount);//随机电源指示灯次数
            foreach (Control control in this.groupBox5.Controls)
            {
                if (control is Button)
                {
                    ((Button)control).Enabled = true;
                }
            }
        }
        #endregion


        #region 初始化界面
        private void Winitial(bool IsEnable)
        {
            foreach (Control control in this.groupBox5.Controls)
            {
                if (control is Button)
                {
                    ((Button)control).Enabled = IsEnable;
                }
            }

            lbl_TestImage.Image = null;
            lbl_TestItem.Text = "待开始电源指示灯测试!!";
            lbl_TestResult.Text = "待测试";
            lbl_TestImage.ForeColor = Color.SandyBrown;
        }
        #endregion

        #region 重测试
        private void btn_Restart_Click(object sender, EventArgs e)
        {
            this.Winitial(false);//初始化
            this.StartTest();//开始测试
        }
        #endregion


        bool isFirst = true;
        private void btn_num_Click(object sender, EventArgs e)
        {
            if (isFirst)
            {
                if (((Button)sender).Text == this.randcount.ToString())
                {
                    this.lbl_TestResult.ForeColor = Color.Green;
                    this.lbl_TestResult.Text = "PASS";

                    //lbl_TestItem.Text = "开始电源充电指示灯,随机闪烁次数测试!!";
                    //lbl_TestImage.Image = Properties.Resources._2;
                    //Random random = new Random();
                    //this.randcount = random.Next(1, 5);
                    //this.BatteryLedIllumTest(this.randcount);//随机电源指示灯次数

                    lbl_TestItem.Text = "开始所有LED指示灯,随机闪烁次数测试!!";
                    lbl_TestImage.Image = Properties.Resources._1;
                    Random random = new Random();
                    this.randcount = random.Next(1, 5);
                    this.AllLEDIllumeTest(this.randcount);//随机数

                    isFirst = false;
                }
                else
                {
                    this.lbl_TestResult.ForeColor = Color.Red;
                    this.lbl_TestResult.Text = "FAIL";
                    btn_Restart.Enabled = true;
                    btn_StartTest.Enabled = false;
                    isFirst = true;
                    //this.Loginfo("所有LED点亮测试,闪烁次数与实际选择的序号不符,测试结果FAIL!!", false);
                    this.Loginfo("电源指示灯测试,闪烁次数与实际选择的序号不符,测试结果FAIL!!", false);
                    foreach (Control control in this.groupBox5.Controls)
                    {
                        if (control is Button)
                        {
                            ((Button)control).Enabled = false;
                        }
                    }

                }
            }
            else
            {
                if (((Button)sender).Text == this.randcount.ToString())
                {
                    this.lbl_TestResult.ForeColor = Color.Green;
                    this.lbl_TestResult.Text = "PASS";
                    timer1.Enabled = true;
                }
                else
                {
                    this.lbl_TestResult.ForeColor = Color.Red;
                    this.lbl_TestResult.Text = "FAIL";
                    btn_Restart.Enabled = true;
                    btn_StartTest.Enabled = false;
                    isFirst = true;
                    //this.Loginfo("电源指示灯测试,闪烁次数与实际选择的序号不符,测试结果FAIL!!", false);
                    this.Loginfo("所有LED点亮测试,闪烁次数与实际选择的序号不符,测试结果FAIL!!", false);
                    foreach (Control control in this.groupBox5.Controls)
                    {
                        if (control is Button)
                        {
                            ((Button)control).Enabled = false;
                        }
                    }
                }
            }

        }

        private int index = 5;
        private void timer1_Tick(object sender, EventArgs e)
        {
            lbl_Exit.Visible = true;
            if (index > 0)
            {
                lbl_Exit.Text = index.ToString();
                index--;
            }
            else
            {
                System.Environment.Exit(0);
            }

        }
    }
}

UI展示:

C#基于inpoutx64读写ECRAM硬件信息_第1张图片

你可能感兴趣的:(c#,java,数据库)