简体   繁体   中英

No output from pyserial

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!

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

Edit2: 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)

The full code:

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

Please try this instead:

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)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM