4
$\begingroup$

Should it be possible to directly send G-code to the printer serial connection using pipes under Linux?

Example:

echo M106 > /dev/ttyUSB0

My controller runs at 250000 baud, I have tried setting the TTY baud rate to 250 kBd with:

stty -F /dev/ttyUSB0 250000

But, unfortunately, this particular baud rate appears to be unsupported under Ubuntu, giving the error:

stty: invalid argument ‘250000’
$\endgroup$
5
$\begingroup$

For direct low-level printer control from a terminal, without specific software, I found the following solution with full credit thanks to user: http://stackexchange.com/users/6463673/meuh

Sharing here as may be of use to other users in the 3d Printing community, and I was unable to source a "complete" solution to this elsewhere.


Step 1) Create a custom python script that allows you to set arbitrary baud rates (make executable with chmod u+x).

#!/usr/bin/python
# set nonstandard baudrate. Original Question: http://unix.stackexchange.com/a/327366/119298
import sys,array,fcntl

# from /usr/lib/python2.7/site-packages/serial/serialposix.py
# /usr/include/asm-generic/termbits.h for struct termios2
#  [2]c_cflag [9]c_ispeed [10]c_ospeed
def set_special_baudrate(fd, baudrate):
    TCGETS2 = 0x802C542A
    TCSETS2 = 0x402C542B
    BOTHER = 0o010000
    CBAUD = 0o010017
    buf = array.array('i', [0] * 64) # is 44 really
    fcntl.ioctl(fd, TCGETS2, buf)
    buf[2] &= ~CBAUD
    buf[2] |= BOTHER
    buf[9] = buf[10] = baudrate
    assert(fcntl.ioctl(fd, TCSETS2, buf)==0)
    fcntl.ioctl(fd, TCGETS2, buf)
    if buf[9]!=baudrate or buf[10]!=baudrate:
        print("failed. speed is %d %d" % (buf[9],buf[10]))
        sys.exit(1)

set_special_baudrate(0, int(sys.argv[1]))

Step 2) Run the script to set your baud rate.
./set_custom_baud_rate.py <> /dev/ttyUSB0 250000


Step 3) You can now monitor your printer output in a terminal window simply with:
tail -f /dev/ttyUSB0


Step 4) And finally, open up a new terminal window, and you can directly send M or Gcode to your printer; example:
echo "M115" >> /dev/ttyUSB0

| improve this answer | |
$\endgroup$
  • $\begingroup$ Can I ask you how is possible that this script works? I don't understand why you use <>, at first I thought was a typo, but modifying the script and opening the serial directly doesn't modify its baudrate. $\endgroup$ – gipi Jul 13 '17 at 13:15
  • 1
    $\begingroup$ <> on unix opens a file read+write mode, without truncation, and creates the file if it doesn't already exist. i.e system call: open(filename, O_RDWR|O_CREAT) $\endgroup$ – Mtl Dev Jul 13 '17 at 13:56
  • 1
    $\begingroup$ For example, cat < foo.txt it will print the contents of foo.txt, or fail if foo.txt doesn't exist. Whereas cat <> foo.txt,will also print the contents if it exists, but will create the file if it doesn't already exist. $\endgroup$ – Mtl Dev Jul 13 '17 at 13:59
  • $\begingroup$ I know what <> does, but I don't understand why this works for the baudrate: you are setting the baudrate of the file descriptor 0, i.e. stdin not for the serial port. $\endgroup$ – gipi Jul 13 '17 at 15:42
  • $\begingroup$ My understanding is the baudrate is simply the first argument to the script, (i.e sys.argv[1]). If you were to modify the script and hardcode a baudrate, and/or open device using the python equiv. of open(filename, O_RDWR|O_CREAT), then I, too, would be surprised if that didn't work. $\endgroup$ – Mtl Dev Jul 13 '17 at 18:25
4
$\begingroup$

you can use screen for that. Open a terminal window and type screen /dev/ttyUSB0 115200 The general form is screen serialdevice baudrate You will then see everything that the printer sends. Everything you type will be send to the printer.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks! Though does that work with non standard baud rates e.g 250k? I suspect not (at least with Ubuntu/Mint), but that it would be possible to combine the above script, with screen command as per your suggestion. $\endgroup$ – Mtl Dev Jul 13 '17 at 14:02
1
$\begingroup$

This forum page strongly suggests you should be using setserial for a port, not stty , which is for terminals. I'd give the code snippets there a try. Alternatively, stackoverflow has a similar discussion, with somewhat more complicated modifications.

Are you sure you can't talk with your printer at a lower baud rate than the maximum capability of the printer-end?

| improve this answer | |
$\endgroup$
  • $\begingroup$ You can't lower Marlin's baudrate without recompiling the firmware, I believe. $\endgroup$ – Tom van der Zanden Dec 1 '16 at 15:23
  • $\begingroup$ @TomvanderZanden bummer :-( $\endgroup$ – Carl Witthoft Dec 1 '16 at 15:24
-2
$\begingroup$

That's work solution:

you need 2 terminal, one for in and another for out flows

| improve this answer | |
$\endgroup$
  • $\begingroup$ From help center: Provide context for links Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Without the video, in case of link rot, this answer has no value for the future, please describe how the video poster got access to the printer from Ubuntu, that would make a great answer! $\endgroup$ – 0scar Feb 18 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.