How to monitor a serial connection @ 250000 baud?

6,091

Solution 1

There are some undocumented ioctls you can use to set non-standard speeds, provided the driver implements them. A simple way to call them is with a small piece of python. Eg put in file mysetbaud.py and chmod +x it:

#!/usr/bin/python
# set nonstandard baudrate. 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]))

This takes some code from the pyserial package with constants for the various values needed from Linux C include files, and an array for the struct termios2. You use it with a baud rate parameter and your device on stdin, eg from bash:

./mysetbaud.py <>/dev/ttyUSB0 250000

Solution 2

setserial -a /dev/ttyUSB0 spd_cust
setserial -a /dev/ttyUSB0 divisor 96

Now set the port to 38400 to get 250000

stty -F /dev/ttyUSB0 38400

(or use it as 38400 in an application, e.g. ser2net)

Got my info from http://www.linurs.org/linux/SerialPort.html

Share:
6,091

Related videos on Youtube

Mtl Dev
Author by

Mtl Dev

PFY Graduate

Updated on September 18, 2022

Comments

  • Mtl Dev
    Mtl Dev almost 2 years

    I wish to directly monitor the serial-over-usb connection to my 3d printer, which runs at 250000 baud. e.g I might monitor it with cat /dev/ttyUSB0

    However first I need to set the baud rate, e.g stty -F /dev/ttyUSB0 115200

    But if I try and set the baud rate to 250k, it fails:

    stty -F /dev/ttyUSB0 250000

    gives result:

    stty: invalid argument 250000

    It appears that baud rate 250000 is not supported under Ubuntu/Mint. Can anyone suggest an alternative way to monitor this serial connection?

    • Mc Kernel
      Mc Kernel over 7 years
      Try screen /dev/ttyUSB0 250000, but afaik, you can only stablish one serial connection, so it's monitorization or 3d printer
    • hellork
      hellork over 2 years
      250000 is a non-standard bitrate, but stty -F /dev/ttyUSB0 500000 works. I couldn't get 250000 with stty, screen, setserial, python, or whatever else I tried. I eventually gave up, recompiled and set bitrate to 500000 in 3D printer firmware. It turned out to be easy. I get faster transfers. And the Configuration.h header file comment said it could even speed up the 3D printer's sd-card access.
    • Mtl Dev
      Mtl Dev over 2 years
      That's a great tip thanks. Just curious if you tried the below mysetbaud.py? It should allow you to set the baud rate to stty...250000. (not that you need to any more)
  • Mtl Dev
    Mtl Dev over 7 years
    Thankyou that worked perfectly! Amazing. I can now directly send Gcode to the printer, example: echo "M115" >> /dev/ttyUSB0 while monitoring the status in another terminal with just tail -f /dev/ttyUSB0
  • wmnitin
    wmnitin about 6 years
    This worked great for connecting to my 3D printer. I used the first two commands and then "screen /dev/ttyUSB0 38400". I can type to the printer and receive output, too.
  • bjornruffians
    bjornruffians about 5 years
    Would append that setserial -a /dev/ttyUSB0 displays Baud_base by which you can determine the divisor value to use.
  • Peter Mortensen
    Peter Mortensen over 2 years
    Why 96? 250000 / 38400 = 6.51. Can you elaborate?