Serial - RS485 Loopback
This example shows you how to perform a RS485 Half-Duplex and a RS485 Full-Duplex loopback test. The Simulink model can be configured for the serial I/O modules from the IO50x/IO581 familiy. The example implements a loopback by sending the RS485 messages from channel 1 to channel 2 and from channel 3 to channel 4.
If you experience any issues with this example, please refer to the Possible Issues section below. |
Setup
Prerequisites
You will require the following to run this example:
- Speedgoat real-time target machine with one serial I/O module (IO50x/IO581) installed
- Connector cable from the I/O module to the terminal board
- Terminal board with jumper wires
Test Setup
In this example, data is alternately sent from channel 1 to channel 2 and vice versa using the RS485 Half-Duplex mode. Channel 3 and channel 4 are connected using the RS485 Full-Duplex mode. You must therefore connect the pins on the terminal board where these channels are located. Please refere to the individual pin mappings for your serial I/O module: IO503 Pin Mapping, IO504 Pin Mapping, IO505 Pin Mapping, IO581 Pin Mapping If an IO581 I/O module is used, hardware jumpers might need to be changed to enable the termination resistors as they are not software configurable. Please check the Serial documentation for more information. Open the Simulink Model
modelName = 'sgMdl_Serial_RS485Loopback';
Model Description
Please note that by default this Simulink model is configured to be used with an IO503. To change to another serial I/O module, simply change the Module Type in all the serial blocks.
In the model, four sine wave signals are encoded to an ASCII data stream and transmitted by the four channels using the Legacy Mode. The legacy mode is used as it supports the same data type as the ASCII Encode block outputs, which is a NULL-terminated character string with a delimiter after the ASCII-encoded variable. Consequently, it is easy to extract the encoded variable and only complete messages are decoded. The transmit software FIFO size must be configured in the Setup block to ensure it is sufficiently large enough to store at least two complete messages in case one is not entirely sent out before the next message is written to the buffer. Channel 1 and channel 2 are both configured in the RS485 Half-Duplex mode and connected. Therefore, only one channel is allowed to send data at the same time. This is achieved by the enabled subsystems for these channels which are alternately enabled. Channel 3 and channel 4 are both configured in the RS485 Full-Duplex mode and connected together. This setup allows data to be sent and received on both channels at the same time. On the receiving side, the ASCII data stream is buffered and then decoded to extract the original sine wave. Buffering is required as a partially complete message might be available when the Serial - Read block is executed. To ensure compatibility with the ASCII Decode blocks' input port data type, the receive track is configured to use the NULL-terminated character string which is sent from the transmit channels. The size of the buffer set to double the size of the receive software FIFO configured in the Serial - Setup block to prevent data loss in case of an overflow. The receive software FIFO is configured to the same size as the transmit software FIFO to ensure it can hold the transmitted message. The FIFO Read block parses the data stream received for the specified delimiter ('\r' = 13) in order to be able to extract a complete message. The maximum read size is set to the same size as the buffer to ensure that all messages are read if multiple messages are available. The default value of 1 is used for the minimum read size: the delimiter will therefore ensure that only complete messages are output. No single bytes will be output. Once a complete message is available in the buffer, it can be decoded by the ASCII Decode block. Build, Download, and Run the Example
To run the example, either run the following code section or click the Run on Target button in the REAL-TIME tab in the Simulink model.
% Build the Simulink model
slbuild(modelName); % this will create the real-time application file (.mldatx)
% Create and connect to the Speedgoat real-time target machine
% Download and install the real-time application on the target machine
% Connect the Simulink model with external mode to the real-time application on the target machine
set_param(modelName,'SimulationMode', 'external') % put model into External Mode
set_param(modelName,'SimulationCommand','connect') % connect with External Mode
% Start the real-time application
% Wait a few seconds and then stop the real-time application on the target machine
Possible Issues
A problem has been encountered with the FIFO Write block used in the legacy configuration with the ASCII workflow. The error PopUp "FIFO overflow: RX Chn overflow" may appear, where 'n' is the channel number.
The referenced FIFO is not linked to the Serial FIFO of the target or the module. In the ASCII workflow, the data is written to a dedicated FIFO and immediately read back so it can be formatted correctly for ASCII decoding. The FIFO Write writes to the FIFO, and the FIFO Read reads the FIFO when it detects the delimiter.
If, for some reason, the messages are not sent correctly, the delimiter may not be transmitted In this case, the FIFO is filled but not emptied, eventually overflowing. One of the causes of the incorrect transmission is unreliable wiring of the terminal board. This ASCII workflow is not tolerant of wiring issues.
To resolve this issue there are three solutions:
- Check the wiring.
- Uncheck the "Read to delimiter" option in the FIFO Read block. (This option is usually suggested to have checked in the ASCII workflow.)
- Do not use the ASCII workflow with the FIFO blocks.
Check the Results
To check if the loopback is working as expected, open the Simulink scope connected to the ASCII Decode blocks. Four sine waves with different amplitudes must be displayed: Additional References