Teensy 3.5 detection problem with Raspberry Pi

Juno

Member
Hi everyone,

I have a Teensy 3.5 connected via USB to a Raspberry Pi. This Teensy sends data frames to the Raspberry via the serial port.

The Teensy is recognized by the Raspberry thanks to a predefined name specified in /etc/udev/rules.d/99-usb-serial.rules (e.g. SUBSYSTEM=="tty", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="0483", ATTRS{serial}=="4514812", SYMLINK+="NAME_OF_TEENSY").

The Raspberry runs a Python script when it starts up using the etc/rc.local file, which detects whether the Teensy in question is plugged in or not, and if so, reads and records the serial port in a .txt file.

My problem is that for everything to work properly with a Teensy 4.0, but when I'm using a Teensy 3.5, I have to disconnect and reconnect my Teensy to the Raspberry for it to start recording frames. If the Teensy was already plugged in before the Raspberry was launched, nothing is recorded in the text file. However, if you go to /dev, the Teensy appears under the name you gave it, and the Python script is running.

Thanks to everyone

Here is the Python script :

Code:
# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import time
import serial

from datetime import datetime
import os

dongle_removed=True
time_before_retry=30

#Verify if dongle is connected before launching
while dongle_removed == True : 
    try:
        #Search in /dev for the name of the Teensy
        dongle=(os.popen("ls /dev | grep NAME_OF_TEENSY").read())
    except:
        pass
    #If detected
    if dongle == "NAME_OF_TEENSY\n":
        dongle_removed=False

        ser = serial.Serial(
                port='/dev/NAME_OF_TEENSY',
                baudrate = 115200,
                parity=serial.PARITY_NONE,
                stopbits=serial.STOPBITS_ONE,
                bytesize=serial.EIGHTBITS,
                timeout=5,
                xonxoff=1
        )
    #If not detected, retry
    else:
        dongle_removed=True
        print("Dongle not detected")
        time.sleep(time_before_retry)

timestr = time.strftime("%Y%m%d-%H%M%S")
print(timestr)
f= open("/home/record"+  timestr + ".txt" ,"w+")

rec = True

while True:
    time.sleep(0.1)
    try:
        dongle=(os.popen("ls /dev | grep NAME_OF_TEENSY").read())
    except:
        pass
    
    if dongle == "NAME_OF_TEENSY\n":
        if(ser.isOpen() == False):
            ser.open()
            
        #Record in .txt file
        if rec == True :
            print ("run record")
            timestr = time.strftime("%Y%m%d-%H%M%S")
            print(timestr)
            f= open("/home/record/NAME_OF_TEENSY_"+  timestr + ".txt" ,"w+")
        else :
            print ("stop record")
            f.close()
        
        #Try to read serial
        try :
            receive_ser=""
            receive_ser = ser.readline()
            print(receive_ser)
        except Exception as e:
            print("Error reading serial : ",e)
            
        else :
            dt = datetime.now().utcnow()
            time_str = str(dt.hour).zfill(2) + str(dt.minute).zfill(2) + str(dt.second).zfill(2) + '.' + str(int(float(dt.microsecond)/1000)).zfill(3)
            rec_ch = time_str + "," + receive_ser[:-3] + "," ";\r\n"
            if rec == True :
                f.write(rec_ch + "\n")
                f.flush()     
    else:
        print("Dongle removed")
        ser.close()
        dongle_removed=True
        print("Dongle not detected")
        time.sleep(time_before_retry)
 
The Teensy is recognized by the Raspberry thanks to a predefined name specified in /etc/udev/rules.d/99-usb-serial.rules (e.g. SUBSYSTEM=="tty", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="0483", ATTRS{serial}=="4514812", SYMLINK+="NAME_OF_TEENSY").

Use of "ATTRS{serial}" restricts this udev rule to 1 specific Teensy board. If you plug in a different Teensy, even if it's the same Teensy model, it will have a different serial number which causes this overly specific udev rule to no longer apply!

I also don't quite understand how the serial number could be 4514812 (last digit of '2'). Teensy's core library always reports the serial number USB descriptor with '0' in the last digit, as a workaround for a bug in older MacOS.
 
Hi Paul, thanks for your reply,

Sorry for the confusion, I didn’t mention it but when I change the Teensy, I also change the Serial parameter with the corresponding number.

What's more, the number I've indicated here doesn't correspond to anything, I typed it in at random because I didn't have any to hand...

What surprises me in all this is that, whether for 3.5 or 4.0, the Teensy appears under the name I have given in /dev. But the Python script doesn't read anything from the serial port when the 3.5 is plugged into the Raspberry before power-up...

I've also tried with another new 3.5 and there's no change.
 
But the Python script doesn't read anything from the serial port when the 3.5 is plugged into the Raspberry before power-up...

Before you pour a lot more time into custom python code with DIY udev rules, only for the sake of troubleshooting I'd recommend testing with Arduino IDE serial monitor using the PJRC published udev rules. Make sure everything is working the normal supported way first.
 
Back
Top