private void OnPrinterChanged(object sender, System.EventArgs e) { //Event handler for change in port try { if (this.mPrinter != null && this.mPrinter.On) { this.mPrinter.TurnOff(); } this.mPrinter = DeviceFactory.CreatePrinter(this.cboPrinter.SelectedItem.ToString(), this.cboPort.SelectedItem.ToString()); if (this.mPrinter != null && this.mPrinter.Settings.PortName != null) { this.cboPort.SelectedItem = this.mPrinter.Settings.PortName; this.cboBaud.SelectedItem = this.mPrinter.Settings.BaudRate.ToString(); this.cboDataBits.SelectedItem = this.mPrinter.Settings.DataBits.ToString(); this.cboParity.SelectedItem = this.mPrinter.Settings.Parity.ToString(); this.cboStopBits.SelectedItem = Convert.ToInt32(this.mPrinter.Settings.StopBits).ToString(); } else { this.cboPort.SelectedIndex = 0; this.cboBaud.SelectedIndex = 0; this.cboDataBits.SelectedIndex = 0; this.cboParity.SelectedIndex = 0; this.cboStopBits.SelectedIndex = 0; } if (this.PrinterChanged != null) { this.PrinterChanged(this.mPrinter, EventArgs.Empty); } } catch (Exception ex) { reportError(ex); } finally { setUserServices(); } }
public void ChangePrinter(string printerType, string portName) { //Change station printer try { if (printerType != "" && portName != "") { ILabelPrinter oPrinter = DeviceFactory.CreatePrinter(printerType, portName); PortSettings oSettings = oPrinter.DefaultSettings; NameValueCollection nvcPrinter = (NameValueCollection)ConfigurationManager.GetSection("station/printer"); oSettings.PortName = portName; oSettings.BaudRate = Convert.ToInt32(nvcPrinter["BaudRate"]); oSettings.DataBits = Convert.ToInt32(nvcPrinter["DataBits"]); switch (nvcPrinter["Parity"].ToString().ToLower()) { case "none": oSettings.Parity = Parity.None; break; case "even": oSettings.Parity = Parity.Even; break; case "odd": oSettings.Parity = Parity.Odd; break; } switch (Convert.ToInt32(nvcPrinter["StopBits"])) { case 1: oSettings.StopBits = StopBits.One; break; case 2: oSettings.StopBits = StopBits.Two; break; } switch (nvcPrinter["Handshake"].ToString().ToLower()) { case "none": oSettings.Handshake = Handshake.None; break; case "rts/cts": oSettings.Handshake = Handshake.RequestToSend; break; case "xon/xoff": oSettings.Handshake = Handshake.XOnXOff; break; } oPrinter.Settings = oSettings; //Atach printer to sort station and turn-on if (this.mLabelPrinter != null) { this.mLabelPrinter.TurnOff(); } this.mLabelPrinter = oPrinter; this.mLabelPrinter.TurnOn(); } } catch (System.IO.IOException) { } catch (ApplicationException ex) { throw ex; } catch (Exception ex) { throw new ApplicationException("Unexpected error while setting the printer.", ex); } finally { if (this.PrinterChanged != null) { this.PrinterChanged(this, new EventArgs()); } } }
public void SetLabelPrinter(string printerType, string portName) { // try { //Validate printer type and port name if (!DeviceFactory.PrinterTypeExists(printerType)) { throw new ArgumentException("'" + printerType + "' is an invalid printer type."); } if (portName.Length <= 0) { throw new ArgumentException("'" + portName + "' is an invalid port name."); } //Create/change the attached label printer if (this.mPrinter != null) { //A current printer exists: check for change to printer type if (printerType != this.mPrinter.Type) { //New printer type: disconnect current printer and attach a new one //with the same settings (except use the specified port name) bool bOn = this.mPrinter.On; if (bOn) { this.mPrinter.TurnOff(); } PortSettings settings = this.mPrinter.Settings; settings.PortName = portName; ILabelPrinter printer = DeviceFactory.CreatePrinter(printerType, portName); if (printer == null) { throw new ApplicationException("Printer " + printerType + " on port " + portName + " could not be created."); } printer.Settings = settings; this.mPrinter = printer; if (bOn) { this.mPrinter.TurnOn(); } } else { //Same printer type: check for change in printer port if (portName != this.mPrinter.Settings.PortName) { //Change port on existing printer PortSettings settings = this.mPrinter.Settings; settings.PortName = portName; this.mPrinter.Settings = settings; } } } else { //Create a new label printer this.mPrinter = DeviceFactory.CreatePrinter(printerType, portName); if (this.mPrinter == null) { throw new ApplicationException("Printer " + printerType + " on port " + portName + " could not be created."); } //Set printer configuration; start with printer's default settings PortSettings settings = this.mPrinter.DefaultSettings; NameValueCollection nvcPrinter = (NameValueCollection)ConfigurationManager.GetSection("station/printer"); settings.PortName = portName; settings.BaudRate = Convert.ToInt32(nvcPrinter["BaudRate"]); settings.DataBits = Convert.ToInt32(nvcPrinter["DataBits"]); switch (nvcPrinter["Parity"].ToString().ToLower()) { case "none": settings.Parity = System.IO.Ports.Parity.None; break; case "even": settings.Parity = System.IO.Ports.Parity.Even; break; case "odd": settings.Parity = System.IO.Ports.Parity.Odd; break; } switch (Convert.ToInt32(nvcPrinter["StopBits"])) { case 1: settings.StopBits = System.IO.Ports.StopBits.One; break; case 2: settings.StopBits = System.IO.Ports.StopBits.Two; break; } switch (nvcPrinter["Handshake"].ToString().ToLower()) { case "none": settings.Handshake = System.IO.Ports.Handshake.None; break; case "rts/cts": settings.Handshake = System.IO.Ports.Handshake.RequestToSend; break; case "xon/xoff": settings.Handshake = System.IO.Ports.Handshake.XOnXOff; break; } this.mPrinter.Settings = settings; } } catch (Exception ex) { throw new ApplicationException("Unexpected error setting the label printer.", ex); } finally { if (this.PrinterChanged != null) { this.PrinterChanged(this, new EventArgs()); } } }