Matlab and Modbus over TCP

2 views (last 30 days)
Andreas
Andreas on 29 Jan 2014
Hello there,
i have this code
function [s] = serialstart(opt)
% Funtion for initializing a serial interface in matlab for interfacing
% Functions using the serial port must be passed the serial port object
% s in order for the serial port to be acessable.
port = 502;
% link = tcpip('127.0.0.1',502);
% link = tcpip('10.64.15.101',502);
s = tcpip('192.168.2.177',port);
%?????
set(s, 'InputBufferSize', 3000);
% Initialize serial port on specified com port
date_addr = 40001;
date_num=1;
date_addr_high = floor(date_addr/100);
date_addr_low = mod(date_addr,100);
date_num_high = floor(date_num/100);
date_num_low = mod(date_num,100);
%Open serial connection
fopen(s);
% Specify Terminator
s.terminator='CR/LF';
fwrite(s,0,'char') %Transactio identifier 0x00
fwrite(s,0,'char') % Transactio identifier 0x00
fwrite(s,0,'char') % Protokol identifier 0x00
fwrite(s,0,'char') % Protokol identifier 0x00
fwrite(s,0,'char') % Anzahl Datenbytes 0x00
fwrite(s,1,'char') % Anzahl Datenbytes 0x06
fwrite(s,255,'char') % unit identifier 0xff
fwrite(s,3,'uint8') % Funktion 0x03
fwrite(s,date_addr_high,'uint8') %Startadresse High Byte
fwrite(s,date_addr_low,'uint8') %Startadresse Low Byte
fwrite(s,date_num_high,'uint8') %Anzahl Register Low Byte
fwrite(s,date_num_low,'uint8') %Anzahl Register High Byte
while ~s.BytesAvailable
end
s.BytesAvailable
res=fread(s,s.BytesAvailable)
fclose(s);
The code should read the data of register 40001 The problem is that i get no response. Matlab doesnt't come back to the prompt.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!