通过并口打开钱箱

        原本计划使用“inpout32.dll”,但是就是没有成功,下载了一个并口监视器监视厂商的DEMO,发现可以监测到指令,可是监测不到inpout32.dll产生的指令,通过万用表测量并口的电平,发现有跟随着指令改变,开始怀疑写到并口的方式不被接受,例如写入的速率等问题,因此更换为文件操作方式。结果成功了。
        下一步要做一个通过并口控制的PWM信号,来控制我的步进电机,不清楚是否可以使用inpout32.dll,还是要使用Write
 
 
        private const int OPEN_EXISTING = 3;
        private string mPrnPort = "LPT1";
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);

        public void OpenBox()
        {
            try
            {
                IntPtr mHandle = CreateFile(mPrnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
                if (mHandle.ToInt32() == -1)
                {
                    throw new Exception("打开端口失败");
                }
                else
                {
                    FileStream mFS = new FileStream(mHandle, FileAccess.ReadWrite);
                    StreamWriter mSW = new StreamWriter(mFS, System.Text.Encoding.Default);
                    //开钱箱
                    mSW.Write(((char)27).ToString() + ((char)112).ToString() + ((char)0).ToString() + ((char)60).ToString() + ((char)255).ToString());
                    mSW.Close();
                    mFS.Close();
                }
            }
            catch (Exception Ex)
            {               
                throw Ex;
            }
        }

你可能感兴趣的:(职场,休闲,通过并口打开钱箱)