Receiving UDP data from Arduino

8 visualizaciones (últimos 30 días)
Martin
Martin el 24 de Jul. de 2013
Comentada: shubhum awarchar el 17 de Dic. de 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.

Respuesta aceptada

Martin
Martin el 30 de Jul. de 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 comentario
shubhum awarchar
shubhum awarchar el 17 de Dic. de 2020
how did u solved the problem of port

Iniciar sesión para comentar.

Más respuestas (1)

Nathan Crosty
Nathan Crosty el 24 de Jul. de 2013
Editada: Nathan Crosty el 24 de Jul. de 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 comentarios
Martin
Martin el 25 de Jul. de 2013
Our code for the Arduino is the same, including the parameterization.
Are you sending to a Matlab application? What code do you use?
I'm not familiar with endianness issues, but isn't it strange that I can send a string from Matlab to Arduino (and it beening interpreted correctly) but not the other way?
Nathan Crosty
Nathan Crosty el 25 de Jul. de 2013
Editada: Nathan Crosty el 25 de Jul. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by