Modbus over TCP/IP
조회 수: 14 (최근 30일)
이전 댓글 표시
Hi everyone
I'm trying do implement the Modbus protocol over TCP/IP, to comunicate with the variable speed drive AVT32, manufactured by Schneider. I know there is a TCP/IP block for simulink models, and in fact, I can comunicate successfully with the AVT32. However, I need to do the same using a "m" file, and, for some reason I'm not been able to find, I could't have success yet. Please help me to figure out my mistakes.
The code I'm using is the following:
%-----------//-------------------
%configuration of TCP/IP channel
tcpip_pipe=tcpip(192.168.1.20, 502); %IP and Port of ATV32 set(tcpip_pipe, 'InputBufferSize', 512); tcpip_pipe.ByteOrder='bigEndian';
try if ~strcmp(tcpip_pipe.Status,'open') fopen(tcpip_pipe); end disp2('TCP/IP Open', handles); catch err disp2('Error: Can''t open TCP/IP', handles); end
%apparently, the channel is successfully opened.
transID = uint16(0); %16b Transaction Identifier ProtID = uint16(0); %16b Protocol ID (0 for ModBus) Lenghf = uint16(6); %16b Remaining bits (6) UnitID = uint16(0); %Unit ID (0) UnitID = bitshift(UnitID,8); FunCod = uint16(6); %Function code: write (6) UnitIDFunCod = bitor(FunCod,UnitID); %Concatenation of UnitID & FunctionCode %in one uint16 word
%According to modbus protocol, UnitID and Function code are 8bit data. In order to maintain the same data tipe in vector "message", I converted each of them to uint16, and used "bitor" to create a uint16 word when the MSB is the UnitID and the LSB is the function code
Add = uint16(8501); %16b Adress of the resister (8501) Val = uint16(value); %16b Data (5)
message = [transID ProtID Lenghf UnitIDFunCod Add Val]; disp2(message,handles); fwrite(tcpip_pipe, message);
--------------///-----------------------
after the execution of "fwrite" funcion, I'm able to verify that ATV32 doesn't receive any data, or at least in the right way.
I'll apreciate very much any help
Best Regards Justino Rodrigues
댓글 수: 3
Trung Bui
2020년 6월 3일
Add = uint16(03); % 16b Adress of the resister (8501)
How 03 in uint16 is 8501? I dont get it
답변 (2개)
Tomasz
2013년 7월 11일
I would like to ask if somebody could bestow (give :)) some mfiles related to Modbus TCP/IP or RTU implemented in Matlab.
I will be very grateful.
I am designing and building/programming a connection between Atmega 128 and Matlab using Modbus protocol. I have programmed Atmega as Modbus Slave and also Master. Now i am looking for Matlab implementation with Modbus.
Best wishes Tom
댓글 수: 1
Eric Wetjen
2017년 3월 10일
MODBUS TCP/IP support was added to Instrument Control Toolbox in R2017a. See this resource page for details.
Jeff
2016년 12월 22일
I got communication with PLC (Automation direct P2) over Modbus TCP/IP: 1, 3, 5, 6, 15, and 16 commands. I switched from 16 bit to 8 bit commands (compared to example above) in order to handle odd bytes in 15 and 16 commands.
see attachment
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Modbus Communication에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!