简体   繁体   English

如何在python中的串口上写ttyUSB0会被解释命令?

[英]How to write on serial port in python that ttyUSB0 will be interpreted commands?

I have raspberry PI B+ with connected Telegesis ZigBee module(ETRX3 USB sticks) via USB. 我有覆盆子PI B +通过USB连接Telegesis ZigBee模块(ETRX3 USB记忆棒)。 Using commands: 使用命令:

    debian:~# stty -F /dev/ttyUSB0 -raw ispeed 19200 ospeed 19200
    debian:~# cat < /dev/ttyUSB0 &
    debian:~# echo "ATI" > /dev/ttyUSB0

the ZigBee module executed ATI command and I can see the correct output: ZigBee模块执行ATI命令,我可以看到正确的输出:

   Telegesis ETRX357

   R308C

   OK

The same thing I want to do with python script. 我想用python脚本做同样的事情。 I was written python script with code: 我写了python脚本代码:

    #!/usr/bin/env python

    # based on tutorials:
    #   http://www.roman10.net/serial-port-communication-in-python/
    #   http://www.brettdangerfield.com/post/raspberrypi_tempature_monitor_project/

    import serial, time

    SERIALPORT = "/dev/ttyUSB0"
    BAUDRATE = 19200

    ser = serial.Serial(SERIALPORT, BAUDRATE)

    ser.bytesize = serial.EIGHTBITS #number of bits per bytes

    ser.parity = serial.PARITY_NONE #set parity check: no parity

    ser.stopbits = serial.STOPBITS_ONE #number of stop bits

    #ser.timeout = None          #block read

    #ser.timeout = 0             #non-block read

    ser.timeout = 2              #timeout block read

    ser.xonxoff = False     #disable software flow control

    ser.rtscts = False     #disable hardware (RTS/CTS) flow control

    ser.dsrdtr = False       #disable hardware (DSR/DTR) flow control

    ser.writeTimeout = 0     #timeout for write

    print 'Starting Up Serial Monitor'

    try:
        ser.open()

    except Exception, e:
        print "error open serial port: " + str(e)
        exit()

    if ser.isOpen():

        try:
            ser.flushInput() #flush input buffer, discarding all its contents
            ser.flushOutput()#flush output buffer, aborting current output

            ser.write("ATI")
            print("write data: ATI")
            time.sleep(0.5)
            numberOfLine = 0

            while True:

                response = ser.readline()
                print("read data: " + response)

                numberOfLine = numberOfLine + 1
                if (numberOfLine >= 5):
                    break

            ser.close()

        except Exception, e:
            print "error communicating...: " + str(e)

    else:
        print "cannot open serial port "

and get results as on the screen 并在屏幕上获得结果

    ATI

but I want to command be execute by ZigBee module, as like in shell commands. 但我希望命令由ZigBee模块执行,就像在shell命令中一样。 What am I doing wrong? 我究竟做错了什么?

写在ttyUSB0上

  1. you need to append an end-of-line to your write() 你需要在你的写()附加一行结尾

    ser.write("ATI\\r\\n")

  2. you should change the timeout to: 你应该将超时更改为:

    ser.timeout = None

Otherwise readline() will return after 2 seconds, even if nothing has been read. 否则readline()将在2秒后返回,即使没有读取任何内容。

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

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