{"id":281,"date":"2019-12-07T14:44:30","date_gmt":"2019-12-07T14:44:30","guid":{"rendered":"https:\/\/r.jander.me.uk\/?p=281"},"modified":"2019-12-07T14:46:20","modified_gmt":"2019-12-07T14:46:20","slug":"updated-ev3g-mailbox-messaging-in-python","status":"publish","type":"post","link":"https:\/\/r.jander.me.uk\/index.php\/2019\/12\/07\/updated-ev3g-mailbox-messaging-in-python\/","title":{"rendered":"Updated EV3g Mailbox Messaging in Python"},"content":{"rendered":"\n<p><a href=\"https:\/\/r.jander.me.uk\/index.php\/2019\/11\/25\/ev3-mailboxes-in-python\/\">Previously<\/a> I posted an article on handling EV3g binary Mailbox messages under Python3. Since then I have carried on working on this class, along with adding a handler class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Improved Mailbox Handling<\/h2>\n\n\n\n<p>One of the things I wasn&#8217;t so keen on with my implementation was the need to specify the type of Mailbox value, i.e. BOOL, NUMBER, or TEXT. Python&#8217;s variables have their own type, so the code has been adjusted to use the value&#8217;s own type to determine the binary payload format. It is still possible to coerce the type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from ev3mailbox import EV3Mailbox\n\nfloat_msg  = EV3Mailbox(\"Pi\", 3.1415)\n\n# Coerce to a string\nstring_msg = EV3Mailbox(\"Pie\", 3.1415, str)<\/pre>\n\n\n\n<p>These changes have made the use of this side of the code much cleaner.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Mailbox I\/O Handler<\/h2>\n\n\n\n<p>Whilst working on my use case for the original code, I had been working on the principle that I&#8217;d be using it in a simple synchronous send\/receive pattern. This worked well, until I started using threads at both sides of the ev3dev &lt;-&gt; EV3g link. Once threads were in the mix, there&#8217;s a risk that the bt_socket.recv(&#8230;) call could actually receive a message that wasn&#8217;t destined for that particular call, but for another area of the program.<\/p>\n\n\n\n<p>The solution to the above problem was to implement a receiving thread that deals with all the socket.recv(&#8230;) calls. Each message is decoded, and then each Mailbox name has its own FIFO of message objects. It&#8217;s a deliberate choice to maintain the list of objects, rather than just their values, so that they can be forced to floats if it&#8217;s known they may be very small &#8211; see my previous post about that problem.<\/p>\n\n\n\n<p>The new class implements a handler that will deal with all the Bluetooth and thread side of things. All that&#8217;s then required to do is call send(&#8230;), get(&#8230;), or stop() on the class instance:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from ev3messages import EV3Messages\n\nhandler = EV3Message(bt_mac_address)\n\nhandler.send(\"Name\", value)\nmsg = handler.get(\"ANOther\")\nvalue = msg.value\n\nhandler.stop()<\/pre>\n\n\n\n<p>The calls to send(&#8230;) and get(&#8230;) should (!) be thread safe, so calls to get(&#8230;) wait on receiving a message of the requested name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code Repo<\/h2>\n\n\n\n<p>The repo is available from: <a href=\"https:\/\/gitlab.com\/Jander\/ev3-mailbox-python\">https:\/\/gitlab.com\/Jander\/ev3-mailbox-python<\/a> and is released under the GPLv3.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously I posted an article on handling EV3g binary Mailbox messages under Python3. Since then I have carried on working on this class, along with adding a handler class. Improved Mailbox Handling One of the things I wasn&#8217;t so keen on with my implementation was the need to specify the type of Mailbox value, i.e. &hellip; <a href=\"https:\/\/r.jander.me.uk\/index.php\/2019\/12\/07\/updated-ev3g-mailbox-messaging-in-python\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Updated EV3g Mailbox Messaging in Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9,37,35,26,34],"class_list":["post-281","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-bluetooth","tag-ev3dev","tag-ev3g","tag-lego","tag-python"],"_links":{"self":[{"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/comments?post=281"}],"version-history":[{"count":2,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/281\/revisions"}],"predecessor-version":[{"id":284,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/281\/revisions\/284"}],"wp:attachment":[{"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/media?parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/categories?post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/tags?post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}