Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
problem with UDP and fclose

Subject: problem with UDP and fclose

From: Matthew

Date: 21 Jul, 2011 15:06:08

Message: 1 of 3

Hi, I am working with UDP as shown here:
----
 u1=udp('','LocalPort',port,'Timeout',inf, ...
        'DatagramReceivedFcn',@udplatlonrespond, ...
        'UserData',respondfcn);
    fopen(u1);
---
This code works OK. But if I try fclose('all') or fclose(u1) it does not appear to close the port. So if I run the program again, I get the error:

??? Error using ==> icinterface.fopen at 83
Address already in use

--
So how can I close the port so I could run the program again?

Thanks Matt

Subject: problem with UDP and fclose

From: Trent Jarvi

Date: 22 Jul, 2011 15:04:51

Message: 2 of 3


"Matthew " <mattinjersey@yahoo.com> wrote in message
news:j09f90$n13$1@newscl01ah.mathworks.com...
> Hi, I am working with UDP as shown here:
> ----
> u1=udp('','LocalPort',port,'Timeout',inf, ...
> 'DatagramReceivedFcn',@udplatlonrespond, ...
> 'UserData',respondfcn);
> fopen(u1);
> ---
> This code works OK. But if I try fclose('all') or fclose(u1) it does not
> appear to close the port. So if I run the program again, I get the error:
> ??? Error using ==> icinterface.fopen at 83
> Address already in use
>
> --
> So how can I close the port so I could run the program again?
>

Hi Matt,

I did a quick test to see if I could reproduce the issue you observed:

>> u1=udp('localhost','RemotePort',16000,'LocalPort',16001,'Timeout',inf,
>> 'DatagramReceivedFcn',@instrcallback );
>> instrreset
>> u1=udp('localhost','RemotePort',16000,'LocalPort',16001,'Timeout',inf,
>> 'DatagramReceivedFcn',@instrcallback );
>> u2=udp('localhost','RemotePort',16001,'LocalPort',16000,'Timeout',inf);
>> fopen([u1 u2])
>> fprintf(u1,'asdf');
>> fprintf(u2,'asdf');
DatagramReceived event occurred at 10:55:09 for the object: UDP-localhost.
5 bytes were received from address 127.0.0.1, port 16000.
>> fclose([u1 u2])
>> fopen([u1 u2])
>> fprintf(u2,'asdf');
DatagramReceived event occurred at 10:55:31 for the object: UDP-localhost.
5 bytes were received from address 127.0.0.1, port 16000.
>> fclose([u1 u2])

There may be some confusion with FCLOSE. There is an iofun FCLOSE used to
close files which accepts 'all' as a parameter. You basically closed any
files opened in MATLAB. Since you probably had no files open, it returns
successfully. What you may have been interested in doing is
fclose(instrfindall);

To get the right help with commands used in several places, you can provide
more information on the command line like the following.

help udp/fopen
help iofun/fopen
which -all fopen

Subject: problem with UDP and fclose

From: Matthew

Date: 22 Jul, 2011 18:31:10

Message: 3 of 3

"Trent Jarvi" wrote in message <j0c3ij$b1h$1@newscl01ah.mathworks.com>...
>
> "Matthew " <mattinjersey@yahoo.com> wrote in message
> news:j09f90$n13$1@newscl01ah.mathworks.com...
> > Hi, I am working with UDP as shown here:
> > ----
> > u1=udp('','LocalPort',port,'Timeout',inf, ...
> > 'DatagramReceivedFcn',@udplatlonrespond, ...
> > 'UserData',respondfcn);
> > fopen(u1);
> > ---
> > This code works OK. But if I try fclose('all') or fclose(u1) it does not
> > appear to close the port. So if I run the program again, I get the error:
> > ??? Error using ==> icinterface.fopen at 83
> > Address already in use
> >
> > --
> > So how can I close the port so I could run the program again?
> >
>
> Hi Matt,
>
> I did a quick test to see if I could reproduce the issue you observed:
>
> >> u1=udp('localhost','RemotePort',16000,'LocalPort',16001,'Timeout',inf,
> >> 'DatagramReceivedFcn',@instrcallback );
> >> instrreset
> >> u1=udp('localhost','RemotePort',16000,'LocalPort',16001,'Timeout',inf,
> >> 'DatagramReceivedFcn',@instrcallback );
> >> u2=udp('localhost','RemotePort',16001,'LocalPort',16000,'Timeout',inf);
> >> fopen([u1 u2])
> >> fprintf(u1,'asdf');
> >> fprintf(u2,'asdf');
> DatagramReceived event occurred at 10:55:09 for the object: UDP-localhost.
> 5 bytes were received from address 127.0.0.1, port 16000.
> >> fclose([u1 u2])
> >> fopen([u1 u2])
> >> fprintf(u2,'asdf');
> DatagramReceived event occurred at 10:55:31 for the object: UDP-localhost.
> 5 bytes were received from address 127.0.0.1, port 16000.
> >> fclose([u1 u2])
>
> There may be some confusion with FCLOSE. There is an iofun FCLOSE used to
> close files which accepts 'all' as a parameter. You basically closed any
> files opened in MATLAB. Since you probably had no files open, it returns
> successfully. What you may have been interested in doing is
> fclose(instrfindall);
>
> To get the right help with commands used in several places, you can provide
> more information on the command line like the following.
>
> help udp/fopen
> help iofun/fopen
> which -all fopen
>
>
Thanks Trent. I may have an older version of Matlab at home, because I just tried it with Matlab 2011a and it is working for me.
- Matt

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us