Serial Communication

Basic Operation

Serial communication uses RX and TX pins to receive and transmit data.

1.gif

As there is no common clock signal, both devices must agree on a number of parameters. If necessary, these parameters can be given to the microcontroller's "Serial" specification.

"BaudRate"Automaticdata transfer rate in bits per second
"DataBits"Automaticnumber of data bits to use per frame
"Parity"Automaticsetting for the parity bit
"StopBits"Automaticnumber of stop bits to use

Basic serial specifications.

Load the package.
In[1]:=
Click for copyable input
Set the baud rate.
In[2]:=
Click for copyable input
Out[2]=

There is only one RX and one TX pin on the Uno, so specification can just be "Serial".

The input and output pins are unique and need not be explicitly given.
In[3]:=
Click for copyable input
Out[3]=

In addition, there are other specifications that configure the actual data that is being transferred.

"StartByte"19 (XOff)the first byte transmitted or to be received
"DelimiterByte"44 (,)the byte inbetween data
"EndByte"17(XOn)the last byte transmitted or to be received
"FractionalPartDigits"Automaticthe number of digits in the fractional part

Serial data specifications.

The "DelimiterByte" is relevant only if there are more than one input or output channels.

Set the delimiter byte as an empty space.
In[4]:=
Click for copyable input
Out[4]=

The parameters in the deployed code can be retrieved from the MicrocontrollerCodeData object.

The values of the serial properties.
In[5]:=
Click for copyable input
Out[5]=

The "BaudRateError" gives the percentage error in the baud rate. Properties like "TransmissionSpeedMode" are microcontroller specific. All the properties, except for "BaudRateError" can be set in the "Serial" option.

Specific property values can also be extracted.

The baud rate value.
In[6]:=
Click for copyable input
Out[6]=

Virtual COM Port

On some microcontrollers the serial functionality can be set up over USB. In these cases, the RX and TX pins are not used but rather the USB pins D- and D+.

The communication happens over a virtual port and the parameters are set using "SerialOverUSB" . Other than that, the workflow is similar to the regular case.

Set the start byte to .
In[7]:=
Click for copyable input
Out[7]=
Retrieve the value of the start byte.
In[8]:=
Click for copyable input
Out[8]=

Device Framework

While on some boards such as the Arduino Micro the serial functionality is implemented in software, other boards such as the Arduino Uno have a USB-to-serial converter. Both kinds of boards can communicate with a personal computer through the USB port using the serial interface. Through this serial interface the Wolfram Device Framework can communicate with these boards.

First, the microcontroller needs to be set up to send or receive data.

Set up the microcontroller to send and receive numerical data.
In[9]:=
Click for copyable input
Out[9]=

The microcontroller is now a serial device. Open a serial connection to it using the device framework.

Open a connection.
In[10]:=
Click for copyable input
Out[10]=
Start a task to send and receive data.
In[11]:=
Click for copyable input
Plot the data.
In[13]:=
Click for copyable input
Out[13]=
Stop the task and close the connection to the device.
In[14]:=
Click for copyable input