2013年1月10日 星期四

[Python] How to read Serial Date by using Python

[Python] How to read Serial Date by using Python

Enviroment :



Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)



Code :



#!/usr/bin/python

import time

import serial



locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3','/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3']



for device in locations:

    try:

        print "Trying...",device

        ser = serial.Serial(device,115200, bytesize=8, parity='N', stopbits=1,timeout=0)

        ser.open()

        ser.isOpen()

        break

    except:

        print "Failed to connect on",device



try:

    ser.write("cli ?"+'\r\n')

    time.sleep(0.5)



    # Read a line and convert it from b'xxx\r\n' to xxx



    line = ser.read(ser.inWaiting())

    if line:  # If it isn't a blank line

        print(line)







except:

    print "Failed to send!"



ser.close()



Reference :








Related Posts:

0 意見:

張貼留言