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

<p>Example:</p>

<pre><code>echo M106 &gt; /dev/ttyUSB0
</code></pre>

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

<pre><code>stty -F /dev/ttyUSB0 250000
</code></pre>

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

<pre><code>stty: invalid argument ‘250000’
</code></pre>

[{'answer_id': 3115, 'author': 'Carl Witthoft', 'author_id': 2191, 'author_profile': 'https://3dprinting.stackexchange.com/users/2191', 'pm_score': 1, 'selected': False, 'text': '<p><a href="https://ubuntuforums.org/showthread.php?t=1609727" rel="nofollow noreferrer">This forum page</a>  strongly suggests you should be using <code>setserial</code> for a port, not <code>stty</code> , which is for terminals.  I\'d give the code snippets there a try.\nAlternatively, <a href="https://stackoverflow.com/questions/12646324/how-to-set-a-custom-baud-rate-on-linux">stackoverflow</a> has a similar discussion, with somewhat more complicated modifications.</p>\n\n<p>Are you sure you can\'t talk with your printer at a lower baud rate than the maximum capability of the printer-end? </p>\n'}, {'answer_id': 3117, 'author': 'Mtl Dev', 'author_id': 4965, 'author_profile': 'https://3dprinting.stackexchange.com/users/4965', 'pm_score': 4, 'selected': True, 'text': '<p>For direct low-level printer control from a terminal, without specific software, I found the following solution with full credit thanks to user: <a href="http://stackexchange.com/users/6463673/meuh">http://stackexchange.com/users/6463673/meuh</a></p>\n\n<p>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.</p>\n\n<hr>\n\n<p>Step 1)\nCreate a custom python script that allows you to set arbitrary baud rates (make executable with <code>chmod u+x</code>).</p>\n\n<pre><code>#!/usr/bin/python\n# set nonstandard baudrate. Original Question: http://unix.stackexchange.com/a/327366/119298\nimport sys,array,fcntl\n\n# from /usr/lib/python2.7/site-packages/serial/serialposix.py\n# /usr/include/asm-generic/termbits.h for struct termios2\n#  [2]c_cflag [9]c_ispeed [10]c_ospeed\ndef set_special_baudrate(fd, baudrate):\n    TCGETS2 = 0x802C542A\n    TCSETS2 = 0x402C542B\n    BOTHER = 0o010000\n    CBAUD = 0o010017\n    buf = array.array(\'i\', [0] * 64) # is 44 really\n    fcntl.ioctl(fd, TCGETS2, buf)\n    buf[2] &amp;= ~CBAUD\n    buf[2] |= BOTHER\n    buf[9] = buf[10] = baudrate\n    assert(fcntl.ioctl(fd, TCSETS2, buf)==0)\n    fcntl.ioctl(fd, TCGETS2, buf)\n    if buf[9]!=baudrate or buf[10]!=baudrate:\n        print("failed. speed is %d %d" % (buf[9],buf[10]))\n        sys.exit(1)\n\nset_special_baudrate(0, int(sys.argv[1]))\n</code></pre>\n\n<hr>\n\n<p>Step 2) Run the script to set your baud rate.<br>\n<code>./set_custom_baud_rate.py &lt;&gt; /dev/ttyUSB0 250000</code></p>\n\n<hr>\n\n<p>Step 3) You can now monitor your printer output in a terminal window simply with:<br>\n <code>tail -f /dev/ttyUSB0</code></p>\n\n<hr>\n\n<p>Step 4) And finally, open up a new terminal window, and you can directly send M or Gcode to your printer; example:<br>\n<code>echo "M115" &gt;&gt; /dev/ttyUSB0</code></p>\n'}, {'answer_id': 3131, 'author': 'Lars Pötter', 'author_id': 204, 'author_profile': 'https://3dprinting.stackexchange.com/users/204', 'pm_score': 3, 'selected': False, 'text': '<p>you can use screen for that. Open a terminal window and type  <code>screen /dev/ttyUSB0 115200</code> The general form is  <code>screen serialdevice baudrate</code> You will then see everything that the printer sends. Everything you type will be send to the printer.</p>\n'}, {'answer_id': 11998, 'author': 'Александр Николаев', 'author_id': 20090, 'author_profile': 'https://3dprinting.stackexchange.com/users/20090', 'pm_score': -1, 'selected': False, 'text': '<p>That\'s work solution:\n<div class="youtube-embed"><div>\r\n                <iframe width="640px" height="395px" src="https://www.youtube.com/embed/1kcvIRhXkCo?start=0"></iframe>\r\n            </div></div>\nyou need 2 terminal, one for in and another for out flows </p>\n'}, {'answer_id': 14387, 'author': 'Jay Dee', 'author_id': 23499, 'author_profile': 'https://3dprinting.stackexchange.com/users/23499', 'pm_score': 2, 'selected': False, 'text': "<p>I spent a number of hours trying to work out why my Creality Ender Pro was ignoring me when I sent it serial data over USB from a Python app. Google searches trying to find a solution that were not talking about <code>encode()</code> and <code>decode()</code> all seemed to lead me to this thread.</p>\n<p>I installed Minicom as per the video link, and all worked as it should, but I still failed to receive a reply in my Python implementation, I could connect and read the serial sent from the printer but nothing append when sending back to it.</p>\n<p>After a few hours scratching my head I realised that the printer reacted to input as it received each new line <code>\\n</code> character that I was failing to add to the end of each command sent so the printer was just sitting waiting for me to finish transmitting my line end that never came.</p>\n<pre><code>self.serialPort = serial.Serial('/dev/ttyUSB0', 115200, exclusive = True)\nlines = self.serialPort.read(self.serialPort.in_waiting).decode().splitlines(False)\nline = line + &quot;\\n&quot;\nself.serialPort.write(line.encode())\n</code></pre>\n<p>Hopefully no one else is as stupid but to save anyone else like me time, I would like to point this out here.</p>\n"}, {'answer_id': 14743, 'author': 'K Mmmm', 'author_id': 10369, 'author_profile': 'https://3dprinting.stackexchange.com/users/10369', 'pm_score': 2, 'selected': False, 'text': '<p>The accepted answer didn\'t work for me. Everything received on the serial port was repeated back to the serial port, sending the printer into a confusing loop. There is more information about that in this answer: <a href="https://unix.stackexchange.com/questions/42964/unexpected-results-testing-serial-loopback-using-echo-and-cat">https://unix.stackexchange.com/questions/42964/unexpected-results-testing-serial-loopback-using-echo-and-cat</a></p>\n<p>The key is to use a command like this to prevent certain line termination characters from repeating themselves: <code>stty 115200 -F /dev/ttyUSB0 -echo -onlcr </code>, where 115200 is your baud rate.</p>\n<p>Here is what I did:</p>\n<pre><code># 1. plug in printer\nsudo chmod +777 /dev/ttyUSB0 # 2. allow access to printer USB permissions, add user to dialout or tty is better\nstty 115200 -F /dev/ttyUSB0 -echo -onlcr # 3. set serial port baud rate, might be 250000 for you\ncat -v &lt; /dev/ttyUSB0 # 4. get printer output \n\n</code></pre>\n<p>Then in a new terminal:</p>\n<pre><code>   echo &quot;M119&quot; &gt;&gt; /dev/ttyUSB0\n\n</code></pre>\n<p>This will give you a real console.</p>\n'}]
['https://3dprinting.stackexchange.com/questions/3112', 'https://3dprinting.stackexchange.com', 'https://3dprinting.stackexchange.com/users/4965/']