2

I have managed it to write from my arduino (Uno) to my Raspberry Pi 3 via Serial.

If I use the same python script on the pi side, and the same Sketch on arduino side, but using a Teensy instead, I cant read any output from my Arduino.

Is there any difference between thes arduino Uno and the teensy depending on Serial communication?

Arduino sketch:

void setup() {
  Serial.begin(115200);
}

void loop() {
  delay(1000);
  Serial.println("10.7;0.7;FFFF:FFFF:FFFF:FFFF:");
}

Python script on my Pi:

import serial
ser=serial.Serial("/dev/ttyACM0",115200)
while True:
    print("Waiting for messages from arduino..");
    read_ser=ser.readline()
    print(read_ser)

This code works fine for my Arduino Uno, but not for my Teensy. ttyACM0 is correct in both cases.

OS on the Pi is ubuntu mate 16.04. I can see the output of both arduinos in the Arduino IDE.

I tried this with 3 different Teensies, so the hardware should not be the problem.

Any advices?

** ser.isOpen() is true

bytesToRead = ser.inWaiting() print(ser.read(bytesToRead)) makes no difference.

Could there be a difference, because the teensy is connected with the pi with micro usb, and the UNO is connected with an A to B USB?

13
  • remove this line: ser.baudrate=115200 Commented Nov 3, 2017 at 17:20
  • 1
    execute print(ser.isOpen()) inside the loop. Commented Nov 3, 2017 at 17:36
  • 1
    You can't read data on teensy if haven't an hardware opening procedure Probable missing points : handshake, additional data for open bus. Dump bus data and you will be see python and terminal data different. Commented Nov 7, 2017 at 10:17
  • 1
    Arduino have low speed usb-ser but teensy(3.X) got 6Mb/s(i tested) bus. Some high speed devices required additional data before start a communication. Commented Nov 7, 2017 at 10:41
  • 1
    Don't do this @MoritzSchmidt , unidirectional communication most awful things on communicate with a device. On teensy speed definition Serial.begin(115200); or Serial.begin(0); or Serial.begin(12345678); not important because got 12Mb/s connection if used native port. Commented Nov 7, 2017 at 18:26

1 Answer 1

3
+50

Teensy is an ACM device ? YES !

Got additional BULK IN and BULK OUT ENDPOINTS(Interrupt_dev = 0, CDC_dev=1)

ACM device

Your Teensy code is very bad (Why calculate and send data if don't need ?)!

For test program like this :

unsigned long s = 0;
void setup(){
    Serial.begin(0);//Not important !(speed 12Mb/s)
    }

void loop(){
    if(Serial.available() > 0){
    while(Serial.available() > 0){//Buffer memory must always be clean !
        char read = Serial.read();
        delay(1);//wait until next_char
        }
    Serial.print("TEST : ");
    Serial.println(s, DEC);
    s++;
    }
}

Python Code :

import thread
import time
time.sleep(20)
#Don't fight with the Kernel, wait some seconds for prepare device

class _CDC :
    def __init__(self):
        self.dev = "/dev/ttyACM0"
        self.query = ""
    def read(self,_passarg):
        with open("/dev/ttyACM0","r") as readBuff:
            while True :
                ans = readBuff.readline()
                if ans:
                    print ans[:-2]#Ignore "\r\n" parts ! 
                #time sleep for save cpu clocks
                time.sleep(0.001)
    def write(self,_passarg):
        with open("/dev/ttyACM0","a") as writeBuff:
            while True :
                if self.query != "" :
                    writeBuff.write(self.query+"\n")
                    self.query = ""
                #time sleep for save cpu clocks
                time.sleep(0.001)

CDC = _CDC()
thread.start_new_thread(CDC.read,(None,))
thread.start_new_thread(CDC.write,(None,))

for i in range(30):
    q = "SEND-TEST%02d"%i
    CDC.query = q+((64-len(q))*"\x00")
    time.sleep(0.1)

Can read write (like a file object) if device is ACM. Open device with "r" and "a" mode for reading last-line.

Sign up to request clarification or add additional context in comments.

1 Comment

I am also having trouble reading and writing to a Teensy from a Raspberry Pi. Can you give an example of using the class _CDC? I am confused by your code. What is the arg _passarg for in read and write? Can you give an example of reading and writing like a file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.