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 :
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)