pyserial 没有输出 [英] No output from pyserial

查看:76
本文介绍了pyserial 没有输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个带有 pyserial (v2.6) 的代码,它应该无限期地等待来自使用 inWaiting() 指定的端口的任何输入,然后使用 read() 读取它 但没有运气,也没有输出.我究竟做错了什么?该程序根本不打印任何内容!

I am trying to write a code wit pyserial (v2.6) that should wait indefinitely for any input from the port specified using inWaiting() and then read it using read() but there's no luck and no output at all. What am I doing wrong? The program just doesn't print anything at all!

程序和端口都在Contiki OS的虚拟机上运行

both the program and the port are running on a virtual machine of Contiki OS

Edit2:z1 mote 是连接到端口的设备.我发现 pyserial 用于写入它(我无法将 pyserial 升级到最新版本,因为它不会与 z1 motes 一起锻炼)

z1 mote is the device connected to the port. I found out that pyserial is used to write to it (I can't upgrade pyserial to the latest version because it won't workout with the z1 motes)

完整代码:

import pyserial
baudrate = 115200
port = '/dev/ttyUSB0' 
ser = serial.Serial(port,baudrate)

while 1:
    time.sleep(1)
    coming_data = ser.inWaiting()
    if coming_data != 0:
        data = ser.read(coming_data)
        print data

# the output from the port is (which should be the output of this program)
# abcd::abcd:0:0:c9 2293 6 -3 243 -23 108 
# abcd::abcd:0:0:c9 2337 8 -4 242 -27 108

推荐答案

请试试这个:

import serial
import sys
from time import sleep

try:
  ser = serial.Serial("/dev/ttyUSB0", 115200,timeout=0, parity=serial.PARITY_NONE, 
                        stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
except:
  sys.exit("Error connecting device")

while True:
  queue = ser.inWaiting()
  if queue > 0:
    data = ser.read(1000)
    print data
  sleep(0.2)

该问题可能与未按预期为 Zolertia Z1 节点配置所有 UART 设置有关.

The problem may be related to not configuring all the UART settings as expected for the Zolertia Z1 mote.

更新:请确保与端口的连接未被其他进程使用.因为如果它在别处打印,那么python脚本将无法读取数据.

Update: Please make sure that the connection to port is not used by another process. Because if it is printing elsewhere then it won't be able to read the data by the python script.

这篇关于pyserial 没有输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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