Virtual serial port connection problem (/tmp/printer)

Hello!

I would like to send commands from my python program to Klipper via /tmp/printer file. I’m trying to use pyserial library, but it don’t work at all.

What I can do:
I can connect correctly using pyserial miniterm with different baudrates (eg. 9600, 38400, 250000). In this case I can home, move, etc.

What I can’t do:

  1. Connect using python’s terminal.
>>> import serial
>>> ser = serial.Serial('/tmp/printer', 38400)
>>> ser.is_open
True
>>> ser
Serial<id=0xf6f66f70, open=True>(port='/tmp/printer', baudrate=38400, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
>>> ser.write(b'G28 X')
5
>>> ser.close()

Nothing happened.

  1. Connect using my app
def send_gcode(gcode):
    # Send gcode in bytes
    printer_serial.open()
    time.sleep(2)
    print(printer_serial)
    printer_serial.write(gcode.encode())
    print('Command sended')
    line = printer_serial.readline()
    print(line)
    printer_serial.close()

No reaction…

Question:
What should I do to run this? I have tried with setDTR and setRTS but it raises error:

>>> ser.setDTR(True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/.local/lib/python3.11/site-packages/serial/serialutil.py", line 603, in setDTR
    self.dtr = value
    ^^^^^^^^
  File "/home/pi/.local/lib/python3.11/site-packages/serial/serialutil.py", line 473, in dtr
    self._update_dtr_state()
  File "/home/pi/.local/lib/python3.11/site-packages/serial/serialposix.py", line 713, in _update_dtr_state
    fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
OSError: [Errno 25] Inappropriate ioctl for device

What about baudrate? I can’t find information about correct value. Is virtual serial port baudrate adaptive?

Maybe GitHub - unjordy/klipper-repl: The missing Klipper command line. A CLI G-Code REPL for 3D printers running on Klipper firmware. can give you some hints.

Solution - I have to add ‘\n’ at the end of command. Now it works!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.