<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[M5Stack Unit2 switch mode by sending command in serial port]]></title><description><![CDATA[<p dir="auto">Hi there,</p>
<p dir="auto">I'm trying to manage UnitV2 device by Serial port. Reading data works just fine, for example in the "Object detection" mode Unit sends data:</p>
<pre><code>{'num': 1, 'obj': [{'prob': 0.697403133, 'x': 44, 'y': 8, 'w': 555, 'h': 466, 'type': 'person'}], 'running': 'Object Recognition'}
{'num': 1, 'obj': [{'prob': 0.684900761, 'x': 49, 'y': 1, 'w': 543, 'h': 468, 'type': 'person'}], 'running': 'Object Recognition'}
{'num': 1, 'obj': [{'prob': 0.686426222, 'x': 23, 'y': 2, 'w': 577, 'h': 472, 'type': 'person'}], 'running': 'Object Recognition'}
</code></pre>
<p dir="auto">Unfortunately I can't manage to send commands and actually switch the mode. Here is my code for Raspberry PI(connected with UnitV2 by serial ports):</p>
<pre><code>import serial, json

with serial.Serial() as s:
    s.port = '/dev/ttyAMA0'
    s.baudrate = 115200
    s.timeout = .5
    s.open()

    js = {
        'function': 'Camera Stream',
        'args': ''
    }
    print("send", js)
    s.write(json.dumps(js).encode('utf-8'))

    response = s.readline()
    print("receive", response)
</code></pre>
<p dir="auto">And the output:</p>
<pre><code>send {'function': 'Camera Stream', 'args': ''}
receive b''
</code></pre>
<p dir="auto">Am I missing something?</p>
]]></description><link>https://community.m5stack.com/topic/4074/m5stack-unit2-switch-mode-by-sending-command-in-serial-port</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 00:56:59 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/4074.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 23 Feb 2022 20:44:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to M5Stack Unit2 switch mode by sending command in serial port on Fri, 09 Dec 2022 08:36:28 GMT]]></title><description><![CDATA[<p dir="auto">Getting closer...  This is the string to change modes.</p>
<p dir="auto">Function switch<br />
Switch between different recognition functions by clicking the navigation bar of the function page or sending JSON commands through Serial Port communication. Note: Except at the end of the sent command string, no line breaks are allowed in other positions.</p>
<p dir="auto">{<br />
"function": "Camera Stream",<br />
"args": ""<br />
}</p>
<p dir="auto">BUT this is what I get in return</p>
<p dir="auto">{"error":"invalid json format."}<br />
{"error":"invalid json format."}<br />
{"error":"invalid json format."}<br />
{"error":"invalid json format."}<br />
{"error":"Command is not supported"}<br />
{"error":"invalid json format."}<br />
{"error":"invalid json format."}</p>
<p dir="auto">Anyone have any ideas?<br />
I'm sending the string through a serial termanal. CR,LF</p>
]]></description><link>https://community.m5stack.com/post/19488</link><guid isPermaLink="true">https://community.m5stack.com/post/19488</guid><dc:creator><![CDATA[class14]]></dc:creator><pubDate>Fri, 09 Dec 2022 08:36:28 GMT</pubDate></item><item><title><![CDATA[Reply to M5Stack Unit2 switch mode by sending command in serial port on Thu, 31 Mar 2022 11:07:10 GMT]]></title><description><![CDATA[<p dir="auto">I would like to send command to change unit v2 to code dectector by arduino when m5 Stack power on. Please give me sample code. Thank you.<br />
.</p>
]]></description><link>https://community.m5stack.com/post/17061</link><guid isPermaLink="true">https://community.m5stack.com/post/17061</guid><dc:creator><![CDATA[satit]]></dc:creator><pubDate>Thu, 31 Mar 2022 11:07:10 GMT</pubDate></item><item><title><![CDATA[Reply to M5Stack Unit2 switch mode by sending command in serial port on Sun, 27 Feb 2022 19:41:58 GMT]]></title><description><![CDATA[<p dir="auto">Hi Odemakov<br />
Any way you can just provide the actual string sent for any one of the functions?<br />
I am not using Python and have a hard time parsing what the code produces as a json string that is sent to V2.<br />
Thanks.</p>
]]></description><link>https://community.m5stack.com/post/16791</link><guid isPermaLink="true">https://community.m5stack.com/post/16791</guid><dc:creator><![CDATA[tguneysu]]></dc:creator><pubDate>Sun, 27 Feb 2022 19:41:58 GMT</pubDate></item><item><title><![CDATA[Reply to M5Stack Unit2 switch mode by sending command in serial port on Thu, 24 Feb 2022 06:56:20 GMT]]></title><description><![CDATA[<p dir="auto">Finally I managed to switch the mode from Raspberry PI using <code>ReaderThread</code>. Here is the code:</p>
<pre><code>import sys
import traceback
import argparse
import json
import serial
from serial.threaded import LineReader, ReaderThread
from time import sleep

MODES = [
    'Audio FFT',
    'Code Detector',
    'Face Detector',
    'Lane Line Tracker',
    'Motion Tracker',
    'Shape Matching',
    'Camera Stream',
    'Online Classifier',
    'Color Tracker',
    'Face Recognition',
    'Target Tracker',
    'Shape Detector',
    'Object Recognition',
]

ap = argparse.ArgumentParser()
ap.add_argument('-m', '--mode', help = 'Switch to mode: {}'.format(MODES))
ap.add_argument('-a', '--args', default = '', help = 'Some modes require extra args. For "Object Recognition" either "yolo_20class" or "nanodet_80class"')
args = vars(ap.parse_args())

if 'mode' not in args or args['mode'] not in MODES:
    ap.print_help()
    exit(1)

OBJECT_RECOGNITION_ARGS = [
    'yolo_20class',
    'nanodet_80class'
]
if args['mode'] == 'Object Recognition' and args['args'] not in OBJECT_RECOGNITION_ARGS:
    ap.print_help()
    exit(1)

class PrintLines(LineReader):
    def connection_made(self, transport):
        super(PrintLines, self).connection_made(transport)
        sys.stdout.write('port opened\n')

    def handle_line(self, data):
        sys.stdout.write('line received: {}\n'.format(repr(data)))

    def connection_lost(self, exc):
        if exc:
            traceback.print_exc(exc)
        sys.stdout.write('port closed\n')

with serial.Serial() as s:
    s.port = '/dev/ttyAMA0'
    s.baudrate = 115200
    s.timeout = .5
    s.open()

    with ReaderThread(s, PrintLines) as protocol:
        js = {
            'function': args['mode'],
            'args': args['args']
        }
        protocol.write_line(json.dumps(js))
        sleep(2)
</code></pre>
<p dir="auto">And the output:</p>
<pre><code>pi@rpi0:~/serial$ ./serial_mode.py --mode 'Object Recognition' --args 'yolo_20class'
port opened
line received: '{"msg":"function switched to object_recognition."}'
line received: '{"msg":"Running Object Recognition, Copyright 2021 M5Stack Technology Co., Ltd. All rights reserved.","running":"Object Recognition"}'
port closed
</code></pre>
]]></description><link>https://community.m5stack.com/post/16758</link><guid isPermaLink="true">https://community.m5stack.com/post/16758</guid><dc:creator><![CDATA[odemakov]]></dc:creator><pubDate>Thu, 24 Feb 2022 06:56:20 GMT</pubDate></item></channel></rss>