udp fread issue
Show older comments
I am sending data through ethernet from my pc to a virtex 6 fpga board. I am able to save the data in the fpga perfectly. Now I am having problems reading back data when needed. here is my matlab code:
u = udp('192.168.1.255','localport',9091,'remoteport',7,'timeout',15,'DatagramTerminateMode','off','timeout',15,'inputbuffersize',1200,'outputbuffersize',1200);
fopen(u);
fwrite(u,dataout,'uint8');
read= fread(u);
but read comes back empty and the following error message is seen:
Warning: The specified amount of data was not returned within the
Timeout period.
any help would be great! thanks!
Answers (2)
Walter Roberson
on 20 Jun 2012
0 votes
You turned off datagramterminatemode, so fread() is not going to look for any kind of terminator. Instead it is going to look only for timeout or for the input buffer to be full. You have set that size to 1200, so if you do not receive 1200 bytes of data before the timeout (15 seconds) then you are going to get that warning message.
44 Comments
Walter Roberson
on 20 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
I note that you are using udp port 7. That port is, by convention, used for the ECHO service, http://tools.ietf.org/html/rfc862 and the ECHO service would not save the data at the other end (though I guess it isn't prohibited from doing so.) Are you attempting to create an ECHO service, or was it just by chance you used that port number?
ECHO is intended to respond datagram by datagram, and the design is intended for datagram mode such as is discussed at http://msdn.microsoft.com/en-us/library/ms881658.aspx with ends using network library recvfrom() and sendto() . Are you using pre-written software on the fpga side, or do you have access to the source code?
I would also be concerned about whether the fpga knows its own address; see the remarks at http://msdn.microsoft.com/en-us/library/aa450870.aspx
I would recommend that you turn on a network sniffer and watch the packets in detail; remember to set the sniffer to pay attention only to that one network interface.
matthew martinez
on 20 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
matthew martinez
on 20 Jun 2012
Walter Roberson
on 20 Jun 2012
Destination address 192.168.1.255 is probably not right. It is not _definitely_ wrong, but it is incorrect as a response packet. 192.168.1.255 would be the "broadcast address" for the 192.168.1.x network, and MATLAB would definitely not pick up on such packets as a reply.
It is unlikely that you want to be using 192.168.1.255 as your destination address for "u". You should only need to open one socket (sockets are bidirectional) and that should be like the one you use for "f".
There is a standard that reserves ports in the range 1025 to 32767, but that isn't followed all that much. Anything from 1025 to 65535 as the port number should be okay provided there isn't already something running on the port and provided the two ends agree. The port on the client (the MATLAB program you show above) does not need to be agreed to ahead of time: the server will read the sender's port number out of the packet and reply to it. The port on the server _does_ need to be agreed upon.
I'd say remove (comment out) your "u" and write to "f".
Say, did you notice that you didn't fopen(f) in your code? Can't read from a port that isn't open.
matthew martinez
on 20 Jun 2012
matthew martinez
on 20 Jun 2012
Walter Roberson
on 20 Jun 2012
Could you show your current code? And also indicate what WireShark sees for the addresses and ports for the outgoing packets (and response packets if you are getting any)
You should probably not be using 192.168.1.255 for anything. There are uses for it, but not in this context.
matthew martinez
on 20 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
Walter Roberson
on 20 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
You need the remoteport to be specified in the client (the one initiating the connection.) If it is defaulting to 0 then you would not get any output packets.
Ummm, the 192.168.1.10 in the udp() call needs to be the address of the destination. Your wireshark trace is showing that you are using that as the address of the source -- you have configured that as the IP address of that ethernet adapter. Right-click on the adapter icon (in Windows) and adjust the properties. If the IP stack thinks that the packets are destined for the same machine that sent them, then the packets might not go into the queue to be sharked.
=====
As an enhancement: consider building your data as uint8:
pre = 36*ones(8,1,'uint8'); %%%for 16-bit data this is an 8-byte long sequence of "$"
post = 126*ones(8,1,'uint8');
x_out = zeros(64,1,'uint8');
data_out = [pre; x_out; post];
This will not change the data that goes onto the wire, but it makes it clearer to the reader that you are not planning to do things like send all 64 bits of the floating point zero() array you construct.
matthew martinez
on 20 Jun 2012
matthew martinez
on 20 Jun 2012
Walter Roberson
on 20 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
The IP address of the adapter on the MATLAB system needs to be in the same network as the IP address of the fpga, and it must not end in .0 or in .255 (well, those aren't _always_ impossible, but at this level they are.) The address must also not be the same as the IP address of the fpga.
It is the IP address of the fpga that needs to go into the udp() call.
To change your IP address, see http://windows.microsoft.com/en-us/windows-vista/change-tcp-ip-settings and use the fixed IP address variation.
Walter Roberson
on 20 Jun 2012
Servers are not required to pay attention to broadcast packets (though they often do.)
Your fpga might not be on the same IP network as your ethernet interface, so those packets might not be broadcast packets as far as the fpga is concerned.
Sending data to a broadcast address is intended to be a privileged operation. It appears that your system is allowing it anyhow.
You cannot specify the broadcast address as the IP address to listen to because it is not valid for packets to use that IP address as their source address. (I've seen it happen, but it _is_ formally disallowed.)
matthew martinez
on 20 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
Walter Roberson
on 20 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
You can only find the IP address of the fpga if it cooperates (or is a little broken.)
There are some circumstances under which you can find its address if you do not know the IP network already (major one: if it implements a DHCP server; other methods pretty much rely on weaknesses in network implementations.)
If you already know the correct IP network, then if it is cooperating, you send an ICMP ECHO packet to the broadcast address, and it might reply. This ICMP ECHO is not the same as the UDP ECHO we were discussing above. And unfortunately it appears to me that the Instrument Control Toolbox does not support ICMP packets, so you might have to start a system() command and parse the output. If, that is, you want to do this automatically; otherwise just
!ping 192.168.1.255
(complete with '!')
MATLAB does not itself implement any way to discover the address. Your FPGA development kit might use a documented default address, though.
matthew martinez
on 20 Jun 2012
Walter Roberson
on 21 Jun 2012
The ! is only used inside MATLAB.
matthew martinez
on 21 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
Walter Roberson
on 21 Jun 2012
Remember, u is commented out, so you are still using .10 as your IP address.
What IP address is showing up for the fpga ?
matthew martinez
on 21 Jun 2012
matthew martinez
on 21 Jun 2012
Walter Roberson
on 21 Jun 2012
Could you post an example of the wireshark capture of the outgoing packet and the capture of the response packet?
matthew martinez
on 21 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
matthew martinez
on 21 Jun 2012
Walter Roberson
on 21 Jun 2012
I'm not sure where the 3e were coming from before; you are constructing 0's for the middle, so it should be 0's.
I do not see anything obviously wrong with the outgoing packet. There doesn't happen to be enough shown for me to have a look at the checksum. If you click on the + beside Ethernet or beside Internet Protocol it might show you additional information.
Is there evidence that the fpga is alive and listening to that particular ethernet port? For example can you go on to the fpga and ask it to ping 192.168.1.3 ?
Is the ethernet port shared with your general network connection? If not then you should go into the network properties and turn off DHCP and Microsoft Networking (NETBUI?), as those are generating unneeded packets. Which the fpga _should_ ignore, but no need to confuse things.
matthew martinez
on 21 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
matthew martinez
on 21 Jun 2012
matthew martinez
on 21 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
Walter Roberson
on 21 Jun 2012
Hmmmm... could you try an experiment in which you leave your MATLAB machine set to 192.168.1.3, and leave the fpga set to 192.168.1.18, and in your code change the udp destination to 192.168.1.255 ? Also, for this experiment, please have the localport and destination port different from each other (by setting the local port)
matthew martinez
on 21 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
matthew martinez
on 21 Jun 2012
Walter Roberson
on 22 Jun 2012
Sorry I need to see the run with the port numbers set explicitly and set to different values (and I need to know the two values)
Please re-check the IP address that is set for the fpga.
The packet you show has some unexpected properties that the ports should help me to resolve.
matthew martinez
on 22 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
Walter Roberson
on 22 Jun 2012
Odd....
I need to know which port was set as local on the matlab side, and which was set as remote?
matthew martinez
on 22 Jun 2012
matthew martinez
on 22 Jun 2012
Walter Roberson
on 22 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
I cannot say that I am certain, but it appears to me that the fpga is badly messing up the packets.
I think when the broadcast packets reach it, it is responding, but instead of putting its own MAC address (ethernet address) into the packet as the source, it is simply exchanging the source and destination MAC addresses, leaving the rest of the header alone (including leaving the IP addresses alone and leaving the ports alone), but it is changing the data to 3E as you expect.
The MATLAB side is probably ignoring the response because (A) the MAC address of the source is invalid; and (B) it knows that its IP address is not 192.168.1.255 and that the packet is not a broadcast packet. Or possibly just (B) alone. Then after that if it _had_ let the packet through it would have tried to deliver it to the wrong port -- but that is something that could be worked around by using the same source and destination port numbers. The fact that you received no response when you were using the same source and destination ports tells me it is likely to be either (A) or (B).
The entire IP stack for the fpga might be messed up. Or, possibly more simply, when the packet comes in, the code that is processing the packet and changing the 00 to 3E is not doing the work of switching the fields. Switching the fields is done automatically in standard network libraries, but since the fpga might not be running a standard network library, possibly the code has to do it explicitly.
The problem with no response except to the broadcast address: I can think of more than one possible reason for that. If we stick to the ID that the source and destination MAC are switched around but the other headers are not being constructed properly, then although the destination MAC ought to be right, with the other headers wrong (e.g., the IP address of the destination would be its own IP address), it could be that the fpga is discarding the packet before it gets out of the network connection. This is a hypothesis, but I don't think it is an especially strong one.
and ask it to scan 192.168.1.18 fairly thoroughly, including a specific scan for udp port 9000 to 9999. I would do this with wireshark running in order to see what (if any) response packets came back even if those response packets were corrupt. (nmap will report on returns that it can understand but that depends on the packet getting back to the right place!)
If nmap says that the host is down, then nmap the entire subnet and see if anything happens.
matthew martinez
on 22 Jun 2012
matthew martinez
on 22 Jun 2012
Walter Roberson
on 22 Jun 2012
If you do use nmap and the host will not ping, then you may have to use a specific option to force nmap to continue anyhow.
Walter Roberson
on 22 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
C will not have any more success if the fpga is simply not responding.
You can see from Wireshark that the packets are going out towards the fpga and appear to be valid. I would think it quite unlikely that the MATLAB level is introducing a bug in the packet, as MATLAB is almost certainly invoking the same standard network libraries that C would call upon.
The nmap people also make ncat which allows you to flexibly build individual packets. See http://nmap.org/ncat/
matthew martinez
on 22 Jun 2012
Walter Roberson
on 22 Jun 2012
Edited: Walter Roberson
on 8 Aug 2019
Hmmm, messy. You would need to socket() AF_INET SOCK_RAW
http://sock-raw.org/papers/sock_raw has some fairly dense technical details. There must be something around with a better overview.
(My university days predate TCP/IP being finalized...)
matthew martinez
on 26 Jun 2012
matthew martinez
on 20 Jun 2012
0 votes
3 Comments
Kavya Managundi
on 9 Oct 2018
Edited: Walter Roberson
on 8 Aug 2019
I think this is of some help here.
YONG LI
on 5 Aug 2019
now,I use TCPIP to receive data from the FPGA and the display is empty.display warning:The specified amount of data was not returned within the Timeout period.'tcpip' unable to read any data. For more information on possible reasons, see TCPIP Read Warnings.
Walter Roberson
on 8 Aug 2019
YONG LI please create a new Question for this, and please post your code there, and also please post wireshark traces showing what packets are being exchanged with the FPGA. If possible, please use a separate network interface to talk to the fpga, so that the rest of your network traffic is not mixed in.
Categories
Find more on AMD FPGA and SoC Devices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!