请问思路应该怎么走?打印机是并口吧,我只做过串口读取数据的程序。
如何向打印机发送指令呢?

解决方案 »

  1.   

    使用LPT1口就好了,
            [DllImport("kernel32")]
            static extern SafeFileHandle CreateFile(
            string filename,
            uint desiredAccess,
            uint shareMode,
            uint attributes,       //   really   SecurityAttributes   pointer   
            uint creationDisposition,
            uint flagsAndAttributes,
            uint templateFile);
            private const uint GENERIC_READ = 0x80000000;
            private const uint GENERIC_WRITE = 0x40000000;
            private const int OPEN_EXISTING = 3;
            private const int INVALID_HANDLE_VALUE = -1; public static void PrintStringToPrinter(string as_str, string s_Port_)
            {
                string s_Port = s_Port_;
                switch (s_Port.ToUpper())
                {
                    case "LPT1":
                        SafeFileHandle iHandle;
                        iHandle = CreateFile("LPT1", GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
                        if (iHandle.IsInvalid)
                        {
                            throw new Exception("Can't find the printer connecting to the port of LPT1");
                        }
                        else
                        {
                            FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
                            //fs.WriteTimeout=0; 
                            byte[] byteOut;
                            Int32 dwCount = as_str.Length;
                            byteOut = new byte[dwCount];
                            for (int i = 0; i < dwCount; i++)
                            {
                                byteOut[i] = System.Convert.ToByte(as_str[i]);
                            }
                            fs.Write(byteOut, 0, dwCount);
                            fs.Flush();
                            fs.Close();
                        }
                        break;
                    
                }        }