Exemplo n.º 1
0
        /// <summary>
        /// Prints the specified string value to the printer.
        /// </summary>
        /// <param name="value">The value to print.</param>
        public override void Print(string value)
        {
            bool  isOnline = false;
            IPort port     = null;

            try
            {
                port     = Factory.I.GetPort("USBPRN:" + PrinterName, "", Timeout);
                isOnline = port.GetOnlineStatus();

                if (!isOnline)
                {
                    throw new PortException("The printer is offline.");
                }

                byte[] dataByteArray     = Encoding.GetEncoding("Windows-1252").GetBytes(value);
                uint   amountWritten     = 0;
                uint   amountWrittenKeep = 0;
                while (dataByteArray.Length > amountWritten)
                {
                    amountWrittenKeep = amountWritten;
                    amountWritten    += port.WritePort(dataByteArray, amountWritten, (uint)dataByteArray.Length - amountWritten);
                    if (amountWrittenKeep == amountWritten)
                    {
                        throw new PortException("Can't send data.");
                    }
                }

                if (amountWritten != dataByteArray.Length)
                {
                    throw new PortException("All data was not sent.");
                }
            }
            finally
            {
                if (port != null)
                {
                    Factory.I.ReleasePort(port);
                }
            }
        }
        public void PrintFromStarIO(string prnData)
        {
            //Set the status to offline because this is a new attempt to print
            onlineStatus = false;

            //TRY -> Use the textboxes to check if the port info given will connect to the printer.
            try
            {
                //Very important to only try opening the port in a Try, Catch incase the port is not working
                this.sPort = Factory.I.GetPort(PrinterPortname, Printersetting, 10000);

                //GetOnlineStatus() will return a boolean to let us know if the printer was reachable.
                this.onlineStatus = sPort.GetOnlineStatus();
            }

            //CATCH -> If the port information is bad, catch the failure.
            catch (PortException err)
            {
                //label1.Text = "Error Message: " + err.Message + "\n\n\nStacktrace: " + err.StackTrace;
                onlineStatus = false;
                if (this.sPort != null)
                {
                    Factory.I.ReleasePort(this.sPort);


                    //label1.Text = "Port Error";
                    tbStatus.BackColor = Color.Red;
                    return; //exit this entire function
                }
            }

            //If it is offline, dont setup receipt or try to write to the port.
            if (onlineStatus == false)
            {
                //label1.Text = "offline";
                tbStatus.BackColor = Color.Red;
                Factory.I.ReleasePort(sPort);
            }
            //Else statement means it is ONLINE, lets start the printjob
            else
            {
                //label1.Text = "online";
                tbStatus.BackColor = Color.Green;

                Encoding targetCodePage;                         // (1)
                byte[]   encodedBytes;                           // (2)
                int      codePage = 865;                         // (4)
                targetCodePage = Encoding.GetEncoding(codePage); // (5)
                string cmd = "\x1b\x1d\x74\x09";                 // (7)
                //encodedBytes = targetCodePage.GetBytes(cmd + this.txtCodePage.Text.ToString() + "\x0a"); // (6)

                byte[] dataByteArray = targetCodePage.GetBytes(cmd + prnData + "\x0a");
                //Write bytes to printer
                uint amountWritten = 0;
                uint amountWrittenKeep;
                while (dataByteArray.Length > amountWritten)
                {
                    //This while loop is very important because in here is where the bytes are being sent out through StarIO to the printer
                    amountWrittenKeep = amountWritten;
                    try
                    {
                        amountWritten += sPort.WritePort(dataByteArray, amountWritten, (uint)dataByteArray.Length - amountWritten);
                    }
                    catch (PortException)
                    {
                        // error happen while sending data
                        //this.lblPrinterStatus.Text = "Port Error";
                        //this.lblPrinterStatus.ForeColor = Color.Red;
                        Factory.I.ReleasePort(sPort);
                        return;
                    }

                    if (amountWrittenKeep == amountWritten) // no data is sent
                    {
                        Factory.I.ReleasePort(sPort);
                        //lblPrinterStatus.Text = "Can't send data";
                        //this.lblPrinterStatus.ForeColor = Color.Red;
                        return; //exit this entire function
                    }
                }

                //Release the port
                //THIS IS VERY IMPORTANT, IF YOU OPEN THE PORT AND DO NOT CLOSE IT, YOU WILL HAVE PROBLEMS
                Factory.I.ReleasePort(sPort);
                //////////////////////////

                //Send the data to the log, you can take this out of the code in your application
                //appendLog(dataByteArray);
            }
        }