UART TxMatlab Block Icon
UART_TxMatlab Block Icon The UART TxMatlab block transmits numerical data through UART to MATLAB (or alternative hosts) for real-time monitoring, plotting, and recording. This block supports multiple data types (int8, uint8, int16, uint16, int32, uint32, single, double) and implements a packetized protocol for reliable data streaming and visualization using the picgui MATLAB interface.

Supported Device Families

FamilySeriesArchitecture IDImplementation
dsPIC30F, 24F, 33F, 33E, 33C, 33A1, 2, 3, 4, 8Same as UART Tx (PIC implementations)
PIC32MK, MZ, MX, PIC32AIncluded in dsPIC archSame as UART Tx (PIC implementations)
SAM E7x/S7xSAME70, SAMS70, V71, PIC32CZ MC705USART/UART variants (SAMx7)
SAM E5x/D5xSAME5x, SAMD5x6SERCOM USART (SAMx5)
SAM C2x/D2xSAMC2x, SAMD2x, SAMDA17SERCOM USART (SAMx5 variant)

Block Inputs

Dynamic Channel Inputs

The number and labels of input ports are determined by the CHANNELS parameter. Each specified channel creates a corresponding input port.

Port NameSupported Data TypesDescription
CH 1int8, uint8, int16, uint16, int32, uint32, single, double, booleanChannel 1 data (if channel 1 in CHANNELS)
CH 2Same as aboveChannel 2 data (if channel 2 in CHANNELS)
Same as aboveAdditional channels (up to 16)
CH 16Same as aboveChannel 16 data (if channel 16 in CHANNELS)

Configuration Parameters

UART Selection

  • UART Module - Select UART/USART number (must match UART Config block)
  • Dropdown automatically populated with available UARTs from selected chip

Channel Configuration

ParameterFormatDescriptionExample
CHANNELSVector [1 to 16]List of channel numbers to transmit[1 2 5] creates CH 1, CH 2, CH 5 inputs

Transmission Options

ParameterOptionsDescription
SampleTimeNumeric (seconds)Block execution rate (affects bytes/step calculation)
AllOrNothingOn/Off checkboxOnly transmit if entire packet can be sent

Packet Protocol

Data Packetization

Each channel is packetized with a control character followed by data bytes:

Control Character Format (8 bits)

BitsFieldDescription
0-1SignatureProtocol signature (always
2-3Data Type0=int8/uint8, 1=int16/uint16, 2=int32/uint32, 3=single/double
4-7Channel IDChannel number - 1 (0-15 for channels 1-16)

Data Type Encoding

Data TypeType CodeBytes SentNotes
int8 / uint801 (+1 control)8-bit integer
int16 / uint1612 (+1 control)16-bit integer
int32 / uint3224 (+1 control)32-bit integer
single34 (+1 control)32-bit float
double (64-bit)38 (+1 control)If doubleIs64Bits enabled
double (32-bit)34 (+1 control)If doubleIs64Bits disabled (default dsPIC)
boolean01 (+1 control)Treated as uint8

Bytes Per Step Calculation

The block automatically calculates and displays transmission rate: BytesPerModelStep = TotalBytes × (FixedStep / SampleTime)

  • TotalBytes: Sum of all channel data bytes + control bytes
  • FixedStep: Model fixed-step size
  • SampleTime: Block sample time

Usage Examples

Example 1: Simple 3-Channel Data Logger

  • Add UART Config block:

  • UART: 1

  • Baud Rate: 115200

  • Tx Implementation: Circular Buffer

  • Add UART TxMatlab block:

  • UART Module: 1

  • CHANNELS: [1 2 3]

  • SampleTime: 0.001 (1 kHz)

  • AllOrNothing: Off

  • Connect inputs:

  • CH 1: Motor speed (int16)

  • CH 2: Motor current (single)

  • CH 3: Temperature (uint8)

  • In MATLAB, run: picgui

  • Select COM port, baud rate 115200, start acquisition

  • Real-time plots appear for all 3 channels

Example 2: High-Speed Vector Transmission

  • Configure UART TxMatlab:

  • CHANNELS: [1]

  • SampleTime: 0.0001 (10 kHz)

  • Connect CH 1 input to vector [100x1] of type single

  • Block calculates:

  • Data bytes: 100 × 4 = 400 bytes

  • Control byte: 1 byte

  • Total: 401 bytes per execution

  • If model step = 0.0001s: 401 bytes/step

  • Required baud rate: 401 × 8 × 10000 = 32 Mbps (needs high-speed UART)

Example 3: Mixed Data Type Monitoring

  • UART TxMatlab configuration:

  • CHANNELS: [1 2 5 10]

  • SampleTime: 0.01 (100 Hz)

  • Input connections:

  • CH 1: Position setpoint (double)

  • CH 2: Actual position (double)

  • CH 5: Error (single)

  • CH 10: Control mode (uint8)

  • picgui displays:

  • Channels 1 & 2: Position tracking plot

  • Channel 5: Error magnitude plot

  • Channel 10: Mode indicator

picgui MATLAB Interface

Starting picgui

>> picgui

  • Opens graphical interface for UART data reception
  • Supports COM port selection and baud rate configuration
  • Real-time plotting of received channels
  • Data recording to MATLAB workspace or file

picgui Features

  • Multi-channel plotting: Up to 16 channels simultaneously
  • Data export: Save to .mat file or workspace variable
  • Auto-scaling: Automatic Y-axis scaling
  • Pause/Resume: Freeze display while maintaining reception
  • Clear buffer: Reset plots and data buffer

Implementation Details

Control Character Generation

ControlChar = 1 + (DataTypeCode << 2) + (ChannelID << 4) Example: Channel 3, single precision (type 3): ControlChar = 1 + (3 << 2) + (2 << 4) = 1 + 12 + 32 = 45 (0x2D)

Transmission Implementation

The block uses the same transmission infrastructure as UART Tx:

  • Reuses UART Tx implementation (bare, circular buffer, DMA)
  • Adds packetization layer with control characters
  • Thread protection identical to UART Tx
  • Buffer management handled by UART Config settings

Troubleshooting

No Data in picgui

Problem: picgui runs but shows no data.Solutions:

  • Verify COM port and baud rate match UART Config
  • Check UART TX pin connection
  • Ensure UART Config block exists
  • Verify channels are correctly specified (1-16 range)

Corrupted Data / Garbled Plots

Problem: picgui shows incorrect values or noise.Solutions:

  • Check baud rate mismatch (must exactly match)
  • Increase UART buffer size (high data rates)
  • Enable “AllOrNothing” mode to prevent partial packets
  • Reduce transmission rate (lower sample frequency or fewer channels)

Buffer Overrun Errors

Problem: Data loss or transmission gaps.Solutions:

  • Increase baud rate (e.g., 115200 → 921600)
  • Reduce bytes/step (lower sample rate or reduce channels)
  • Use DMA Ping-Pong mode (if available)
  • Increase circular buffer size in UART Config
  • Check “Bytes / Step” display on block - should be < buffer size

Wrong Data Type Displayed

Problem: picgui shows incorrect data type interpretation.Solutions:

  • Verify input signal data types match expected types
  • Check double precision setting (doubleIs64Bits)
  • Ensure control character calculation is correct
  • Recompile model to update MCHP_COMPILED_INFO
  • [UART Config] - Configure UART module (required)
  • [UART Tx] - Basic UART transmission
  • [UART Rx] - Receive data
  • [UART Brk/Autobaud] - Break detection and autobaud

See Also

[Communication Blocks] | [UART Config] | External Mode | Code Generation