- https://www.mathworks.com/help/matlab/ref/try.html?s_tid=doc_ta
- https://www.mathworks.com/help/matlab/ref/pause.html?s_tid=doc_ta
Read OK but I can't Write in PLC by TCP/IP client.
6 views (last 30 days)
Show older comments
I have a comunicatio betwen PLC Mitsubhishi Q03UDE and MatLab by TCP/IP. I can read register and memories, but I cant write. When I use the 'write' command to request the reading of the registers, it returns the values correctly. However, when I try to use 'write' to write into the PLC, it doesn't write.
% Define the frame structure
frame.header = [hex2dec('50'), 0, 0, hex2dec('ff'), hex2dec('ff'), 3, 0];
frame.length = [hex2dec('0c'), 0];
frame.timer = [hex2dec('20'), 0];
frame.command = [1, 4];
frame.sub_command = [1, 0];
frame.start_addr = [hex2dec('64'), 0, 0];
frame.device = hex2dec('90');
frame.points = [8, 0];
frame1.header = [hex2dec('50'), 0, 0, hex2dec('ff'), hex2dec('ff'), 3, 0];
frame1.length = [hex2dec('10'), 0];
frame1.timer = [hex2dec('20'), 0];
frame1.command = [1, hex2dec('14')];
frame1.sub_command = [0, 0];
frame1.start_addr = [hex2dec('64'), 0, 0];
frame1.device = hex2dec('a8');
frame1.points = [1, 0];
frame1.w_data = [hex2dec('64'), 0];
s = tcpclient('192.168.3.38', 2500, 'Timeout', 10);
write(s, uint8(struct2array(frame)));
data = read(s, 15);
write(s, uint8(struct2array(frame1)));
0 Comments
Answers (1)
SANKALP DEV
on 15 Dec 2023
Hello Jose,
I understand that you are experiencing difficulties with writing data to a Mitsubishi PLC (Q03UDE) from MATLAB using TCP/IP communication.
To resolve the issue, you can consider implementing robust error handling using try-catch blocks in MATLAB to capture and display any exceptions that may occur during communication and utilize MATLAB debugging tools to step through the code, inspect variable values, and print/display intermediate results for better visibility.
Wrapping the code in a try-catch block will allow you to catch any exceptions that may occur during the execution of the code and handle them appropriately.
Secondly, you can also add a delay between writing the first and the second frame, the delay is necessary to allow the plc sufficient time to process the write operation. You can use MATLAB’s ‘pause’ function to add delay.
Refer to the following documentation to know more about try, catch statements, and ‘pause’ function respectively in MATLAB: -
I hope it helps.
Thanks, and regards,
Sankalp dev.
0 Comments
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!