Yancy
Yancy

Reputation: 91

How to use MATLAB to send signals to a port of an IP address?

How to use MATLAB to send signals to a port of an IP address?

The fact is that I have connected a robot car with PC by Wifi and I've decided that the signal FF000400FF received by the robot will drive the car forward. I tested this by TCP&UDP tools and it was OK. So I think it should have been OK using MATLAB to send this signal, too.

my code is:

t = tcpip('192.168.1.1',2001);
fopen(t);
a=dec2hex(1095216922879);  %FF000400FF(H)
fprintf(t,a)

but it doesn't work. I've been working on this for long but failed.

Upvotes: 9

Views: 358

Answers (2)

Jeff E Mandel
Jeff E Mandel

Reputation: 81

Try this:

myhost = java.net.InetSocketAddress('192.168.1.1',2001);
mysoc = java.net.Socket();
mysoc.connect(myhost, 2000); % Two second timeout
connected=mysoc.isConnected();
if connected
    mystream = mysoc.getOutputStream();
    mystream.write(hex2dec('FF000400FF'));
    mystream.flush();
    mystream.close();
end

mysoc.close();

You can wrap this in a try catch block:

try
...
catch ME
    if (isa(ME, 'matlab.exception.JavaException'))
       reason = class(ME.ExceptionObject);
       connected = 0;
    end
end

This should work on any platform, desktop or deployed. If your car returns something in response to the command, you could read it with an inputStream. Best of all, you can post the text in reason if "it doesn't work"!

Upvotes: 0

user5678677
user5678677

Reputation: 11

try this one:

fwrite(t, [255, 0, 4, 0, 255])

Upvotes: 1

Related Questions