Python reading and writing to tty

26,276

Solution 1

Well, I asked too soon. I hope someone benefits from this self answer.

So this works to read and write from both the emulator and the actual device. I am not exactly sure why, and would appreciate an explanation, but this does work in all of my tests

import serial

class VISA:
    def __init__(self, tty_name):
        self.ser = serial.Serial()
        self.ser.port = tty_name
        # If it breaks try the below
        #self.serConf() # Uncomment lines here till it works

        self.ser.open()
        self.ser.flushInput()
        self.ser.flushOutput()

        self.addr = None
        self.setAddress(0)

    def cmd(self, cmd_str):
        self.ser.write(cmd_str + "\n")
        sleep(0.5)
        return self.ser.readline()

    def serConf(self):
        self.ser.baudrate = 9600
        self.ser.bytesize = serial.EIGHTBITS
        self.ser.parity = serial.PARITY_NONE
        self.ser.stopbits = serial.STOPBITS_ONE
        self.ser.timeout = 0 # Non-Block reading
        self.ser.xonxoff = False # Disable Software Flow Control
        self.ser.rtscts = False # Disable (RTS/CTS) flow Control
        self.ser.dsrdtr = False # Disable (DSR/DTR) flow Control
        self.ser.writeTimeout = 2

    def close(self):
        self.ser.close()

Solution 2

You do not actually have to use any special module to read from TTY.
Option O_NOCTTY solved my problems with CDCACM example MCU app.
I'm sure it will work for you (as you work on Linux too).

#!/usr/bin/env python3

import io, os

tty = io.TextIOWrapper(
        io.FileIO(
            os.open(
                "/dev/ttyACM1",
                os.O_NOCTTY | os.O_RDWR),
            "r+"))

for line in iter(tty.readline, None):
    print(line.strip())
Share:
26,276
ignorance
Author by

ignorance

I studied physics and computer engineering with dreams of doing a PhD in high energy or condensed matter theory, but alas I sold out to the data science industry. My favorite papers, "A Theoretical Framework for Back-Propagation" by Yann le Cun "Information Theory and Statistical Mechanics" by E. T. Jaynes "The wavelet transform, time-frequency localization and signal analysis" by Ingid Daubechies "Equation of State Calculations by Fast Computing Machines" by Metropolis et al. "Stochastic Relaxation, Gibbs Distributions, and the Bayesian Restoration of Images" by Geman and Geman "Nonlinear Programming" by Kuhn and Tucker "The Generalized Simplex Method for Minimizing a Linear Form under Linear Inequality Restraints" by Dantzig et al. "Coherent States for Arbitrary Lie Group" by Perelomov "A discrete time multiresolution theory" by Rioul "Derivation of the Schrodinger Equation from Newtonian Mechanics" by Nelson "MCMC using Hamiltonian dynamics" by Radford M. Neal

Updated on September 03, 2020

Comments

  • ignorance
    ignorance over 3 years

    BACKGROUND: If you want, skip to the problem section

    I am working on a front end for test equipment. The purpose of the front end is to make it easier to write long test scripts. Pretty much just make them more human readable and writable.

    The equipment will be tested using a Prologix GPIB-USB Controller (see prologix.biz). We found a tutorial at http://heliosoph.mit-links.info/gpib-on-debian-linux-the-easy-way/ and did all of the steps, and it worked!

    As we don't have the test equipment yet, we wanted to write an emulator in Python using openpty. We do have the GPIB-USB Controller, just not what gets connected to that. I got the emulator working as a perfect replacement for the GPIB-USB. This means that I would follow the "GPIB on Debian ..." tutorial (above) and get output that I programmed the emulator to return. The input and output were done in the same manner as the tutorial just reading and writing to/from a pty device (ie /dev/pts/2) instead of the tty (ie /dev/ttyUSB0).

    Now that the emulator works, we want to write a front end that can be used to write scripts easily. The goal is to make a kind of macro system that writes a bunch of commands when we call a function.

    PROBLEM: exists using both the emulator and the device

    I am using the following Python functions to read, write, and open the tty/pty devices, but I am not getting the same result that I get if I just use echo and cat in bash.

    tty = os.open(tty_path, os.O_RDWR)
    os.read(tty, 100)
    os.write(tty, "++ver")
    

    for example, I would expect the following to be equivalent

    $ cat < /dev/pty/2 &   # According to the tutorial, this must be run in parallel
    $ echo "++ver" > /dev/pty/2
    Prologix GPIB Version 1.2.3.4 ...
    

    and

    tty = os.open("/dev/pyt/2", os.o_RDWR)
    os.read(tty, 100) # In separate Thread to be run in parallel
    os.write(tty, "++ver") # in main thread
    

    The output is very different, please explain why and how I can fix it.

    FULL CODE is here: http://pastebin.com/PWVsMjD7

  • Jmons
    Jmons about 7 years
    Could you clarify which serial.Serial you are using (missing an import?)
  • ignorance
    ignorance about 7 years
    I edited it. BE WARY!! This was 3 years ago so I might be wrong, but I just guessed given that python -c "import serial; print serial.Serial" works
  • Jmons
    Jmons about 7 years
    Thanks, I'm just looking for a solution which lets me talk to a tty which is a bluetooth connection to a device. - I don't think serial is a standard python lib though? is it the pySerial perhaps? There are a couple of things which don't make sense, but I'll keep plugging away ;)
  • Jmons
    Jmons about 7 years
    With pySerial, and removing self.setAddress(0) line, this works beautifully for my (admitadly hacky) script to talk to bluetooth, on a Mavericks based Mac in 2017. Thanks for your quick response! `