read
Description
Examples
Use NVIDIA Jetson to Read from a Modbus Server
This example shows how to use modbus
function to
configure the NVIDIA®
Jetson™ TX2 as a Modbus® client and read from a remote 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 tx2ModbusClient.m
that configures the Jetson TX2 board as a client and connect to the remote server at address
'127.0.0.1'
and port 502
. This function acts as the
entry-point for code generation.
function out = tx2ModbusClient(target_func, address, count, val) %#codegen hwobj = jetson(); % Modbus object creation m = modbus(hwobj,'127.0.0.1',1478,'Mode','Client'); % Write to target register write(m, target_func, address, val); pause(0.1); % Read from a target register [out, ~] = read(m, target_func, address, count); pause(0.1); % Clear the Modbus object 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','tx2ModbusClient','examples'); cfg.CustomSource = fullfile('codegen','exe','tx2ModbusClient','examples','main.cu');
The main.cu
file is generated as part of code generation process. For
this example, this file can be used without any modification.
To generate CUDA code, use the codegen
command and pass the GPU code configuration object along with the
tx2ModbusClient
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 tx2ModbusClient
Use the runApplication
function to launch the executable on the TX2
board.
runApplication(hwobj,'tx2ModbusClient');
Input Arguments
target
— Target area to read
character vector | string
Target area to read, specified as a character vector or string. You can perform a
Modbus read operation on four types of targets: coils, inputs, input registers,
and holding registers, corresponding to the values 'coils'
,
'inputs'
, 'inputregs'
, and
'holdingregs'
. Target must be the first argument after the object
name. This example reads 8 coils starting at address 1.
Example: read(m,'coils',1,8)
Data Types: char
address
— Starting address to read from
double
Starting address to read from, specified as a double. Address must be the second argument after the object name. This example reads 10 coils starting at address 2.
Example: read(m,'coils',2,10)
Data Types: double
count
— Number of values to read
double
Number of values to read, specified as a double. Count must be the third argument
after the object name. If you do not specify a count, the default of
1
is used. This example reads 12 coils starting at address
2.
Example: read(m,'coils',2,12)
Data Types: double
Output Arguments
out
— Output data
vector
Data read from the target area as a N-by-1 vector, where N represents the number of registers.
When reading coil or discrete inputs, the read operation is 1-bit and the output data is logical.
For holding register or input registers, the read is 16-bit and the output is
uint16
datatype.
Data Types: logical
| uint16
status
— Status of read operation
0
| 1
The status of the read operation.
0
: Indicates an unsuccessful read operation, which means that the data received on theout
is invalid.1
: Indicates a successful read operation, which means that the data received on theout
is valid.
Data Types: uint8
Version History
Introduced in R2022a
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
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)