我有一个Socket连接到COM-Server ++从W & T连接到互联网,并通过串行到USB连接到我的PC。从套接字的InputStream读取
的COM服务器为TCP站通信的设置为:
ACTIV。数据包选项:已禁用
无效。超时:00030
连接。超时:00300
断开字符:000
客户: “C” +地址:禁用
响应模式:禁用
在我的应用程序阅读这个服务器这样的输入数据:
boolean running = true;
log.info("{0}: Starting to listen for input data", name);
while (running)
{
try
{
int charsRead = inputStream.read(buffer);
if (charsRead < 0)
{
running = false;
}
else
{
byte[] received = Arrays.copyOf(buffer, charsRead);
/** TODO: Call interface of protocol here */
log.info("{0}: data received: {1}", connection.getName(), new String(received));
}
}
catch (IOException ie)
{
setStatus(ConnectionStatus.FAILURE);
close();
/** TODO: Exception handling */
running = false;
}
}
如果我派:test<CR><LF>
从设备日志输出我得到的是:
(terminal1) terminal1: data received: t
(terminal1) terminal1: data received: e
(terminal1) terminal1: data received: st
(terminal1) terminal1: data received:
(terminal1) terminal1: data received:
所需的输出不过是:
(terminal1) terminal1: data received: test
我的错误在哪里?或者我假设InputStream的读取方法的错误工作流程?
[DOC](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html) 公共抽象INT读() 抛出IOException异常 读取的下一个数据字节从输入流 public int read(byte [] b) throws IOException 从输入流中读取一定数量的字节 – mihatel
噢好吧,所以他不会读到最后。在技术上等待完成时间,然后注销缓冲区将会返回预期的结果,我正确吗? – Nico
只需将读取的内容追加到数组中直到它结束,全部为 – mihatel