Receiving UDP data from Arduino

조회 수: 22 (최근 30일)
Martin
Martin 2013년 7월 24일
댓글: shubhum awarchar 2020년 12월 17일
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.

채택된 답변

Martin
Martin 2013년 7월 30일
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
shubhum awarchar
shubhum awarchar 2020년 12월 17일
how did u solved the problem of port

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Nathan Crosty
Nathan Crosty 2013년 7월 24일
편집: Nathan Crosty 2013년 7월 24일
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
Martin
Martin 2013년 7월 25일
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 2013년 7월 25일
편집: Nathan Crosty 2013년 7월 25일
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by