Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/kliment/Printrun.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2013-05-29 18:24:13 +0400
committerGuillaume Seguin <guillaume@segu.in>2013-05-29 18:24:13 +0400
commita42617a0b3a3b3eedf84e69d3e504e201ed7c37a (patch)
treeb4ec4875296ed624b1e847f68b80f1f4c4afc461 /printcore.py
parent94f2f97e69cd12a799e0a6429822090e68e15921 (diff)
Rework TCP support to make it closer to Serial support
This is mainly done by using the file-like interface of socket objects.
Diffstat (limited to 'printcore.py')
-rwxr-xr-xprintcore.py64
1 files changed, 30 insertions, 34 deletions
diff --git a/printcore.py b/printcore.py
index a703209..ae73e5b 100755
--- a/printcore.py
+++ b/printcore.py
@@ -19,9 +19,9 @@ from serial import Serial, SerialException
from threading import Thread
from select import error as SelectError, select
import time, getopt, sys
-import platform, os
-import socket # Network
-import re # Regex
+import platform, os, traceback
+import socket
+import re
from collections import deque
from printrun.GCodeAnalyzer import GCodeAnalyzer
from printrun import gcoder
@@ -40,9 +40,6 @@ def enable_hup(port):
def disable_hup(port):
control_ttyhup(port, True)
-def is_socket(printer):
- return (type(printer) == socket._socketobject)
-
class printcore():
def __init__(self, port = None, baud = None):
"""Initializes a printcore instance. Pass the port and baud rate to connect immediately
@@ -121,12 +118,27 @@ class printcore():
except:
pass
if not is_serial:
- self.printer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.printer_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.timeout = 0.25
- self.printer.connect((hostname, port))
+ try:
+ self.printer_tcp.connect((hostname, port))
+ self.printer = self.printer_tcp.makefile()
+ except socket.error:
+ print _("Could not connect to %s:%s:") % (hostname, port)
+ self.printer = None
+ self.printer_tcp = None
+ traceback.print_exc()
+ return
else:
disable_hup(self.port)
- self.printer = Serial(port = self.port, baudrate = self.baud, timeout = 0.25)
+ self.printer_tcp = None
+ try:
+ self.printer = Serial(port = self.port, baudrate = self.baud, timeout = 0.25)
+ except SerialException:
+ print _("Could not connect to %s at baudrate %s:") % (self.port, self.baud)
+ self.printer = None
+ traceback.print_exc()
+ return
self.stop_read_thread = False
self.read_thread = Thread(target = self._listen)
self.read_thread.start()
@@ -134,26 +146,19 @@ class printcore():
def reset(self):
"""Reset the printer
"""
- if self.printer and not is_socket(self.printer):
+ if self.printer and not self.printer_tcp:
self.printer.setDTR(1)
time.sleep(0.2)
self.printer.setDTR(0)
def _readline(self):
try:
- # Read line if socket
- if is_socket(self.printer):
- line = ''
- ready = select([self.printer], [], [], self.timeout)
- if ready[0]:
- while not "\n" in line:
- chunk = self.printer.recv(1)
- if chunk == '':
- raise RuntimeError("socket connection broken")
- line = line + chunk
- # Read if tty
- else:
+ try:
line = self.printer.readline()
+ if self.printer_tcp and not line:
+ raise OSError("Read EOF from socket")
+ except socket.timeout:
+ return ""
if len(line) > 1:
self.log.append(line)
@@ -176,7 +181,7 @@ class printcore():
return None
def _listen_can_continue(self):
- if is_socket(self.printer):
+ if self.printer_tcp:
return not self.stop_read_thread and self.printer
return not self.stop_read_thread and self.printer and self.printer.isOpen()
@@ -468,17 +473,8 @@ class printcore():
try: self.sendcb(command)
except: pass
try:
- # If the printer is connected via Ethernet, use send
- if is_socket(self.printer):
- msg = str(command+"\n")
- totalsent = 0
- while totalsent < len(msg):
- sent = self.printer.send(msg[totalsent:])
- if sent == 0:
- raise RuntimeError("socket connection broken")
- totalsent = totalsent + sent
- else:
- self.printer.write(str(command+"\n"))
+ self.printer.write(str(command + "\n"))
+ self.printer.flush()
except SerialException, e:
print "Can't write to printer (disconnected?)."
except RuntimeError, e: