Read data from a connected MODBUS device
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
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
function to create a configuration object for the Jetson platform and assign it to the
Hardware property of the code
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');
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
runApplication function to launch the executable on the TX2
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
'holdingregs'. Target must be the first argument after the object
name. This example reads 8 coils starting at address 1.
address — Starting address to read from
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.
count — Number of values to read
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
out — Output data
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
status — Status of read operation
The status of the read operation.
0: Indicates an unsuccessful read operation, which means that the data received on the
1: Indicates a successful read operation, which means that the data received on the