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.