Skip to content

Commit

Permalink
Implements isPortAvailable() per FaradayRF#38
Browse files Browse the repository at this point in the history
Working towards detecting hardware and performing additional unit
testing, @lqdev made a great suggestion to detect the presence of a
serial port. I found it useful enough to include in the `faradayio`
library itself.
  • Loading branch information
kb1lqc committed Mar 1, 2018
1 parent 1c91385 commit ee5a0e9
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions faradayio/faraday.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import serial
import timeout_decorator

import serial.tools.list_ports


class Faraday(object):
"""A class that enables transfer of data between computer and Faraday
Expand Down Expand Up @@ -249,3 +251,25 @@ def __init__(self):
serial.serial_for_url(url=self._port,
timeout=self._timeout,
baudrate=self._baudrate)

def isPortAvailable(port='/dev/ttyUSB0'):
'''
Checks whether specified port is available.
Source code derived from @lqdev suggestion per #38
Args:
port: Serial port location i.e. 'COM1'. Default is /dev/ttyUSB0
Returns:
available: Boolean value indicating presence of port
'''
isPortAvailable = serial.tools.list_ports.grep(port)

try:
next(isPortAvailable)
available = True
except StopIteration:
available = False

return available

0 comments on commit ee5a0e9

Please sign in to comment.