public void Print(List <TicketBase> tickets, int issueingNumber, Action <int> onPrint, Action <Exception, int> onError) { StarPrinterStatus status = port.BeginCheckedBlock(); if (status.Offline) { } ICommandBuilder builder = StarIoExt.CreateCommandBuilder(Emulation.StarGraphic); builder.BeginDocument(); tickets.ForEach((t, i) => { try { int height = 8 * 58 - 85 - 15; // height = 8 * 58 - 25 - 15; Bitmap bmp = t.Bitmap.Clone(new Rectangle((t.Bitmap.Width - 576) / 2, 70, 576, height), t.Bitmap.PixelFormat); builder.AppendBitmapWithAbsolutePosition(bmp, false, 576, false, BitmapConverterRotation.Normal, 0); builder.AppendCutPaper(CutPaperAction.PartialCutWithFeed); bmp.Dispose(); onPrint(i); } catch (Exception ex) { onError(ex, i); } }); builder.EndDocument(); byte[] command = builder.Commands; uint writtenLength = port.WritePort(command, 0, (uint)command.Length); status = port.EndCheckedBlock(); if (status.Offline) { } }
public static void SendCommands(byte[] commands, string portName, string portSettings, int timeout, Action <Result> action) { Task task = new Task(() => { Result result = Result.UnknownError; IPort port = null; IPrinterStatus printerStatus; try { result = Result.GetPortError; port = Port.GetPort(portName, portSettings, timeout); result = Result.BeginCheckedBlockError; printerStatus = port.BeginCheckedBlock(); if (printerStatus.Offline) { throw new Exception("Printer is offline (BeginCheckedBlock)"); } result = Result.WritePortError; DateTime startDateTime = DateTime.Now; int total = 0; while (true) { int written = port.WritePort(commands, total, commands.Length - total); total += written; // if (total == commands.Length) if (total >= commands.Length) { break; } TimeSpan timeSpan = DateTime.Now - startDateTime; if (timeSpan.TotalMilliseconds >= 30000) // 30000mS!!! { throw new Exception("Write port timed out"); } } result = Result.EndCheckedBlockError; port.EndCheckedBlockTimeoutMillis = 30000; // 30000mS!!! printerStatus = port.EndCheckedBlock(); if (printerStatus.Offline) { throw new Exception("Printer is offline (EndCheckedBlock)"); } result = Result.Success; } catch (Exception exception) { DebugExt.WriteLine(exception.Message); } finally { if (port != null) { Port.ReleasePort(port); port = null; } } Device.BeginInvokeOnMainThread(() => { action(result); }); }); task.Start(); }
/// <summary> /// Sample : Sending commands to printer with check condition. /// </summary> public static CommunicationResult SendCommands(byte[] commands, string portName, string portSettings, int timeout) { Result result = Result.ErrorUnknown; int code = StarResultCode.ErrorFailed; IPort port = null; try { result = Result.ErrorOpenPort; port = Factory.I.GetPort(portName, portSettings, timeout); StarPrinterStatus status; result = Result.ErrorBeginCheckedBlock; status = port.BeginCheckedBlock(); if (status.Offline) { string message = "Printer is Offline."; if (status.ReceiptPaperEmpty) { message += "\nPaper is Empty."; } if (status.CoverOpen) { message += "\nCover is Open."; } throw new PortException(message); } result = Result.ErrorWritePort; uint commandsLength = (uint)commands.Length; uint writtenLength = port.WritePort(commands, 0, commandsLength); if (writtenLength != commandsLength) { throw new PortException("WritePort failed."); } result = Result.ErrorEndCheckedBlock; status = port.EndCheckedBlock(); if (status.Offline == true) { string message = "Printer is Offline."; if (status.ReceiptPaperEmpty == true) { message += "\nPaper is Empty."; } if (status.CoverOpen == true) { message += "\nCover is Open."; } throw new PortException(message); } result = Result.Success; code = StarResultCode.Succeeded; } catch (PortException ex) { code = ex.ErrorCode; } finally { if (port != null) { Factory.I.ReleasePort(port); } } return(new CommunicationResult() { Result = result, Code = code }); }
/// <summary> /// Sample : Sending commands to printer using redirection. /// </summary> public static CommunicationResult[] SendCommandsForRedirection(byte[] commands, string[] portNameArray, string[] portSettingsArray, int[] timeoutMillisArray) { List <CommunicationResult> resultList = new List <CommunicationResult>(); // Loop sending commands process until it is succeeded. for (int i = 0; i < portNameArray.Length; i++) { Result result = Result.ErrorUnknown; int code = StarResultCode.ErrorFailed; IPort port = null; // Set target printer settings. string portName = portNameArray[i]; string portSettings = portSettingsArray[i]; int timeoutMillis = timeoutMillisArray[i]; try { result = Result.ErrorOpenPort; port = Factory.I.GetPort(portName, portSettings, timeoutMillis); result = Result.ErrorBeginCheckedBlock; StarPrinterStatus status = port.BeginCheckedBlock(); if (status.Offline) { string message = "Printer is Offline."; if (status.ReceiptPaperEmpty) { message += "\nPaper is Empty."; } if (status.CoverOpen) { message += "\nCover is Open."; } throw new PortException(message); } result = Result.ErrorWritePort; uint commandsLength = (uint)commands.Length; uint writtenLength = port.WritePort(commands, 0, commandsLength); if (writtenLength != commandsLength) { throw new PortException("WritePort failed."); } result = Result.ErrorEndCheckedBlock; status = port.EndCheckedBlock(); if (status.Offline == true) { string message = "Printer is Offline."; if (status.ReceiptPaperEmpty == true) { message += "\nPaper is Empty."; } if (status.CoverOpen == true) { message += "\nCover is Open."; } throw new PortException(message); } result = Result.Success; code = StarResultCode.Succeeded; } catch (PortException ex) { code = ex.ErrorCode; } finally { if (port != null) { Factory.I.ReleasePort(port); } } resultList.Add(new CommunicationResult() { Result = result, Code = code }); // Finish process if it is succeeded. if (result == Result.Success) { break; } } return(resultList.ToArray()); }
/// <summary> /// Sample : Sending commands to printer with check condition (already open port). /// </summary> public static CommunicationResult SendCommands(byte[] commands, IPort port) { Result result = Result.ErrorUnknown; int code; try { result = Result.ErrorOpenPort; if (port == null) { throw new PortException("port is null."); } StarPrinterStatus printerStatus; result = Result.ErrorBeginCheckedBlock; printerStatus = port.BeginCheckedBlock(); if (printerStatus.Offline) { string message = "Printer is Offline."; if (printerStatus.ReceiptPaperEmpty) { message += "\nPaper is Empty."; } if (printerStatus.CoverOpen) { message += "\nCover is Open."; } throw new PortException(message); } result = Result.ErrorWritePort; uint commandsLength = (uint)commands.Length; uint writtenLength = port.WritePort(commands, 0, commandsLength); if (writtenLength != commandsLength) { throw new PortException("WritePort failed."); } result = Result.ErrorEndCheckedBlock; printerStatus = port.EndCheckedBlock(); if (printerStatus.Offline == true) { string message = "Printer is Offline."; if (printerStatus.ReceiptPaperEmpty == true) { message += "\nPaper is Empty."; } if (printerStatus.CoverOpen == true) { message += "\nCover is Open."; } throw new PortException(message); } result = Result.Success; code = StarResultCode.Succeeded; } catch (PortException ex) { code = ex.ErrorCode; } return(new CommunicationResult() { Result = result, Code = code }); }
/// <summary> /// Sample : Sending commands to printer with check condition (already open port). /// </summary> public static CommunicationResult SendCommands(byte[] commands, IPort port) { CommunicationResult result = CommunicationResult.ErrorUnknown; try { if (port == null) { return(CommunicationResult.ErrorOpenPort); } StarPrinterStatus status; result = CommunicationResult.ErrorBeginCheckedBlock; status = port.BeginCheckedBlock(); if (status.Offline) { string message = "Printer is Offline."; if (status.ReceiptPaperEmpty) { message += "\nPaper is Empty."; } if (status.CoverOpen) { message += "\nCover is Open."; } throw new PortException(message); } result = CommunicationResult.ErrorWritePort; uint commandsLength = (uint)commands.Length; uint writtenLength = port.WritePort(commands, 0, commandsLength); if (writtenLength != commandsLength) { throw new PortException("WritePort failed."); } result = CommunicationResult.ErrorEndCheckedBlock; status = port.EndCheckedBlock(); if (status.Offline == true) { string message = "Printer is Offline."; if (status.ReceiptPaperEmpty == true) { message += "\nPaper is Empty."; } if (status.CoverOpen == true) { message += "\nCover is Open."; } throw new PortException(message); } result = CommunicationResult.Success; } catch (PortException) { } return(result); }