PySerial应用运行于外壳,通过不PY脚本 [英] PySerial App runs in shell, by not py script

查看:186
本文介绍了PySerial应用运行于外壳,通过不PY脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用pySerial以通过串行端口到我的Arduino的数据发送一个非常简单的Python脚本。当我在Python shell中执行此行由行,它工作得很好,但是当我把它放在一个py为文件,并尝试运行它,没有任何反应。虽然我的UART串行灯闪做。因此就越来越通过,但它是垃圾(我检查)。

I have a very simple python script that uses pySerial to send data over the serial port to my Arduino. When I execute this line-by-line in the python shell, it works just fine, but when I put it in a ".py" file, and try to run it, nothing happens. Though the serial lights on my UART do flash. So something is getting through, but it's garbage (I checked).

下面是一个简单的code。

Here is the simple code.

import serial

ser = serial.Serial('COM3',9600,timeout=.2)
ser.write('A')
ser.close()

我已经尝试添加睡觉,但似乎没有任何要修复它。任何想法?

I've already tried adding sleeps, but nothing seems to be fixing it. Any ideas?

推荐答案

好了,我想通了!

这是需要使用code这样在执行写入前:

It's necessary to use code like this before performing the write:

time.sleep(1)
ser.setDTR(level=0)
time.sleep(1)

否则,Arduino的自动在接收因某种原因串行连接重置。耶!

Otherwise, the arduino automatically resets upon receiving a serial connection for some reason. yay!

这篇关于PySerial应用运行于外壳,通过不PY脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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