0

I am trying to send datetimes to simulink via python. (I have to use simulink, not just MATLAB)

Here is my python program for a UDP server

import socket
from datetime import datetime

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("127.0.0.1", 12345))

while True:
     data, addr = sock.recvfrom(256)
     print(data)
     message = bytes(str(datetime.now()), encoding='utf-8')
     sock.sendto(message, addr)

And here is my attempt at writing a receiver in Simulink enter image description here

I have set the simulink model to run indefinitely but I get an error:

OSError: [Errno 48] Address already in use from python

Isn't it the point to use the same address to communicate? I understand the error is because it's between MATLAB and Python, so how do I get around it?

Po Chen Liu
  • 253
  • 2
  • 12
  • 1
    The sample time is how often that block will be executed. If you want the model to run indefinitely, you need to set the model's solver "Stop Time" to "inf". This can be done from the command prompt using `set_param(gcs,'StopTime','inf')` – scotty3785 Mar 04 '21 at 16:58
  • 1
    You also probably want the sample time of that block to be much more frequent (like 0.5 seconds at least) since you python code will be continuously be sending out UDP datagrams. – scotty3785 Mar 04 '21 at 16:59
  • @scotty3785 I have an error saying OSError: [Errno 48] Address already in use from python. Isn't it the point to use the same address to communicate? – Po Chen Liu Mar 08 '21 at 01:47
  • 1
    That is a common issue and one that already has an answer. https://stackoverflow.com/questions/6380057/python-binding-socket-address-already-in-use you need to tell python to reuse the port. – scotty3785 Mar 08 '21 at 08:56
  • 1
    However, you may need to send/receive data on different ports. If both applications (MATLAB/Python) are trying to listen on the same port, it might not work. Use 12345 (for example) for python to simulink and 12346 (for example) for simulink to python. – scotty3785 Mar 08 '21 at 09:51
  • 1
    Was the Simulink program running as UDB server? Also run `netstat -a` to check if something else is already listening to port 12345. – yoonghm Mar 13 '21 at 15:53
  • 1
    Is it possible if you use Python as your server? As simulink seems to be the client. Run the simulink first then run the Python script. – thethiny Mar 14 '21 at 06:51
  • 1
    It appears you're setting both simulink and python to be server sockets listening on the same port. What you want is to have one of them be the server socket (it looks like simulink is the server here) and have python connect to that socket instead of binding to the port as another server socket. Your python socket should therefore not "bind", it should "connect" to the port. – polortiz40 Mar 15 '21 at 00:49
  • 1
    @thethiny, why do you think simulink is the client here? We can agree this Python program is definitely attempting to setup a server connection, but the fact that OP gets "Address already in use" to me suggests they already have a server running, which points to simulink. Additionally, https://www.mathworks.com/help/instrument/udpreceive.html suggests simulink binds a port, making it a server that waits for a client connection. – polortiz40 Mar 15 '21 at 01:01
  • 1
    @polortiz40 I have mixed the two together, I meant Python is client and Simulink as server. My apologies. – thethiny Mar 15 '21 at 09:30

0 Answers0