Sending information to MATLAB from Python via TCP
I want to send data from Python to MATLAB every 10 seconds via TCP. The code I have works for the first iteration of the loop, but on the second iteration we reestablish the connection, yet the data fails to send.
The relevant Python code is:
import socket
import sys
import time
%Create a TCP/IP socket
i = 0 %loop counter
j=45.395 %data to be sent to MATLAB
while i < 1000:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((‘localhost’, 51001))
s.listen(1)
conn, addr = s.accept()
print(f"Connected by {addr}")
conn.sendall(bytes(str(j), ‘ASCII’))
conn.close()
i+=1
j+=0.6
time.sleep(10)
The MATLAB code is
i=0;
while i < 1000
t = tcpclient(‘localhost’, 51001);
output = read(t);
data = char(output(1:4));
pwrcmd = str2double(data);
fprintf(‘%f’, pwrcmd);
clear t;
i = i+1;
pause(10)
end
and finally the output on the terminal is:
>python matlab_connect_test.py
Connected by (‘127.0.0.1’, 56010)
Connected by (‘127.0.0.1’, 56012)
The first thing I tried was to establish just one server-client connection, and send data every 10 seconds through that one connection, rather than reconnecting every 10 seconds. That was unsuccessful. For some reason even though the second connection is established, MATLAB never receives the second value of j, or any subsequent values. Any help would be very much appreciated.I want to send data from Python to MATLAB every 10 seconds via TCP. The code I have works for the first iteration of the loop, but on the second iteration we reestablish the connection, yet the data fails to send.
The relevant Python code is:
import socket
import sys
import time
%Create a TCP/IP socket
i = 0 %loop counter
j=45.395 %data to be sent to MATLAB
while i < 1000:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((‘localhost’, 51001))
s.listen(1)
conn, addr = s.accept()
print(f"Connected by {addr}")
conn.sendall(bytes(str(j), ‘ASCII’))
conn.close()
i+=1
j+=0.6
time.sleep(10)
The MATLAB code is
i=0;
while i < 1000
t = tcpclient(‘localhost’, 51001);
output = read(t);
data = char(output(1:4));
pwrcmd = str2double(data);
fprintf(‘%f’, pwrcmd);
clear t;
i = i+1;
pause(10)
end
and finally the output on the terminal is:
>python matlab_connect_test.py
Connected by (‘127.0.0.1’, 56010)
Connected by (‘127.0.0.1’, 56012)
The first thing I tried was to establish just one server-client connection, and send data every 10 seconds through that one connection, rather than reconnecting every 10 seconds. That was unsuccessful. For some reason even though the second connection is established, MATLAB never receives the second value of j, or any subsequent values. Any help would be very much appreciated. I want to send data from Python to MATLAB every 10 seconds via TCP. The code I have works for the first iteration of the loop, but on the second iteration we reestablish the connection, yet the data fails to send.
The relevant Python code is:
import socket
import sys
import time
%Create a TCP/IP socket
i = 0 %loop counter
j=45.395 %data to be sent to MATLAB
while i < 1000:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((‘localhost’, 51001))
s.listen(1)
conn, addr = s.accept()
print(f"Connected by {addr}")
conn.sendall(bytes(str(j), ‘ASCII’))
conn.close()
i+=1
j+=0.6
time.sleep(10)
The MATLAB code is
i=0;
while i < 1000
t = tcpclient(‘localhost’, 51001);
output = read(t);
data = char(output(1:4));
pwrcmd = str2double(data);
fprintf(‘%f’, pwrcmd);
clear t;
i = i+1;
pause(10)
end
and finally the output on the terminal is:
>python matlab_connect_test.py
Connected by (‘127.0.0.1’, 56010)
Connected by (‘127.0.0.1’, 56012)
The first thing I tried was to establish just one server-client connection, and send data every 10 seconds through that one connection, rather than reconnecting every 10 seconds. That was unsuccessful. For some reason even though the second connection is established, MATLAB never receives the second value of j, or any subsequent values. Any help would be very much appreciated. python, matlab, tcp, tcpclient, server MATLAB Answers — New Questions