quarc_serial_interfacing_read_binary_demo.mdl
quarc_serial_interfacing_write_binary_demo.mdl
Open these models

MATLAB Command Line

Click to copy the following command line to the clipboard. Then paste it in the MATLAB Command Window:

quarc_serial_interfacing_read_binary_demo; quarc_serial_interfacing_write_binary_demo

QUARC Serial Interfacing Binary Demo

This example consists of a writer model that emulates a serial device and a reader model that normally acts as a controller. It demonstrates how to use the Stream Call, Stream Answer, Stream Read, and Stream Write blocks from the QUARC Targets Library to transfer binary data via serial port. For a detailed description of these models and how they operate, please refer to the Intermediate Communications section of the QUARC documentation.

Serial Interfacing Read Binary Demo QUARC Diagram

Serial Interfacing Write Binary Demo QUARC Diagram

Configuring the Demonstration

To set up the demonstration, double-click on the Stream Call block in the quarc_serial_interfacing_read_binary_demo.mdl model and on the Stream Answer block in the quarc_serial_interfacing_write_binary_demo.mdl model and ensure the URI upon which to listen and URI of host to which to connect parameters respectively are set correctly. In particular, ensure the port numbers in the URI reflect the correct COM port. The port is the number following the hostname. For example, "serial://localhost:6" refers to serial port COM6.

To run this demonstration on a single computer, there must be two serial ports available that are connected by a serial crossover cable or NULL modem cable. It is best if the hardware flow control lines RTS and CTS are included in the cable. Furthermore, the port being used by the writer should be different from the port being used by the reader. Otherwise an error will occur whenever you start the second model because a single serial port doesn't accept more than one connection at a time.

Warning

If the serial cable does not support hardware flow control lines then the flow option in the URI of both models must be set to flow=none or the serial connection may not function correctly!

Demonstration

Select Build from the QUARC menu of each diagram, or press Ctrl+B while each diagram is the active window. A great deal of output will appear in the Diagnostic Viewer about the progress of the build. If you cannot see the Diagnostic Viewer, you can open it by selecting View/Diagnostic Viewer from the menu of the diagram, or clicking on the View Diagnostics hyperlink at the bottom of the diagram. If you have MATLAB R2013b or earlier then the output will appear in the MATLAB Command Window.

Double-click on the "Received" Scope block in the quarc_serial_interfacing_read_binary_demo.mdl model to open the scope if it's not already open. Do the same for the "Sent" scope block in the quarc_serial_interfacing_write_binary_demo.mdl model.

Click on the Connect to Target button or select Connect to Target from the Simulation menu of the diagrams to connect to the models.

Start the models by clicking on the Run button or selecting Run from the Simulation menu of the diagram. The Start item of the QUARC menu may also be used to both connect and start the model in one operation. Start the quarc_serial_interfacing_read_binary_demo.mdl first.

A sine wave will show up in the "Sent" scope.

The sampling time can be changed by updating the value of Fixed-step size in the Configuration Parameters. If the quarc_serial_interfacing_read_binary_demo.mdl is being run on a different computer then it may be necessary to run it at a slightly higher sampling rate to ensure it doesn't fall behind the writer model (since sampling periods can differ very slightly between computers, particularly from different manufacturers).

Scope with Sawtooth Wave of Amplitude 1

Scope with Sine Wave of Amplitude 1

Click on the Stop button or select Stop from the Simulation menu of each diagram to stop the model. The Stop item of the QUARC menu may also be used.

Running the example on a different target

To run the example on a different target, refer to the instructions on the Running QUARC Examples on Remote Targets page.

Be sure to adjust the URI of each block appropriately as described in the Configuring the Demonstration section. Refer to the Serial Protocol page for the ports and options available on a particular target.