Python and MATLAB in the socket
62 ビュー (過去 30 日間)
I need a way to communicate dynamically between Python and MATLAB, so I am currently trying to connect python and MATLAB through socketing. Although I can get Python to work with socket and MATLAB with the tcpip functionality, they seem unable to sense each other despite being connected to the same tcp port. I can write to the port in each environment and read the data, but one environment cannot seem to detect the data sent it by the other.
path=input('Please enter the location of the GDSII file: ','s');
sock = tcpip('localhost',1722);
from Tkinter import *
from gdspy import *
from socket import *
#Use this port to read data from matlab
socky = socket(AF_INET,SOCK_STREAM)
port2 = 1722
addr2 = (host2,port2)
print "socky connected"
print socky.recv(78) #print path
The recv function just hangs when running in either Python or MATLAB, so I am not sure what the problem is.
Thank you in advance for any help.
回答 (1 件)
Robert Snoeberger 2015 年 6 月 28 日
Connect to Python echo server from MATLAB. The echo server code is given as an example in the Python socket documentation. See here .
Python echo server script - echoserver.py
# Echo server program
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn, addr = s.accept()
print 'Connected by', addr
data = conn.recv(1024)
if not data: break
Python code was obtained from the Python socket documentation.
MATLAB client code
>> % start the echo server
>> !python echoserver.py &
>> % connect to the server
>> t = tcpip('localhost', 50007);
>> % write a message
>> fwrite(t, 'This is a test message.');
>> % read the echo
>> bytes = fread(t, [1, t.BytesAvailable]);
This is a test message.
>> % close the connection