MATLAB Answers

Receiving UDP data from Arduino

42 views (last 30 days)
Martin
Martin on 24 Jul 2013
Commented: shubhum awarchar on 17 Dec 2020
I am trying to send a string from my Arduino to Matlab using Ethernet. Sending from Matlab to the Arduino board is no problem using UDP and I have successful communication both ways using Processing instead of Matlab (also UDP).
I simply can't seem to read the UDP object. I'm sure the object is sent as the Processing application can read it.
For the most basic example, here is the code in Matlab:
remPort=8888;
host='10.0.0.177';
locPort=9055;
u = udp(host,'RemotePort',remPort,'LocalPort',locPort);
fopen(u)
A = fread(u);
fclose(u)
And on the Arduino:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xFF, 0xA0 };
byte ip[] = { 10, 0, 0, 177 };
unsigned int localPort = 8888;
EthernetUDP Udp;
void setup()
{
Ethernet.begin(mac, ip);
Udp.begin(localPort);
}
void loop () {
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write("Hi");
delay(50);
Udp.endPacket();
}
I get this warning:
Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period.
When using fread(u,'BytesAvailable') i get 0.
Any ideas?
Edit:
I have made further tests using serial connection, works like a charm both ways. The serial object seems very similar to the UDP object:
>> a
Serial Port Object : Serial-COM3
Communication Settings
Port: COM3
BaudRate: 9600
Terminator: 'LF'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 5
ValuesSent: 0
>> u
UDP Object : UDP-10.0.0.177
Communication Settings
RemotePort: 8888
RemoteHost: 10.0.0.177
Terminator: 'LF'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
As you see, data has been received only using serial.
Tried using fscanf instead of fread and to specify a string beeing reveived as fscanf(u,'%s'), but doesn't work for UDP. I get new warning:
Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.
Strange since the serial and UDP have the same terminator.

Accepted Answer

Martin
Martin on 30 Jul 2013
SOLVED
After many hours of pulling my hair it turns out it was the firewall blocking the port :-/
Somehow the Processing application could read it anyway.
Used fscanf for strings and fread for others.
  1 Comment
shubhum awarchar
shubhum awarchar on 17 Dec 2020
how did u solved the problem of port

Sign in to comment.

More Answers (1)

Nathan Crosty
Nathan Crosty on 24 Jul 2013
Edited: Nathan Crosty on 24 Jul 2013
One thing I can tell you is that your endianness will be opposite between the Arduino and Matlab. Other than that, your paremeters for BeginPacket are different than how I use them. Here is a copy paste of some working example code.
Setup (Run this once):
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Serial.begin(9600);
Eth.begin(mac);
Udp.begin(8888);
Loop:
IPAddress sendTo(192,168,1,80);
int remotePort = 8885;
Udp.beginPacket(sendTo, remotePort);
Udp.write("hello");
Udp.endPacket();
/*...delay...*/
  2 Comments
Nathan Crosty
Nathan Crosty on 25 Jul 2013
I just noticed that you are not being specific enough with your fread in Matlab. You need to specify a buffer size and a type, etc. for this to work. Check out the documentation for fread.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by