Epson Nadeldrucker einzelne Buchstaben drucken?
Hey Leute!
Ich versuche aus einem Raspberry Pi 4 und meinem EPSON LQ Nadeldrucker eine "Schreibmaschiene" zu bauen. Ist es möglich, statt Line-By-Line jeden Buchstaben den ich eingebe einzeln zu drucken? Ich komme nämlich nicht weiter.
Mein aktuelles Script:
import os
import sys
import termios
import tty
# Pfad zum USB-Drucker
DRUCKER_USB_PORT = '/dev/usb/lp0'
# Funktion, um eine Taste direkt ohne ENTER zu lesen
def lese_taste():
fd = sys.stdin.fileno()
alte_einstellungen = termios.tcgetattr(fd)
try:
tty.setraw(fd)
taste = sys.stdin.read(1) # Liest genau 1 Zeichen
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, alte_einstellungen)
return taste
# Verbindung zum Drucker öffnen
if not os.path.exists(DRUCKER_USB_PORT):
print(f"Der Drucker wurde nicht unter {DRUCKER_USB_PORT} gefunden.")
sys.exit(1)
try:
with open(DRUCKER_USB_PORT, 'wb', buffering=0) as drucker: # Ungepufferter Modus
print("Schreibmaschinen-Modus aktiv. Tippe, um zu drucken. Drücke 'Strg+C' zum Beenden.")
while True:
taste = lese_taste() # Zeichen sofort lesen
if ord(taste) == 3: # Strg+C beenden
break
elif ord(taste) == 13: # ENTER (Carriage Return)
drucker.write(b'\r\n') # Zeilenumbruch senden
else:
drucker.write(taste.encode('ascii')) # Zeichen sofort senden
drucker.flush() # Sicherstellen, dass die Daten sofort geschrieben werden
except PermissionError:
print(f"Keine Berechtigung für Zugriff auf {DRUCKER_USB_PORT}.")
print("Führe das Skript mit 'sudo' aus oder ändere die Berechtigungen:")
print(f" sudo chmod 666 {DRUCKER_USB_PORT}")
except Exception as e:
print(f"Ein Fehler ist aufgetreten: {e}")
finally:
print("Verbindung zum Drucker geschlossen.")
2 Antworten
Das "lp" steht für "Line Printer", d.h. der Drucker druckt Zeile für Zeile.
Gibt es alternativ auch ein "tty" Gerät? Das wäre dann eine Teletypewriter mit Ausgabe im gewünschten Fernschreiber-Stil.
pi@nas:~ $ ls /dev
autofs char full input loop3 media2 null ram11 ram5 sg0 tty1 tty17 tty24 tty31 tty39 tty46 tty53 tty60 uhid vcs vcsa1 vcsu1 vhost-net video16 watchdog
block console fuse kmsg loop4 media3 port ram12 ram6 shm tty10 tty18 tty25 tty32 tty4 tty47 tty54 tty61 uinput vcs1 vcsa2 vcsu2 vhost-vsock video18 watchdog0
bsg cpu_dma_latency gpiochip0 kvm loop5 mem ppp ram13 ram7 snd tty11 tty19 tty26 tty33 tty40 tty48 tty55 tty62 urandom vcs2 vcsa3 vcsu3 video10 video19 zero
btrfs-control cuse gpiochip1 log loop6 mmcblk0 ptmx ram14 ram8 stderr tty12 tty2 tty27 tty34 tty41 tty49 tty56 tty63 usb vcs3 vcsa4 vcsu4 video11 video20
bus disk gpiochip4 loop-control loop7 mmcblk0p1 pts ram15 ram9 stdin tty13 tty20 tty28 tty35 tty42 tty5 tty57 tty7 v4l vcs4 vcsa5 vcsu5 video12 video21
cachefiles dma_heap gpiomem loop0 mapper mmcblk0p2 ram0 ram2 random stdout tty14 tty21 tty29 tty36 tty43 tty50 tty58 tty8 vc-mem vcs5 vcsa6 vcsu6 video13 video22
cec0 dri hwrng loop1 media0 mqueue ram1 ram3 rfkill tty tty15 tty22 tty3 tty37 tty44 tty51 tty59 tty9 vchiq vcs6 vcsm-cma vga_arbiter video14 video23
cec1 fd initctl loop2 media1 net ram10 ram4 sdb tty0 tty16 tty23 tty30 tty38 tty45 tty52 tty6 ttyprintk vcio vcsa vcsu vhci video15 video31
Das ist alles unter /dev
Mein Ziel ist es auch, einen Fernschreiber zu reproduzieren, da ein Kollege von mir einen besitzt. Jedoch komme ich mit dem Line-By-Line Druck nicht auf das gewünschte Ergebnis.
Wie zalto schon sagte, schreibst Du ein ein Gerät, welches zeilenweise druckt!
Alle Devices, welche ein tty im Namen tragen sind aber Geräte, welche Zeichenweise arbeiten. Daher musst Du eines dieser Geräte verwenden. Du hast eine große Auswahl, da Linux auf Unix basiert und Unix aus einer Zeit stammt, als man noch mit seriellen Terminals gearbeitet hat.
Wichtig ist nur, dass Deine TTY Schnittstelle die richtigen Parameter zum Drucker verwendet - Geschwindigkeit in Baud, Start Bits, Stop Bits, Parität.