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?

Moritz Schmidt
  • 2,635
  • 3
  • 27
  • 51
  • remove this line: `ser.baudrate=115200` – eyllanesc Nov 03 '17 at 17:20
  • @eyllanesc thanks for the advice but this makes do difference. – Moritz Schmidt Nov 03 '17 at 17:35
  • 1
    execute `print(ser.isOpen())` inside the loop. – eyllanesc Nov 03 '17 at 17:36
  • @eyllanesc it is open, I also get an error if I remove the USB from my pi, while the scrpt is running ** It doesnt pass readline(), it hangs there – Moritz Schmidt Nov 03 '17 at 17:38
  • Change while loop to `bytesToRead = ser.inWaiting() print(ser.read(bytesToRead))` – eyllanesc Nov 03 '17 at 17:40
  • @eyllanesc also no difference :/ – Moritz Schmidt Nov 05 '17 at 13:17
  • 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? (this one) http://www.skyviewelectronics.com/sites/skyviewelectronics.com/files/usb-a-to-b-cable.jpg – Moritz Schmidt Nov 05 '17 at 13:21
  • 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. – dsgdfg Nov 07 '17 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. – dsgdfg Nov 07 '17 at 10:41
  • Thanks for your answer. If you know my next steps should be, It would be really nice if you could answer my question, so I can mark it as the correct answer :) – Moritz Schmidt Nov 07 '17 at 12:35
  • @dsgdfg You also know that i want to write from teensy to computer, not read? just to be sure :) – Moritz Schmidt Nov 07 '17 at 12:57
  • 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. – dsgdfg Nov 07 '17 at 18:26
  • So there is no solution? – Moritz Schmidt Nov 07 '17 at 18:50

1 Answers1

3

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.

dsgdfg
  • 1,492
  • 11
  • 18
  • 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? – Prof Huster Apr 08 '19 at 19:22