diff --git a/examples/autoBaudrate.py b/examples/autoBaudrate.py new file mode 100644 index 00000000..fa81d622 --- /dev/null +++ b/examples/autoBaudrate.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +from brping import Ping1D +import time +import argparse + +##Parse Command line options +############################ + +parser = argparse.ArgumentParser(description="Ping python library example.") +parser.add_argument('--device', action="store", required=True, type=str, help="Ping device port.") +args = parser.parse_args() + +#Make a new Ping +myPing = Ping1D(args.device) + +baudrates = [9600, 115200] + +while (True): + for baudrate in baudrates: + if not myPing.initialize(baudrate): + print("failed to initialize at %d bps", baudrate) + exit(1) + print("ok") + time.sleep(0.01) diff --git a/generate/templates/device.py.in b/generate/templates/device.py.in index 9d6cb19f..e35dbad1 100644 --- a/generate/templates/device.py.in +++ b/generate/templates/device.py.in @@ -72,10 +72,19 @@ class PingDevice(object): return self.iodev.write(data) ## - # @brief Make sure there is a device on and read some initial data + # @brief initialize the device communication interface. A serial break signal + # is sent to the device followed by 'U' (0x55) to begin the autobaurate procedure + # on the device. Some data is then read from the device to verify communications + # + # @param baudrate: the baudrate to use. if None, the baudrate will not + # be changed (default is 115200) # # @return True if the device replies with expected data, False otherwise - def initialize(self): + def initialize(self, baudrate=None): + if baudrate is not None: + self.iodev.baudrate = baudrate + self.iodev.send_break() + self.iodev.write("U".encode("utf-8")) return self.request(definitions.COMMON_PROTOCOL_VERSION) is not None ## diff --git a/generate/templates/ping1d.py.in b/generate/templates/ping1d.py.in index 76de25cd..c3edaf8d 100644 --- a/generate/templates/ping1d.py.in +++ b/generate/templates/ping1d.py.in @@ -29,8 +29,8 @@ class Ping1D(PingDevice): return self.wait_message([m_id], timeout) - def initialize(self): - if not PingDevice.initialize(self): + def initialize(self, baudrate=None): + if not PingDevice.initialize(self, baudrate): return False if self.legacyRequest(definitions.PING1D_GENERAL_INFO) is None: return False