简体   繁体   中英

Pyserial prints empty b ' ' from a USB serial port

I seem to be having an problem, when I run this Python 3.5 script It's for USB serial controlled device:

import serial
import time

ser1 = serial.Serial('/dev/tty.usbserial', 115200, timeout=0.1)

def setupMode():
    ser1.write(b'$PF,200\r\n')
    ser1.write(b'$PO,20\r\n')

setupMode()


def startMeasurments():
    ser1.write(b'$GO\r\n')

startMeasurments()

def checkUnit():
    ser1.write(b'$US\r\n')

checkUnit()

while True:
    data = ser1.read(9999)
    print ('Got:', data)

time.sleep(0.1)
ser1.close()

I get these results:

python maintest.py
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''
Got: b''

the frequency of the printed data seems correct, and when tested the command:

ser1.write(xxxxx)

it triggers the device and outputs the necessary data to manufacturer provided software, so it's working fine- just the python output seems not to be working. How could i tackle this?

Maybe this will work:

while True:
    data = ''
    while ser1.inWaiting()>0:
        data += ser1.read(1)
    if data:
        print ('Got:', data)

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