Python 串行通信 Arduino (Teensy) 到 Raspberry Pi [英] Python Serial communication Arduino (Teensy) to Raspberry Pi

查看:23
本文介绍了Python 串行通信 Arduino (Teensy) 到 Raspberry Pi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法通过串行从我的 arduino (Uno) 写入到我的 Raspberry Pi 3.

如果我在 pi 端使用相同的 python 脚本,在 arduino 端使用相同的 Sketch,但使用青少年,我无法从我的 Arduino 读取任何输出.

arduino Uno 和 teensy 之间是否有任何区别,具体取决于串行通信?

Arduino 草图:

void setup() {Serial.begin(115200);}无效循环(){延迟(1000);Serial.println("10.7;0.7;FFFF:FFFF:FFFF:FFFF:");}

我的 Pi 上的 Python 脚本:

导入串口ser=serial.Serial("/dev/ttyACM0",115200)而真:print("正在等待来自arduino的消息..");read_ser=ser.readline()打印(read_ser)

此代码适用于我的 Arduino Uno,但不适用于我的 Teensy.ttyACM0 在两种情况下都是正确的.

Pi 上的操作系统是 ubuntu mate 16.04.我可以在 Arduino IDE 中看到两个 arduino 的输出.

我用 3 个不同的青少年试过这个,所以硬件应该不是问题.

有什么建议吗?

** ser.isOpen() 为真

bytesToRead = ser.inWaiting() print(ser.read(bytesToRead)) 没有区别.

会不会有区别,因为teensy是用micro usb接pi的,UNO是接一个

你的Teensy代码很糟糕(如果不需要,为什么要计算和发送数据?)!

对于这样的测试程序:

unsigned long s = 0;无效设置(){Serial.begin(0);//不重要!(速度12Mb/s)}无效循环(){如果(串行.可用()> 0){while(Serial.available() > 0){//缓冲存储器必须始终干净!字符读取 = Serial.read();delay(1);//等到next_char}Serial.print("测试:");Serial.println(s, DEC);s++;}}

Python 代码:

导入线程导入时间时间.睡眠(20)#不要和Kernel打架,等几秒准备设备_CDC 类:def __init__(self):self.dev = "/dev/ttyACM0"self.query = ""定义读取(自我,_passarg):使用 open("/dev/ttyACM0","r") 作为 readBuff:而真:ans = readBuff.readline()如果回答:打印 ans[:-2]#忽略\r\n"部分!#time sleep 以节省 CPU 时钟时间.睡眠(0.001)def write(self,_passarg):使用 open("/dev/ttyACM0","a") 作为 writeBuff:而真:如果 self.query != "" :writeBuff.write(self.query+"\n")self.query = ""#time sleep 以节省 CPU 时钟时间.睡眠(0.001)CDC = _CDC()thread.start_new_thread(CDC.read,(None,))thread.start_new_thread(CDC.write,(None,))对于范围内的我(30):q = "发送测试%02d"%iCDC.query = q+((64-len(q))*"\x00")时间.睡眠(0.1)

如果设备是ACM,则可以读写(如文件对象).使用r"和a"模式打开设备以读取最后一行.

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?

解决方案

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.

这篇关于Python 串行通信 Arduino (Teensy) 到 Raspberry Pi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆