README.rst
1.. zephyr:code-sample:: zbus-uart-bridge
2 :name: UART bridge
3 :relevant-api: zbus_apis
4
5 Redirect channel events to the host over UART.
6
7Overview
8********
9
10This simple application demonstrates a UART redirection of all channel events to the host.
11The device sends information to the script running on a computer host. The script decodes it and prints it to the stdout.
12
13Building and Running
14********************
15
16This project outputs to the console. It can be built and executed
17on native_sim as follows:
18
19.. zephyr-app-commands::
20 :zephyr-app: samples/subsys/zbus/uart_bridge
21 :host-os: unix
22 :board: native_sim
23 :goals: run
24
25Sample Output
26=============
27
28.. code-block:: console
29
30 [0/1] cd .../zephyr/build/zephyr/zephyr.exe
31 uart_1 connected to pseudotty: /dev/pts/2
32 uart connected to pseudotty: /dev/pts/3
33 *** Booting Zephyr OS build zephyr-v3.1.0 ***
34 D: Core sending start measurement with status 0
35 D: START processing channel start_measurement change
36 D: FINISH processing channel start_measurement change
37 D: Peripheral sending sensor data
38 D: START processing channel sensor_data change
39 D: FINISH processing channel sensor_data change
40 D: Bridge Started
41 D: Channel start_measurement claimed
42 D: Channel start_measurement finished
43 D: Bridge send start_measurement
44 D: Channel sensor_data claimed
45 D: Channel sensor_data finished
46 D: Bridge send sensor_data
47 D: Core sending start measurement with status 1
48 D: START processing channel start_measurement change
49 D: FINISH processing channel start_measurement change
50 D: Channel start_measurement claimed
51 D: Channel start_measurement finished
52 D: Bridge send start_measurement
53 D: Peripheral sending sensor data
54 D: START processing channel sensor_data change
55 D: FINISH processing channel sensor_data change
56 D: Channel sensor_data claimed
57 D: Channel sensor_data finished
58 D: Bridge send sensor_data
59
60 <repeats endlessly>
61
62Exit execution by pressing :kbd:`CTRL+C`.
63
64The :file:`decoder.py` script can be executed using the following command:
65
66.. code-block:: bash
67
68 python3.8 samples/subsys/zbus/uart_bridge/decoder.py /dev/pts/2
69
70
71Note the run command above prints the value of pts port because it is running in
72:ref:`native_sim <native_sim>`.
73Look at the line indicating ``uart_1 connected to pseudotty: /dev/pts/2``.
74It can be different in your case. If you are using a board, read the documentation to get the
75correct port destination (in Linux is something like ``/dev/tty...`` or in Windows ``COM...``).
76
77From the serial decoder (Python script), you would see something like this:
78
79.. code-block:: shell
80
81 PUB [sensor_data] -> b'\xc5\x01\x00\x00\xb2\x11\x00\x00'
82 PUB [start_measurement] -> b'\x00'
83 PUB [sensor_data] -> b'\xc6\x01\x00\x00\xbc\x11\x00\x00'
84 PUB [start_measurement] -> b'\x01'
85 PUB [sensor_data] -> b'\xc7\x01\x00\x00\xc6\x11\x00\x00'
86 PUB [start_measurement] -> b'\x00'
87 PUB [sensor_data] -> b'\xc8\x01\x00\x00\xd0\x11\x00\x00'
88 PUB [start_measurement] -> b'\x01'
89 PUB [sensor_data] -> b'\xc9\x01\x00\x00\xda\x11\x00\x00'
90 PUB [start_measurement] -> b'\x00'
91 PUB [sensor_data] -> b'\xca\x01\x00\x00\xe4\x11\x00\x00'
92 PUB [start_measurement] -> b'\x01'
93 PUB [sensor_data] -> b'\xcb\x01\x00\x00\xee\x11\x00\x00'
94 PUB [start_measurement] -> b'\x00'
95 PUB [sensor_data] -> b'\xcc\x01\x00\x00\xf8\x11\x00\x00'
96 PUB [start_measurement] -> b'\x01'
97 PUB [sensor_data] -> b'\xcd\x01\x00\x00\x02\x12\x00\x00'
98 PUB [start_measurement] -> b'\x00'
99 PUB [sensor_data] -> b'\xce\x01\x00\x00\x0c\x12\x00\x00'
100 PUB [start_measurement] -> b'\x01'
101 PUB [sensor_data] -> b'\xcf\x01\x00\x00\x16\x12\x00\x00'
102 PUB [start_measurement] -> b'\x00'
103
104Exit the decoder script by pressing :kbd:`CTRL+C`.
105