2022-06-01 16:37:19 +00:00
|
|
|
import sys
|
|
|
|
|
|
2022-07-18 10:32:05 +00:00
|
|
|
if "--sim-serial" in sys.argv:
|
2022-06-29 11:04:31 +00:00
|
|
|
from components.dummies.serial import serial
|
2022-06-01 16:37:19 +00:00
|
|
|
else:
|
|
|
|
|
import serial
|
|
|
|
|
|
|
|
|
|
from PyQt5.QtWidgets import QMessageBox
|
|
|
|
|
|
|
|
|
|
from .component import Component
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Serial_Label_Printer(Component):
|
|
|
|
|
def __init__(self, config=None, name=None):
|
|
|
|
|
super().__init__(config=config, name=name, threaded=False)
|
|
|
|
|
|
|
|
|
|
def config_changed(self):
|
2022-06-29 09:02:58 +00:00
|
|
|
self.address = self.config[self.name]["address"]
|
|
|
|
|
self.baudrate = int(self.config[self.name]["baudrate"])
|
|
|
|
|
self.stopbits = getattr(serial, self.config[self.name].get("stopbits", "stopbits_one").upper())
|
|
|
|
|
self.parity = getattr(serial, self.config[self.name].get("parity", "parity_none").upper())
|
|
|
|
|
self.bytesize = getattr(serial, self.config[self.name].get("bytesize", "eightbits").upper())
|
2022-06-01 16:37:19 +00:00
|
|
|
|
|
|
|
|
def print_label(self, template, archived):
|
|
|
|
|
# LOAD LABEL TEMPLATE
|
|
|
|
|
with open(f"config/label_templates/{template}.prn") as f:
|
|
|
|
|
label = f.read()
|
|
|
|
|
# LABEL PRINT
|
|
|
|
|
label = label.replace("$PH1", archived.barcode).replace("$PH2", archived.barcode)
|
|
|
|
|
try:
|
|
|
|
|
conn = serial.Serial(
|
|
|
|
|
self.address,
|
|
|
|
|
baudrate=self.baudrate,
|
|
|
|
|
stopbits=self.stopbits,
|
|
|
|
|
parity=self.parity,
|
|
|
|
|
bytesize=self.bytesize,
|
|
|
|
|
)
|
|
|
|
|
conn.write(bytes(label, encoding="ascii"))
|
|
|
|
|
conn.close()
|
|
|
|
|
except serial.serialutil.SerialException as e:
|
|
|
|
|
QMessageBox.critical(
|
|
|
|
|
None,
|
|
|
|
|
"Errore Connessione Stampante",
|
|
|
|
|
"Non e stato possibile connettersi alla stampante di etichette\nL'etichetta non verra stampata.\n\nErrore:\n" + str(e)
|
|
|
|
|
)
|