Skip to content

Instantly share code, notes, and snippets.

@TannerRayMartin
Created December 28, 2017 11:47
Show Gist options
  • Select an option

  • Save TannerRayMartin/f0b91c5e492b1f15698656486403ef02 to your computer and use it in GitHub Desktop.

Select an option

Save TannerRayMartin/f0b91c5e492b1f15698656486403ef02 to your computer and use it in GitHub Desktop.

Revisions

  1. TannerRayMartin created this gist Dec 28, 2017.
    88 changes: 88 additions & 0 deletions serial_test1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    __author__ = 'Tanner Ray Martin'

    #this is for python 3, for python 2: change input to raw_input
    import serial

    #serial is the main module used and needs to be installed

    import time

    '''
    I have a cisco 2960-s series switch hooked up to a laptop through a serial port so think of 'ser' as 'switch'
    '''

    #creating your serial object
    ser = serial.Serial(
    port = 'COM3', #COM is on windows, linux is different
    baudrate=9600, #many different baudrates are available
    parity='N', #no idea
    stopbits=1,
    bytesize=8,
    timeout=8 #8 seconds seems to be a good timeout, may need to be increased
    )

    #open your serial object
    ser.isOpen()

    #in this case it returns str COM3
    print(ser.name)

    #first command (hitting enter)
    command = '\r\n'

    #convert str to binary (commands sent to switch must be binary)
    command = str.encode(command)

    #send the command to the switch
    ser.write(command)

    #wait a sec
    time.sleep(0.5)
    ser.inWaiting()

    #get the response from the switch
    input_data = ser.read(225) #(how many bytes to limit to read)

    #convert binary to str
    input_data = input_data.decode("utf-8", "ignore")

    #print response
    print(input_data)

    #create a loop
    while 1:
    #enter your own command
    command = input(':: ')

    #type 'exit' to end serial session with the switch
    if command == 'exit':
    ser.close()
    exit()

    else:
    #convert command to binary
    command = str.encode(command + '\r\n')

    #send command
    ser.write(command)

    #set response variable (empty binary str)
    out = b''

    #take a short nap
    time.sleep(.5)

    #while response is happening (timeout hasnt expired)
    while ser.inWaiting() > 0:

    #trying to read one line at a time (100 bytes at a time)
    out = ser.readline(100)

    #converting response to str
    out = out.decode("utf-8", "ignore")

    #printing response if not empty
    if out != '':
    print(out)

    #repeat until timeout