Telnet与MATLAB联系

时间:2014-06-05 16:21:35

标签: matlab telnet

我正在尝试使用MATLAB访问telnet数据流。

我发现的唯一可以做到这一点的工具箱是TCPIP工具箱。

可悲的是,我无法在两台机器之间建立讨论

使用的代码:

t = tcpip(IP, PORT);
fopen(t);
fwrite(t, MyTelnetCommand);
response = fread(t, 1)

我希望回复是MyTelnetCommand回复,但我总是得到:

Warning: Unsuccessful read:  The specified amount of data was
not returned within the Timeout period. 

response =

    Empty matrix: 1-by-0

有没有办法处理我想要实现的目标?

1 个答案:

答案 0 :(得分:0)

我最终在Mathworks的支持下找到了解决方案。

首先,最好将命令转换为double:

MyTelnetCommandConverted = double(MyTelnetCommand)';

其次,根据文档(fwritefprintf),请不要将fwrite()用于文本命令,而是fprintf()

最后,人们应该等待获得服务器的答案:

while(tCmd.BytesAvailable<=0)
    drawnow
end
response = fread(tCmd, tCmd.BytesAvailable);
char(response')

while(tCmd.BytesAvailable<=0)
    drawnow
end
response = fscanf(tCmd, '%s', tCmd.BytesAvailable)

所以综合代码是:

tCmd = tcpip(ip, portCmd);
set(tCmd,'InputBufferSize',buffSize); % buffSize = 1000000
set(tCmd,'OutputBufferSize',buffSize);

fopen(tCmd);

MyTelnetCommand = '(sendCommand cmd_Start 0 -1 1)';
MyTelnetCommandConverted = double(MyTelnetCommand)';
fprintf(tCmd, MyTelnetCommandConverted);

while(tCmd.BytesAvailable<=0)
    drawnow
end
response = fscanf(tCmd, ,'%s', tCmd.BytesAvailable)

fclose(tCmd);