我正在使用java类通过matlab tcp(以太网电缆)与机器人手臂进行通信。它运行良好我发送一些整数值并接收。问题是当我尝试在linux上工作时我无法连接。我不认为问题出在代码上,但我仍然分享我的代码和matlab输出。 (我用sudo权限打开matlab。)
host = '169.254.131.130';
port = 10300;
int32 roll
roll = 457;
int32 a;
int32 b;
int32 c;
int32 d;
%% Socket connection
javaaddpath('tcpip');
import java.net.Socket
import java.io.*
inputSocket = [];
while true
try
fprintf(1, 'Connecting to %s:%d\n', host, port);
% throws if unable to connect
inputSocket = Socket(host, port);
fprintf(1, 'Connected to server\n');
break;
catch
if ~isempty(inputSocket)
inputSocket.close;
end
pause(1);
end
end
%% read data from the socket
input_stream = inputSocket.getInputStream;
d_input_stream = DataInputStream(input_stream);
bytes_available = input_stream.available;
tic
for i = (1:100)
output_stream = inputSocket.getOutputStream;
d_output_stream = DataOutputStream(output_stream);
d_output_stream.writeInt(i);
d_output_stream.flush;
%pause(0.01);
while (bytes_available == 0)
bytes_available = input_stream.available;
end
fprintf(1, 'Reading %d bytes\n',bytes_available);
data_reader = DataReader(d_input_stream);
message = int32(data_reader.readBuffer(4));
if message(4) < 0
message(4) = message(4) + 256;
end
result = message(1) * 2^24 + message(2) * 2^16 + message(3) * 2^8 + message(4)
bytes_available = 0;
end
toc
MATLAB输出
Connecting to 169.254.131.130:10300
Connecting to 169.254.131.130:10300
Connecting to 169.254.131.130:10300
Connecting to 169.254.131.130:10300
Connecting to 169.254.131.130:10300
Connecting to 169.254.131.130:10300
Connecting to 169.254.131.130:10300
Connecting to 169.254.131.130:10300
Connecting to 169.254.131.130:10300
Connecting to 169.254.131.130:10300
Connecting to 169.254.131.130:10300
一次又一次