[POS-commit] r1165 - fiscalprinter/trunk/fiscalprinter

Evandro Vale Miquelito evandro at async.com.br
Fri Sep 9 11:21:53 BRT 2005


Author: evandro
Date: Fri Sep  9 11:21:53 2005
New Revision: 1165

Added:
   fiscalprinter/trunk/fiscalprinter/basedevice.py
   fiscalprinter/trunk/fiscalprinter/networkserial.py
   fiscalprinter/trunk/fiscalprinter/serialdevice.py
Log:
Adding device management modules


Added: fiscalprinter/trunk/fiscalprinter/basedevice.py
==============================================================================
--- (empty file)
+++ fiscalprinter/trunk/fiscalprinter/basedevice.py	Fri Sep  9 11:21:53 2005
@@ -0,0 +1,50 @@
+# -*- Mode: Python; coding: iso-8859-1 -*-
+# vi:si:et:sw=4:sts=4:ts=4
+
+##
+## Fiscal Printer
+## Copyright (C) 2005 Async Open Source <http://www.async.com.br>
+## All rights reserved
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+## USA.
+##
+## Author(s):   Christian Reis              <kiko at async.com.br>
+##              Evandro Vale Miquelito      <evandro at async.com.br>
+##
+"""
+fiscalprinter/basedevice.py:
+    
+     Base serial device definition
+"""
+
+
+class BaseSerialDevice:
+    "Simple class that shows what should be implemented on subclasses."
+    
+    def __init__(self):
+        pass
+
+    def read(self, n_bytes):
+        "Subclasses should implement this properly"        
+        raise NotImplementedError
+
+    def write(self, data):
+        "Subclasses should implement this properly"
+        raise NotImplementedError        
+
+    def close(self):
+        "Subclasses should implement this properly"        
+        raise NotImplementedError        

Added: fiscalprinter/trunk/fiscalprinter/networkserial.py
==============================================================================
--- (empty file)
+++ fiscalprinter/trunk/fiscalprinter/networkserial.py	Fri Sep  9 11:21:53 2005
@@ -0,0 +1,71 @@
+# -*- Mode: Python; coding: iso-8859-1 -*-
+# vi:si:et:sw=4:sts=4:ts=4
+
+##
+## Fiscal Printer
+## Copyright (C) 2005 Async Open Source <http://www.async.com.br>
+## All rights reserved
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+## USA.
+##
+## Author(s):   Christian Reis              <kiko at async.com.br>
+##              Evandro Vale Miquelito      <evandro at async.com.br>
+##
+"""
+fiscalprinter/networkserial.py:
+    
+    Network serial routines
+"""
+
+import socket
+
+from fiscalprinter.basedevice import BaseSerialDevice
+from fiscalprinter.exceptions import CommError
+
+
+class NetworkSerialDevice(BaseSerialDevice):
+    """This class is designed to work with the 'ser2net' package, from
+    http://ser2net.sourceforge.net.
+
+    You should use this device when the serial port is not on the same
+    machine the application is running on (think of thin clients, more
+    specifically LTSP terminals)."""
+
+    def __init__(self, host, port):
+        BaseSerialDevice.__init__(self)
+        try:
+            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            self.socket.connect(host, port)            
+        except:
+           raise CommError("Could not communicate with printer."
+                           "Please check if you have network "
+                           "connectivity and if the printer is "
+                           "attached to the remote computer.")
+
+    def read(self, n_bytes=1):
+        try:
+            return self.socket.recv(n_bytes)
+        except:
+            raise CommError("Could not read data from network printer.")
+
+    def write(self, data):
+        try:
+            self.socket.send(data)
+        except:
+            raise CommError("Could not send data to network printer.")
+
+    def close(self):
+        self.socket.close()

Added: fiscalprinter/trunk/fiscalprinter/serialdevice.py
==============================================================================
--- (empty file)
+++ fiscalprinter/trunk/fiscalprinter/serialdevice.py	Fri Sep  9 11:21:53 2005
@@ -0,0 +1,79 @@
+# -*- Mode: Python; coding: iso-8859-1 -*-
+# vi:si:et:sw=4:sts=4:ts=4
+
+##
+## Fiscal Printer
+## Copyright (C) 2005 Async Open Source <http://www.async.com.br>
+## All rights reserved
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+## USA.
+##
+## Author(s):   Christian Reis              <kiko at async.com.br>
+##              Evandro Vale Miquelito      <evandro at async.com.br>
+##
+"""
+fiscalprinter/serialdevice.py:
+    
+    Base routines for serial devices
+"""
+
+import serial
+
+from fiscalprinter.basedevice import BaseSerialDevice
+from fiscalprinter.exceptions import CommError
+
+
+class SerialDevice(BaseSerialDevice):
+    """This class implements a SerialDevice using the pySerial package
+    available at http://pyserial.sourceforge.net/. The reasons for using
+    this instead of the original PosixSerial.py include :
+       * Multi-plattform
+       * Thread safe
+       * Timeouts (properly) implemented
+       * Readily available in Debian ;) """
+
+    # Optional read/write timeout
+    # Should be moved later to config file
+    _read_timeout  = 3
+    _write_timeout = 3
+
+    def __init__(self, device, baudrate):
+        BaseSerialDevice.__init__(self)
+        try:
+            # serial.Serial wants a integer
+            self.port = serial.Serial(port=device,
+                                      baudrate=baudrate, 
+                                      timeout=self._read_timeout)
+            self.port.setDTR(1)
+            self.port.setWriteTimeout(self._write_timeout)
+        except:
+            raise CommError('Could not *open* printer port.')
+
+    def read(self, n_bytes=1):
+        try:
+            return self.port.read(size=n_bytes)
+        except:
+            raise CommError('Could not *read* data from printer.')
+
+
+    def write(self, data):
+        try:
+            self.port.write(data)
+        except:
+            raise CommError('Could not *send* data to printer.')
+
+    def close(self):
+        self.port.close()


More information about the POS-commit mailing list