简体   繁体   English

如何使用python从树莓派向arduino发送字符串

[英]How to send string to arduino from raspberry pi using python

I have an Arduino microcontroller (Adafruit feather M0), and if I have it plugged into my windows PC and open the serial monitor, I can type P 0 2 1 0 and then enter , the Arduino interprets this command and works as expected.我有一个 Arduino 微控制器(Adafruit Feather M0),如果我将它插入 Windows PC 并打开串行监视器,我可以输入P 0 2 1 0然后enter ,Arduino 会解释此命令并按预期工作。 Now I want to plug in the same device into my raspberry pi, and have python do this instead.现在我想将同一个设备插入我的树莓派,让 python 来代替。 I have done this in the past on a different microcontroller (sparkfun pro micro) and it worked great.我过去在不同的微控制器(sparkfun pro micro)上做过这个,效果很好。 I can't seem to get it to work and am not sure what I am missing.我似乎无法让它工作,也不确定我错过了什么。

python code:蟒蛇代码:

import serial
import time

ser = serial.Serial('/dev/ttyACM0', 115200, timeout = None)
time.sleep(2)

returnKey = "\r\n"
ser.write(str.encode("P 2 1 0"))
ser.write(str.encode(returnKey)

The code finishes, but the Arduino simply does nothing, as if it discarded the command or didn't get it at all.代码完成了,但 Arduino 什么也不做,就好像它丢弃了命令或根本没有得到它。 The serial speed is matching on the Arduino too (115200). Arduino 上的串行速度也匹配(115200)。

I also tried:我也试过:

ser.write(b'P 2 1 0')
ser.write(returnKey.encode())

Any ideas what I am missing here?任何想法我在这里缺少什么?

Solved it.解决了。 Ended up using结束使用

import serial
import time

ser = serial.Serial('/dev/ttyACM0', 115200, timeout = None)
time.sleep(2)

ser.write(b'P 2 1 0 \r\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将带有 Python 的树莓派字符串发送到 Arduino 以启动 LED 灯条。 怎么把select正确的USB端口? - Send string from raspberry pi with Python to Arduino for starting led strip. How to select the right USB port? Raspberry pi不会使用minicom或python将串行数据发送到arduino - Raspberry pi will not send serial data to an arduino using either minicom or python 如何通过 USB 电缆将消息从 Raspberry Pi 发送到 Arduino - How send messages from Raspberry Pi to Arduino via USB cable 使用 python 将 CSV 从树莓派 pi3 发送到树莓派 3 - Send CSV from raspberry pi3 to raspberry pi 3 with python 具有Python的Raspberry Pi Arduino串行 - Raspberry Pi Arduino Serial with Python 如何使用Python将Hex数据发送到Raspberry Pi串行端口 - How to send Hex Data to Raspberry Pi Serial Port using Python 如何将数据从python raspberry pi发送到mssql服务器? - how to send data to to mssql server from python raspberry pi? 如何使用 python 将实时数据从计算机(PC)发送到 Raspberry pi 4? - How to send Live Data from Computer(PC) to Raspberry pi 4 using python? 通过XBee模块将Raspberry Pi中的字符串发送到Arduino - Sending a String from Raspberry Pi to Arduino via XBee modules 使用Python模块SMBus,Raspberry Pi和Arduino输入/输出错误 - Input/output error using Python module SMBus, a Raspberry Pi and an Arduino
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM