问题
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?
回答1:
Teensy is an ACM device ? YES !
Got additional BULK IN and BULK OUT ENDPOINTS
(Interrupt_dev = 0, CDC_dev=1)
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.
来源:https://stackoverflow.com/questions/47101062/python-serial-communication-arduino-teensy-to-raspberry-pi