1.0.2 update example and handle serial disconnection gracefully

This commit is contained in:
geeksville
2020-09-11 11:08:16 -07:00
parent a4d40a209e
commit 6942677b85
3 changed files with 145 additions and 130 deletions

View File

@@ -52,10 +52,10 @@ If you want to see all packets, simply subscribe to "meshtastic.receive".</li>
<pre><code>import meshtastic
from pubsub import pub
def onReceive(packet): # called when a packet arrives
def onReceive(packet, interface): # called when a packet arrives
print(f&quot;Received: {packet}&quot;)
def onConnection(): # called when we (re)connect to the radio
def onConnection(interface, topic): # called when we (re)connect to the radio
# defaults to broadcast, specify a destination ID if you wish
interface.sendText(&quot;hello mesh&quot;)
@@ -102,10 +102,10 @@ type of packet, you should subscribe to the full topic name. If you want to see
import meshtastic
from pubsub import pub
def onReceive(packet): # called when a packet arrives
def onReceive(packet, interface): # called when a packet arrives
print(f&#34;Received: {packet}&#34;)
def onConnection(): # called when we (re)connect to the radio
def onConnection(interface, topic): # called when we (re)connect to the radio
# defaults to broadcast, specify a destination ID if you wish
interface.sendText(&#34;hello mesh&#34;)
@@ -537,50 +537,55 @@ class StreamInterface(MeshInterface):
&#34;&#34;&#34;The reader thread that reads bytes from our stream&#34;&#34;&#34;
empty = bytes()
while not self._wantExit:
b = self.stream.read(1)
if len(b) &gt; 0:
# logging.debug(f&#34;read returned {b}&#34;)
c = b[0]
ptr = len(self._rxBuf)
try:
while not self._wantExit:
b = self.stream.read(1)
if len(b) &gt; 0:
# logging.debug(f&#34;read returned {b}&#34;)
c = b[0]
ptr = len(self._rxBuf)
# Assume we want to append this byte, fixme use bytearray instead
self._rxBuf = self._rxBuf + b
# Assume we want to append this byte, fixme use bytearray instead
self._rxBuf = self._rxBuf + b
if ptr == 0: # looking for START1
if c != START1:
self._rxBuf = empty # failed to find start
if self.debugOut != None:
if ptr == 0: # looking for START1
if c != START1:
self._rxBuf = empty # failed to find start
if self.debugOut != None:
try:
self.debugOut.write(b.decode(&#34;utf-8&#34;))
except:
self.debugOut.write(&#39;?&#39;)
elif ptr == 1: # looking for START2
if c != START2:
self.rfBuf = empty # failed to find start2
elif ptr &gt;= HEADER_LEN: # we&#39;ve at least got a header
# big endian length follos header
packetlen = (self._rxBuf[2] &lt;&lt; 8) + self._rxBuf[3]
if ptr == HEADER_LEN: # we _just_ finished reading the header, validate length
if packetlen &gt; MAX_TO_FROM_RADIO_SIZE:
self.rfBuf = empty # length ws out out bounds, restart
if len(self._rxBuf) != 0 and ptr + 1 == packetlen + HEADER_LEN:
try:
self.debugOut.write(b.decode(&#34;utf-8&#34;))
except:
self.debugOut.write(&#39;?&#39;)
elif ptr == 1: # looking for START2
if c != START2:
self.rfBuf = empty # failed to find start2
elif ptr &gt;= HEADER_LEN: # we&#39;ve at least got a header
# big endian length follos header
packetlen = (self._rxBuf[2] &lt;&lt; 8) + self._rxBuf[3]
if ptr == HEADER_LEN: # we _just_ finished reading the header, validate length
if packetlen &gt; MAX_TO_FROM_RADIO_SIZE:
self.rfBuf = empty # length ws out out bounds, restart
if len(self._rxBuf) != 0 and ptr + 1 == packetlen + HEADER_LEN:
try:
self._handleFromRadio(self._rxBuf[HEADER_LEN:])
except Exception as ex:
logging.error(
f&#34;Error handling FromRadio, possibly corrupted? {ex}&#34;)
traceback.print_exc()
self._rxBuf = empty
else:
# logging.debug(f&#34;timeout on {self.devPath}&#34;)
pass
logging.debug(&#34;reader is exiting&#34;)
self.stream.close()
self._disconnected()</code></pre>
self._handleFromRadio(self._rxBuf[HEADER_LEN:])
except Exception as ex:
logging.error(
f&#34;Error while handling message from radio {ex}&#34;)
traceback.print_exc()
self._rxBuf = empty
else:
# logging.debug(f&#34;timeout on {self.devPath}&#34;)
pass
except serial.SerialException as ex:
logging.warn(
&#34;Meshtastic erial port disconnected, disconnecting...&#34;)
finally:
logging.debug(&#34;reader is exiting&#34;)
self.stream.close()
self._disconnected()</code></pre>
</details>
</section>
<section>
@@ -1241,50 +1246,55 @@ debugOut {stream} &ndash; If a stream is provided, any debug serial output from
&#34;&#34;&#34;The reader thread that reads bytes from our stream&#34;&#34;&#34;
empty = bytes()
while not self._wantExit:
b = self.stream.read(1)
if len(b) &gt; 0:
# logging.debug(f&#34;read returned {b}&#34;)
c = b[0]
ptr = len(self._rxBuf)
try:
while not self._wantExit:
b = self.stream.read(1)
if len(b) &gt; 0:
# logging.debug(f&#34;read returned {b}&#34;)
c = b[0]
ptr = len(self._rxBuf)
# Assume we want to append this byte, fixme use bytearray instead
self._rxBuf = self._rxBuf + b
# Assume we want to append this byte, fixme use bytearray instead
self._rxBuf = self._rxBuf + b
if ptr == 0: # looking for START1
if c != START1:
self._rxBuf = empty # failed to find start
if self.debugOut != None:
if ptr == 0: # looking for START1
if c != START1:
self._rxBuf = empty # failed to find start
if self.debugOut != None:
try:
self.debugOut.write(b.decode(&#34;utf-8&#34;))
except:
self.debugOut.write(&#39;?&#39;)
elif ptr == 1: # looking for START2
if c != START2:
self.rfBuf = empty # failed to find start2
elif ptr &gt;= HEADER_LEN: # we&#39;ve at least got a header
# big endian length follos header
packetlen = (self._rxBuf[2] &lt;&lt; 8) + self._rxBuf[3]
if ptr == HEADER_LEN: # we _just_ finished reading the header, validate length
if packetlen &gt; MAX_TO_FROM_RADIO_SIZE:
self.rfBuf = empty # length ws out out bounds, restart
if len(self._rxBuf) != 0 and ptr + 1 == packetlen + HEADER_LEN:
try:
self.debugOut.write(b.decode(&#34;utf-8&#34;))
except:
self.debugOut.write(&#39;?&#39;)
elif ptr == 1: # looking for START2
if c != START2:
self.rfBuf = empty # failed to find start2
elif ptr &gt;= HEADER_LEN: # we&#39;ve at least got a header
# big endian length follos header
packetlen = (self._rxBuf[2] &lt;&lt; 8) + self._rxBuf[3]
if ptr == HEADER_LEN: # we _just_ finished reading the header, validate length
if packetlen &gt; MAX_TO_FROM_RADIO_SIZE:
self.rfBuf = empty # length ws out out bounds, restart
if len(self._rxBuf) != 0 and ptr + 1 == packetlen + HEADER_LEN:
try:
self._handleFromRadio(self._rxBuf[HEADER_LEN:])
except Exception as ex:
logging.error(
f&#34;Error handling FromRadio, possibly corrupted? {ex}&#34;)
traceback.print_exc()
self._rxBuf = empty
else:
# logging.debug(f&#34;timeout on {self.devPath}&#34;)
pass
logging.debug(&#34;reader is exiting&#34;)
self.stream.close()
self._disconnected()</code></pre>
self._handleFromRadio(self._rxBuf[HEADER_LEN:])
except Exception as ex:
logging.error(
f&#34;Error while handling message from radio {ex}&#34;)
traceback.print_exc()
self._rxBuf = empty
else:
# logging.debug(f&#34;timeout on {self.devPath}&#34;)
pass
except serial.SerialException as ex:
logging.warn(
&#34;Meshtastic erial port disconnected, disconnecting...&#34;)
finally:
logging.debug(&#34;reader is exiting&#34;)
self.stream.close()
self._disconnected()</code></pre>
</details>
<h3>Ancestors</h3>
<ul class="hlist">

View File

@@ -31,10 +31,10 @@ type of packet, you should subscribe to the full topic name. If you want to see
import meshtastic
from pubsub import pub
def onReceive(packet): # called when a packet arrives
def onReceive(packet, interface): # called when a packet arrives
print(f"Received: {packet}")
def onConnection(): # called when we (re)connect to the radio
def onConnection(interface, topic): # called when we (re)connect to the radio
# defaults to broadcast, specify a destination ID if you wish
interface.sendText("hello mesh")
@@ -466,47 +466,52 @@ class StreamInterface(MeshInterface):
"""The reader thread that reads bytes from our stream"""
empty = bytes()
while not self._wantExit:
b = self.stream.read(1)
if len(b) > 0:
# logging.debug(f"read returned {b}")
c = b[0]
ptr = len(self._rxBuf)
try:
while not self._wantExit:
b = self.stream.read(1)
if len(b) > 0:
# logging.debug(f"read returned {b}")
c = b[0]
ptr = len(self._rxBuf)
# Assume we want to append this byte, fixme use bytearray instead
self._rxBuf = self._rxBuf + b
# Assume we want to append this byte, fixme use bytearray instead
self._rxBuf = self._rxBuf + b
if ptr == 0: # looking for START1
if c != START1:
self._rxBuf = empty # failed to find start
if self.debugOut != None:
if ptr == 0: # looking for START1
if c != START1:
self._rxBuf = empty # failed to find start
if self.debugOut != None:
try:
self.debugOut.write(b.decode("utf-8"))
except:
self.debugOut.write('?')
elif ptr == 1: # looking for START2
if c != START2:
self.rfBuf = empty # failed to find start2
elif ptr >= HEADER_LEN: # we've at least got a header
# big endian length follos header
packetlen = (self._rxBuf[2] << 8) + self._rxBuf[3]
if ptr == HEADER_LEN: # we _just_ finished reading the header, validate length
if packetlen > MAX_TO_FROM_RADIO_SIZE:
self.rfBuf = empty # length ws out out bounds, restart
if len(self._rxBuf) != 0 and ptr + 1 == packetlen + HEADER_LEN:
try:
self.debugOut.write(b.decode("utf-8"))
except:
self.debugOut.write('?')
elif ptr == 1: # looking for START2
if c != START2:
self.rfBuf = empty # failed to find start2
elif ptr >= HEADER_LEN: # we've at least got a header
# big endian length follos header
packetlen = (self._rxBuf[2] << 8) + self._rxBuf[3]
if ptr == HEADER_LEN: # we _just_ finished reading the header, validate length
if packetlen > MAX_TO_FROM_RADIO_SIZE:
self.rfBuf = empty # length ws out out bounds, restart
if len(self._rxBuf) != 0 and ptr + 1 == packetlen + HEADER_LEN:
try:
self._handleFromRadio(self._rxBuf[HEADER_LEN:])
except Exception as ex:
logging.error(
f"Error handling FromRadio, possibly corrupted? {ex}")
traceback.print_exc()
self._rxBuf = empty
else:
# logging.debug(f"timeout on {self.devPath}")
pass
logging.debug("reader is exiting")
self.stream.close()
self._disconnected()
self._handleFromRadio(self._rxBuf[HEADER_LEN:])
except Exception as ex:
logging.error(
f"Error while handling message from radio {ex}")
traceback.print_exc()
self._rxBuf = empty
else:
# logging.debug(f"timeout on {self.devPath}")
pass
except serial.SerialException as ex:
logging.warn(
"Meshtastic erial port disconnected, disconnecting...")
finally:
logging.debug("reader is exiting")
self.stream.close()
self._disconnected()

View File

@@ -10,7 +10,7 @@ with open("README.md", "r") as fh:
# This call to setup() does all the work
setup(
name="meshtastic",
version="1.0.1",
version="1.0.2",
description="Python API & client shell for talking to Meshtastic devices",
long_description=long_description,
long_description_content_type="text/markdown",