How do I specify a serial port in the following python script using sys.argv and serial?
Question: I am relatively new to python, and am trying to specify a bluetooth serial
port to be used with a script I obtained from GitHub
(<https://github.com/ShimmerResearch/tinyos-
shimmer/blob/e04d83d9df615fc5f49f43765642cd59e979503e/apps/SimpleGSR/simpleGsr.py>).
I am on a mac and want to specify a bluetooth device whose port looks like
this: "/dev/tty.XXXX-XXX-XXX". So far all of my attempts result in the "no
device specified" error provided by the program. How to I embed my serial port
path into this script?

    
    
    #!/usr/bin/python
    import sys, struct, array, time, serial
    
    def wait_for_ack():
       ddata = ""
       ack = struct.pack('B', 0xff) 
       while ddata != ack:
       ddata = ser.read(1)
    return
    
    if len(sys.argv) < 2:
       print "no device specified"
       print "You need to specifiy the serial port of the shimmer you wish to connect to"
       print "example:"
       print "  getBtStreamVersion.py Com5"
       print " or"
       print "  getBtStreamVersion.py /dev/rfcomm0"
    
       print
    else:
    ser = serial.Serial(sys.argv[1], 115200)
    ser.flushInput()
    

Thanks for your help and sharing your expertise!


Answer: Pass it as an argument into the script when you run it, like this: `python
getBtStreamVersion.py /dev/tty.XXXX-XXX-XXX`

Or, if you want to hard-code your serial port into the program (not
recommended), you can replace the following:

    
    
    if len(sys.argv) < 2:
       print "no device specified"
       print "You need to specifiy the serial port of the shimmer you wish to connect to"
       print "example:"
       print "  getBtStreamVersion.py Com5"
       print " or"
       print "  getBtStreamVersion.py /dev/rfcomm0"
    
       print
    else:
        ser = serial.Serial(sys.argv[1], 115200)
        ser.flushInput()
    

with this:

    
    
    ser = serial.Serial("/dev/tty.XXXX-XXX-XXX", 115200)
    ser.flushInput()
    

sys.argv[1] is just getting the first command-line argument that you're
passing to the script.

