TPC/IP write failed

9 views (last 30 days)
Jeremy
Jeremy on 11 Jul 2023
Commented: Jeremy on 12 Jul 2023
Hello,
I want to make a connection between Matlab and Labview using TPC/IP protocol.
I tried to create a server TCP/IP using tcpserver matlab command.
I send via LabVIEW a "TEST_COMM;" message and I correctly read the data with matlab by using read command.
The issue is on the write. I want to send a message to labview (client) via matlab (server) with write command but matlab returns an error: "Failed to write from the server. A TCP/IP client must be connected to the server"
Code below:
clear
[~,hostname] = system('hostname');
hostname = string(strtrim(hostname));
address = resolvehost(hostname,"address");
server = tcpserver("0.0.0.0",4000);
datatoread = server.NumBytesAvailable;
while (datatoread==0)
datatoread = server.NumBytesAvailable;
end
data = server.read(2,"char");
data2 = server.read(base2dec(data,10), "char");
disp(data)
disp(data2)
disp(server)
if (data2=="TEST_COMM;")
%server.write("TEST_COMM_ACK;")
write(server,"TEST_COMM_ACK;","string");
end
Thanks in advance for your help

Answers (2)

Anavi Somani
Anavi Somani on 12 Jul 2023
Try this with a couple of changes in the code, where the client accepts, waits and closes the connection.
[~,hostname] = system('hostname');
hostname = string(strtrim(hostname));
address = resolvehost(hostname,"address");
server = tcpserver("0.0.0.0", 4000);
% Wait for a client to connect
client = server.accept();
datatoread = server.NumBytesAvailable;
while (datatoread == 0)
datatoread = server.NumBytesAvailable;
end
data = server.read(2, "char");
data2 = server.read(base2dec(data, 10), "char");
disp(data)
disp(data2)
disp(server)
if (data2 == "TEST_COMM;")
% Write data to the client
write(client, "TEST_COMM_ACK;", "string");
end
% Close the connection
client.close();
server.close();
In this updated code, the accept() method is used to wait for a client to connect to the server.
Once the client is connected, you can use the write() method on the client object to send data back to the LabVIEW client.
Remember to close the connection using client.close() and server.close() after you have finished communicating with the client. Make sure to update your LabVIEW code to establish a connection with the Matlab server before sending the "TEST_COMM;" message.
  3 Comments
Anavi Somani
Anavi Somani on 12 Jul 2023
The tcpserver object does not have a direct accept() method to accept incoming connections.
Try this if it helps.
server = tcpserver("0.0.0.0", 4000);
% Wait for a client to connect
while ~server.Connected
pause(0.1);
end
% Retrieve the client information
clientInfo = server.Message;
% Display the client information
disp(clientInfo);
% Read data from the client
data = server.read();
disp(data);
% Send data to the client
message = "TEST_COMM_ACK;";
server.write(message);
% Close the connection
server.close();
Please ensure that your LabVIEW code establishes a connection with the Matlab server using the correct IP address and port number (in this case, "0.0.0.0" and 4000).
And go through the document link suggested by Ram Sreekar.
Jeremy
Jeremy on 12 Jul 2023
ok thanks, it helps to debug,
now I have more information on the behavior, I tried this code:
clear server
server = tcpserver("localhost",5000,"ConnectionChangedFcn",@connectionFcn);
client = server.Connected;
while ~client
client = server.Connected;
pause(0.1);
end
disp(server)
data = server.read(12,"char");
disp(data);
function connectionFcn(src,~)
if src.Connected
disp("This message is sent by the server after accepting the client connection request.")
disp(src)
src.write("TEST_COMM_ACK;");
else
disp("Client has disconnected.")
disp(src)
end
end
I detect a connection to the server (connected pass to 1) but just after I receive a disconnection. On the Labview I receive well the TEST_COMM_ACK; data but I don't understand the deconnection:
>>tcp2
This message is sent by the server after accepting the client connection request.
TCPServer with properties:
ServerAddress: "127.0.0.1"
ServerPort: 5000
Connected: 1
ClientAddress: "127.0.0.1"
ClientPort: 58301
NumBytesAvailable: 12
Show all properties, functions
Client has disconnected.
TCPServer with properties:
ServerAddress: "127.0.0.1"
ServerPort: 5000
Connected: 0
ClientAddress: ""
ClientPort: []
NumBytesAvailable: 12
Show all properties, functions
TCPServer with properties:
ServerAddress: "127.0.0.1"
ServerPort: 5000
Connected: 0
ClientAddress: ""
ClientPort: []
NumBytesAvailable: 12
Show all properties, functions
10TEST_COMM;

Sign in to comment.


Ram Sreekar
Ram Sreekar on 12 Jul 2023
Hi Jeremy,
I see that you are trying to send data from MATLAB to LabView and vice-versa.
After reviewing your code, it appears that you have successfully utilized the ‘ tcpserver’ object in MATLAB to receive data from the LabVIEW client. However, the problem arises when you attempt to send data back to the LabVIEW client using the write function.
To resolve this issue, you can rectify the problem by substituting the 'write' function with the ‘ fwrite’ function. You can rectify this problem by substituting the write function with the ‘ fwrite’ function. This adjustment will enable you to effectively transmit data from the MATLAB server to the LabVIEW client.
You can refer to the sample code given below.
if (data2=="TEST_COMM;")
ack = "TEST_COMM_ACK;";
fwrite(client, ack 'char');
end
For more information about MATLAB and LabView data exchange over TCP/IP you can refer to the link given below.
Hope this helps you resolve your query.
  1 Comment
Jeremy
Jeremy on 12 Jul 2023
Hello, I have the same issue with fwrite. Matlab detects no connection to tcpserver.
Here the display:
>> untitled2
10
TEST_COMM;
TCPServer with properties:
ServerAddress: "0.0.0.0"
ServerPort: 4000
Connected: 0
ClientAddress: ""
ClientPort: []
NumBytesAvailable: 0
Show all properties, functions
Error using matlabshared.transportlib.internal.compatibility.LegacyBinaryMixin/writeHook
Failed to write from the server. A TCP/IP client must be connected to the server.
Error in matlabshared.transportlib.internal.compatibility.LegacyBinaryMixin/fwrite (line 100)
writeHook(obj, data, precision);
Error in untitled2 (line 21)
server.fwrite("TEST_COMM_ACK;");

Sign in to comment.

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!