This section refers to the following sample application(s):
02-uart_raw
In addition to the using the UART with hardware HDLC framing, the UART serial port can be used raw (not HDLC) encoded - this allows an external microcontroller or sensor with a UART interface to communicate with your application.
Your application can set up the UART device to trigger a notification whenever valid bytes are received over the serial port. Similarly, your application can send bytes over UART. In raw mode, you can configure the baud rate, number of stop bits, parity, and inter-byte delay settings.
The 02-uart_raw
Sample Application
The 02-uart_raw
sample application creates two tasks, and defines three CLI commands - len
(gth), delay
, and tx
. A companion Python script uart_raw_app.py
runs on your computer and connects to the API UART port of your device.
Setup
To test bidirectional UART messaging:
- Your computer and
SmartMesh mote
are connected over a USB cable. - Your
SmartMesh mote
is programmed with the02_uart_raw
sample application. - Your computer runs the following:
- The
uart_raw_app.py
Python script (available in theonchipsdk/
src/
/app/02-uart_raw/
directory). - A terminal client to enter CLI commands for the
SmartMesh mote
.
- The
You need to know the API and CLI serial ports corresponding to your
SmartMesh mote
. You will connect theuart_raw_app.py
Python script to the API port, and the terminal client to the CLI port.
Refer to the SmartMesh IP Tools Guide for information about how to identify the different serial ports.
Overview
The 02-uart_raw
sample application creates two tasks.
- The
uartTxTask
task transmits a number of UART "packets" from yourSmartMesh mote
to theuart_raw_app.py
Python script. The number of packets to send, their length, and the inter-packet delay are set using CLI commands. - The
uartRxTask
task listens for incoming UART bytes, sent in this case by the theuart_raw_app.py
Python script.
02-uart_raw
sample application defines three CLI commands:- use
len
to specify the length, in bytes, of the packet theuartTxTask
task transmits. - use
delay
to specify the duration, in ms, for theuartTxTask
task to wait between two successive packets. - use
tx
to specify the number of packets to transmit, and trigger theuartTxTask
task to start transmitting.
uart_raw_app.py
Python script connects to the API port of your SmartMesh mote
. It defines a number of commands allowing you to transmit UART frames to theSmartMesh mote
. It also prints the UART frames it receives. Start theuart_raw_app.py
Python script by double-clicking on its file.Before you run this application, make sure you have Python and pyserial
installed on your computer.
Refer to the SmartMesh IP Tools Guide for installation instructions.
uart_raw_app.py
Python script defines the same CLI commands are the 02-uart_raw
sample application, i.e. len
, delay
and tx
. On top of those, the uart_raw_app.py
Python script defines the connect
command to connect it to the API serial port of your SmartMesh mote
.help
on both the 02-uart_raw
sample application and the uart_raw_app.py
Python script to obtain the list of available commands.help
on the your SmartMesh mote
:> uart_raw app, ver 1.2.0.1 SmartMeshIP stack, ver 1.4.0.5 > help help <command> Commands: mtrace mset mget mseti mgeti mxtal mfs mlog mtrace minfo mshow len delay tx >
Type help
in the uart_rawapp.py
Python script:
uart_app OCFDK Sample application > help Commands: connect <comPort> baudrate <bps> stopbits <stopbits> len <numBytes> delay <numMs> tx <numPackets> exit >
Send UART frames from the
SmartMesh mote
First, connect the uart_raw_app.py
Python script to the API port of your
SmartMesh mote
(here onCOM28
).> connect COM28 >
On the
SmartMesh mote
, type the following command to send 5 packets of 10 bytes each, with an inter-packet delay of 100 ms to theuart_raw_app.py
Python script. > len 10 > delay 100 > tx 5 > Sending 5 UART packets, 10 bytes, delay 100 ms uart TX ( 5): 00 01 02 03 04 05 06 07 08 09 uart TX ( 4): 00 01 02 03 04 05 06 07 08 09 uart TX ( 3): 00 01 02 03 04 05 06 07 08 09 uart TX ( 2): 00 01 02 03 04 05 06 07 08 09 uart TX ( 1): 00 01 02 03 04 05 06 07 08 09 done.
The following is printed by the uart_raw_app.py
Python script. Note that the uart_raw_app.py
uses the 00 as a break between packets - there is no data framing in the raw UART (other than the interpacket delay).
uart RX: 00 01 02 03 04 05 06 07 08 09 uart RX: 00 01 02 03 04 05 06 07 08 09 uart RX: 00 01 02 03 04 05 06 07 08 09 uart RX: 00 01 02 03 04 05 06 07 08 09 uart RX: 00 01 02 03 04 05 06 07 08 09
Receive UART frames at the
SmartMesh mote
On the uart_raw_app.py
Python script, type the following command to send 5 packets of 10 bytes each, with an inter-packet delay of 100 ms to the
SmartMesh mote
:> len 10 > delay 100 > tx 5 Sending 5 UART packets, 10 bytes, delay 100 ms uart TX ( 5): 00 01 02 03 04 05 06 07 08 09 uart TX ( 4): 00 01 02 03 04 05 06 07 08 09 uart TX ( 3): 00 01 02 03 04 05 06 07 08 09 uart TX ( 2): 00 01 02 03 04 05 06 07 08 09 uart TX ( 1): 00 01 02 03 04 05 06 07 08 09 done. >
The following is printed at the
SmartMesh mote
:uart RX (10 bytes) 00 01 02 03 04 05 06 07 08 09 uart RX (10 bytes) 00 01 02 03 04 05 06 07 08 09 uart RX (10 bytes) 00 01 02 03 04 05 06 07 08 09 uart RX (10 bytes) 00 01 02 03 04 05 06 07 08 09 uart RX (10 bytes) 00 01 02 03 04 05 06 07 08 09
The mote uses the inter-byte timeout provided in the dn_open()
call to "frame" the incoming bytes.