write
Add-On Required: This feature requires the MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms add-on.
Description
Examples
Create Modbus Server on an NVIDIA Jetson Platform
This example shows how to use modbus
function to
configure the NVIDIA®
Jetson™ TX2 as a Modbus® server.
Create a live hardware connection from the MATLAB® software to the NVIDIA hardware by using the jetson
function.
To create a live hardware connection object, provide the host name or IP address, user name,
and password of the target board. For example:
hwobj = jetson('jetson-board-name','ubuntu','ubuntu');
Create a MATLAB function tx2ModbusServer.m
that configures the Jetson TX2 board as a server and allocate address space for coil, discrete inputs,
input registers and holding registers. This function acts as the
entry-point for code generation.
function tx2ModbusServer() %#codegen hwobj = jetson(); m = modbus(hwobj, 1478, 'Mode', 'server',... 'CoilStartAddress', 1, 'NumCoils', 10, ... 'InputStartAddress', 11, 'NumInputs', 10,... 'InputRegStartAddress', 21, 'NumInputRegs', 10,... 'HoldingRegStartAddress', 31, 'NumHoldingRegs', 10); val = 1; for i = 1:10 write(m, 'coils', i, val); write(m, 'inputs', i+10, val); write(m, 'inputregs', i+20, i+20); write(m, 'holdingregs', i+30, i+30); pause(0.1); end for i=1:1000 % Holding the server live pause(0.5); end clear(m); end
Create a GPU code configuration object for generating an executable. Use the coder.hardware
function to create a configuration object for the Jetson platform and assign it to the Hardware
property of the code
configuration object cfg
.
cfg = coder.gpuConfig('exe'); cfg.GenerateReport = true; cfg.Hardware = coder.hardware('NVIDIA Jetson'); cfg.CustomInclude = fullfile('codegen','exe','tx2ModbusServer','examples'); cfg.CustomSource = fullfile('codegen','exe','tx2ModbusServer','examples','main.cu');
The main.cu
file is generated as part of code generation process. For
this example, you can this file without modification.
To generate CUDA code, use the codegen
command and pass the GPU code configuration object along with the
tx2ModbusServer
entry-point function. After the code generation takes
place on the host, the generated files are copied over and built on the target.
codegen -config cfg tx2ModbusServer
Use the runApplication
function to launch the executable on the TX2
board.
runApplication(hwobj,'tx2ModbusServer');
Input Arguments
target
— Target area to write to
character vector | string
Target area to write to, specified as a character vector or string. You can perform
a Modbus write operation on two types of targets: coils and holding registers, so
you must set the target type as either 'coils'
or
'holdingregs'
. Target must be the first argument after the object
name. This example writes to 4 coils starting at address 8289.
Example: write(m,'coils',8289,[1 1 0 1])
Data Types: char
address
— Starting address to write to
double
Starting address to write to, specified as a double. Address must be the second argument after the object name. This example writes to 6 coils starting at address 5200.
Example: write(m,'coils',5200,[1 1 0 1 1 0])
Data Types: double
values
— Array of values to write
double | array of doubles
Array of values to write, specified as a double or array of doubles.
values
must be the third argument after the object name. If the
target is coils, valid values are 0
and 1
. If the
target is holding registers, valid values must be in the range of the specified
precision. You can include the array of values in the syntax, as shown here, or use a
variable for the values.
This example writes to 4 coils starting at address 8289.
Example: write(m,'coils',8289,[0 1 0 1])
Data Types: double
Output Arguments
status
— Status of write operation
0
| 1
The status of the write operation.
0
: Indicates an unsuccessful write operation.1
: Indicates a successful write operation.
Data Types: uint8
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Version History
Introduced in R2022a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)