Giter VIP home page Giter VIP logo

Comments (1)

vmario89 avatar vmario89 commented on July 21, 2024

Some weeks later: i made a stable Python 3.7 script using PySerial library for polling out data. The following example code grabs ctrlmode and cuts out the mode number of the read line.

This script can be used to do things like sending values into an InfluxDB or whatever. It's stable enough to survive removed USB device, powered off Smart Stepper and other errors. I also configured it to set USB port to exclusive so no other connection can mess with the established one.

import serial
import time
import termios # workaround for IOError on resetting input/output buffers
import re

# note that when defining serial.Serial will automatically open the port!
ser=None
def createSerial():
        global ser
        time.sleep(1) # always wait one second before re-opening. This prevents hiccups
        try:
                #print("trying to open serial connection")
                ser=serial.Serial(
                        port="/dev/ttyUSB-SMART-A-AXIS",
                        baudrate=115200,
                        bytesize=serial.EIGHTBITS,
                        parity=serial.PARITY_NONE,
                        stopbits=serial.STOPBITS_ONE,
                        timeout=1,
                        xonxoff=False,
                        rtscts=False,
                        write_timeout=1,
                        dsrdtr=False,
                        inter_byte_timeout=None,
                        exclusive=True) # force to get behaviour "Cannot open line '/dev/ttyUSB-SMART-A-AXIS' for R/W: open() blocked, aborted." or "Error: read failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?)"

        except IOError as e: # the serial creation may fail if device not connected or not ready
                print("Error:", e)
                pass
while True:
        if ser == None:
                #print("serial is set to None. Creating new serial connection")
                createSerial()
        else:
                try:
                        ser.reset_input_buffer()
                        ser.reset_output_buffer()
                        ser.write("ctrlmode".encode() + b'\r')
                        #time.sleep(0.05)
                        while ser.inWaiting() > 0:
                                line=ser.readline().decode()
                                #print(line)
                                p=re.compile(r'^controller\s.*\(\d\)$', re.M)
                                m=p.search(line)
                                if m:
                                        print("ctrlmode =",m.group(0)[-2]) #extract the number from "controller <modeString> (<number>)"
                except (IOError, termios.error) as e: # in case the USB device was removed or buffer reset fails try to close and re-open serial port
                        print("Error:", e)
                        ser.close()
                        ser = None #unset
                        createSerial()
                        pass

from nano_stepper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.