TPC/IP write failed
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (2)
Anavi Somani
le 12 Juil 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 commentaires
Anavi Somani
le 12 Juil 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.
Ram Sreekar
le 12 Juil 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.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!