Ejemplo n.º 1
0
        //链接打印机
        public static string LinkPrinter(string printerName, int port)
        {
            try
            {
                string ip = GetRegistryData(printerName + "\\DsSpooler", "portName");

                if (ip == "" || ip.Split(new char[] { '.' }).Length != 4)
                {
                    throw new ConnectionException("没找到IP");
                }
                foreach (string s in ip.Split(new char[] { '.' }))
                {
                    int.Parse(s);
                }


                Ping      pingSender = new Ping();
                PingReply reply      = pingSender.Send(ip, 1);;
                if (reply.Status != IPStatus.Success)
                {
                    throw new ConnectionException("链接失败!");
                }



                /*Ping pingSender = new Ping();
                 * Thread thread = new Thread(new ThreadStart()));
                 * thread.Start();
                 * PingReply reply = null;
                 * reply = pingSender.Send(ip, 1);//第一个参数为ip地址,第二个参数为ping的时间
                 *
                 * Func<string, int, PingReply> func = new Func<string, int, PingReply>(Printer.PingIP);
                 * reply = func(ip, 1);
                 *
                 * if (reply.Status != IPStatus.Success)
                 *      throw new ConnectionException("链接失败!");*/

                TcpConnection connection = new TcpConnection(ip, port);
                connection.Open();
                printerHTTP   = ZebraPrinterFactory.GetInstance(connection);
                linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printerHTTP);
                printerStatus = printerHTTP.GetCurrentStatus();
            }
            catch (ConnectionException e)
            {
                printerHTTP = null;
                FileTools.WriteLineFile(FileTools.exceptionFilePath, DateTime.Now.ToString() + " " + printerName + e.Message);
                return(printerName + e.Message);
            }
            catch (FormatException e)
            {
                printerHTTP = null;
                FileTools.WriteLineFile(FileTools.exceptionFilePath, DateTime.Now.ToString() + " " + printerName + "IP地址不正确!");
                return(printerName + "IP地址不正确!");
            }

            return("");
        }
Ejemplo n.º 2
0
 //关闭链接
 public static void ClosePrinter()
 {
     if (printerHTTP != null)
     {
         printerHTTP.Connection.Close();
         printerHTTP   = null;
         printerStatus = null;
         linkOsPrinter = null;
     }
 }
Ejemplo n.º 3
0
        private List <string> GetPrinterStatusPrefix(ZebraPrinterStatus printerStatus)
        {
            bool   ready              = printerStatus != null ? printerStatus.isReadyToPrint : false;
            string readyString        = "Printer " + (ready ? "ready" : "not ready");
            string labelsInBatch      = "Labels in batch: " + Convert.ToString(printerStatus.labelsRemainingInBatch);
            string labelsInRecvBuffer = "Labels in buffer: " + Convert.ToString(printerStatus.numberOfFormatsInReceiveBuffer);

            return(new List <string> {
                readyString, labelsInBatch, labelsInRecvBuffer
            });
        }
Ejemplo n.º 4
0
        private void GetPrinterStatus()
        {
            Connection printerConnection = null;

            Task.Run(() => {
                try {
                    printerConnection = connectionSelector.GetConnection();
                    printerConnection.Open();

                    ZebraPrinter printer             = ZebraPrinterFactory.GetInstance(printerConnection);
                    ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);

                    ZebraPrinterStatus status = (linkOsPrinter != null) ? linkOsPrinter.GetCurrentStatus() : printer.GetCurrentStatus();

                    string[] printerStatusString      = new PrinterStatusMessages(status).GetStatusMessage();
                    List <string> printerStatusPrefix = GetPrinterStatusPrefix(status);

                    StringBuilder sb = new StringBuilder();
                    foreach (string s in printerStatusPrefix)
                    {
                        sb.AppendLine(s);
                    }

                    foreach (string s in printerStatusString)
                    {
                        sb.AppendLine(s);
                    }

                    Application.Current.Dispatcher.Invoke(() => {
                        printerStatus.Text = sb.ToString();
                    });
                } catch (ConnectionException e) {
                    MessageBoxCreator.ShowError(e.Message, "Connection Error");
                } catch (ZebraPrinterLanguageUnknownException e) {
                    MessageBoxCreator.ShowError(e.Message, "Connection Error");
                } finally {
                    if (printerConnection != null)
                    {
                        try {
                            printerConnection.Close();
                        } catch (ConnectionException) {
                        } finally {
                            SetTestButtonState(true);
                        }
                    }
                    else
                    {
                        SetTestButtonState(true);
                    }
                }
            });
        }
        private void TestConnectionString()
        {
            Task.Run(() => {
                try {
                    ClearProgress();
                    connection = ZebraConnectionBuilder.Build(GetConnectionStringForSdk());
                    PublishProgress("Connection string evaluated as class type " + connection.GetType().Name);
                    connection.Open();

                    PublishProgress("Connection opened successfully");

                    if (IsAttemptingStatusConnection())
                    {
                        ZebraPrinterLinkOs printer = ZebraPrinterFactory.GetLinkOsPrinter(connection);
                        PublishProgress("Created a printer, attempting to retrieve status");

                        ZebraPrinterStatus status = printer.GetCurrentStatus();
                        PublishProgress("Is printer ready to print? " + status.isReadyToPrint);
                    }
                    else
                    {
                        ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection);
                        PublishProgress("Created a printer, attempting to print a config label");
                        printer.PrintConfigurationLabel();
                    }

                    PublishProgress("Closing connection");
                } catch (ConnectionException) {
                    MessageBoxCreator.ShowError("Connection could not be opened", "Error");
                } catch (ZebraPrinterLanguageUnknownException) {
                    MessageBoxCreator.ShowError("Could not create printer", "Error");
                } finally {
                    if (connection != null)
                    {
                        try {
                            connection.Close();
                        } catch (ConnectionException) { } finally {
                            connection = null;
                            SetTestButtonState(true);
                        }
                    }
                    else
                    {
                        SetTestButtonState(true);
                    }
                }
            });
        }