From efec3e508c1fa487ff35225034bb03776fc80270 Mon Sep 17 00:00:00 2001 From: TAHRI Ahmed Date: Sat, 17 Jun 2017 00:58:47 +0200 Subject: [PATCH] Fix SerialException when trying to close device on __del__ without verifing if is actually opened. --- src/escpos/printer.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/escpos/printer.py b/src/escpos/printer.py index d14b93d..a15fe21 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -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()