No response: Matlab and Modbus TCP

Hi there i have this code to read data from Modbus register 40001:

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('',502);  
%   link = tcpip('',502);  
s = tcpip('',port);  
set(s, 'InputBufferSize', 30000); 

% Initialize serial port on specified com port
date_addr   = 40001;
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


% Specify Terminator

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,0,'uint8')           %Anzahl Register Low Byte
fwrite(s,2,'uint8')     %Anzahl Register High Byte

while ~s.BytesAvailable

I get no response. Matlab doesn't get back to the command prompt. The Modbus slave is a arduino with an ethernet shield. When i test with a modbus master (e.g. Modbus Poll), i can read Register 40001.


It looks to me that your sixth byte:

fwrite(s,1,'char')       %Anzahl Datenbytes 0x06

is a 1, not 0x06? This byte in the protocol is the payload data size, and if you have it set to '1', I doubt any good Modbus Slave will respond.

The second thing to check is it looks like you are writing to a Modbus Slave address of 255. In my experience, this isn't usually used as a valid ID (although it works with the protocol). Make sure the slave you are trying to communicate with will respond to a unit ID 255.

Good luck!

Need Your Help

Controlling symbol visibility iOS

ios iphone xcode sdk static-libraries

I'm developing a sdk which uses a third party library say "X".

What is the max size of localStorage values?

javascript html5 local-storage

Since localStorage (currently) only supports strings as values, and in order to do that the objects need to be stringified (stored as JSON-string) before they can be stored, is there a defined limi...