Fix SerialException when trying to close device on __del__ without verifing if is actually opened.

This commit is contained in:
TAHRI Ahmed 2017-06-17 00:58:47 +02:00 committed by Patrick Kanzler
parent c3e952befa
commit efec3e508c
1 changed files with 3 additions and 1 deletions

View File

@ -131,6 +131,8 @@ class Serial(Escpos):
def open(self):
""" Setup serial port and set is as escpos device """
if self.device is not None and self.device.is_open:
self.close()
self.device = serial.Serial(port=self.devfile, baudrate=self.baudrate,
bytesize=self.bytesize, parity=self.parity,
stopbits=self.stopbits, timeout=self.timeout,
@ -151,7 +153,7 @@ class Serial(Escpos):
def close(self):
""" Close Serial interface """
if self.device is not None:
if self.device is not None and self.device.is_open:
self.device.flush()
self.device.close()