2012-12-14 103 views
1

我想连接到一个终端模拟器使用Android的库,这将连接到串行设备,并应显示我发送/接收的数据。要附加到终端会话,我需要提供inputstreamsetTermIn(InputStream)outputstreamsetTermOut(OutputStream)重新分配输入/输出流?

我初始化并附加了一些数据流,如onCreate(),这些只是初始流,并没有附加到我想要发送/接收的数据上。

private OutputStream bos; 
private InputStream bis; 

... 

byte[] a = new byte[4096]; 
bis = new ByteArrayInputStream(a); 
bos = new ByteArrayOutputStream(); 
session.setTermIn(bis); 
session.setTermOut(bos); 
/* Attach the TermSession to the EmulatorView. */ 
mEmulatorView.attachSession(session); 

我现在想分配数据流作为我发送和接收它,但我认为我做错了。在sendData()方法,我称之为我每次按enter键的时候,我有:

public void sendData(byte[] data) 
{ 
     bos = new ByteArrayOutputStream(data.length);   
} 

,并在onReceiveData()方法,称为每次接收数据通过串行时间:

public void onDataReceived(int id, byte[] data) 
{ 
     bis = new ByteArrayInputStream(data);   
} 

我没有看到我的终端屏幕上的任何数据,但我正在通过串行成功发送和接收它。所以我的问题是,我应该在每次发送和接收数据时设置流,还是只设置一次。还需要将它们再次附加到终端会话mEmulatorView.attachSession(session)某处或者应该将新流自动发送到屏幕?

我的理论是,我的终端连接到旧的流,这就是为什么我不能在终端屏幕上看到数据。这是正确的吗?

我试图仅有一次使用if语句的布尔和每种方法的新的输入/输出流,但后来我得到logcat的警告消息

RuntimeException 'sending message to a Handler on a dead thread'

我它编辑成写和rad现在基于回答,但我注意到,该库有它自己的写入方法将数据提供给终端,所以我甚至不知道什么是数据流,如果是这种情况,我需要这写写到模拟器?在Java中

public void write(byte[] data, 
       int offset, 
       int count) 
Write data to the terminal output. The written data will be consumed by the emulation  client as input. 
write itself runs on the main thread. The default implementation writes the data into a  circular buffer and signals the writer thread to copy it from there to the OutputStream. 

Subclasses may override this method to modify the output before writing it to the stream, but implementations in derived classes should call through to this method to do the actual writing. 

Parameters: 
data - An array of bytes to write to the terminal. 
offset - The offset into the array at which the data starts. 
count - The number of bytes to be written. 
+0

您想在不同的流或什么的每一块数据的发送?如果是,那么这是不好的主意,你应该使用已创建的流的bos.Read()和bis.Write()。 – Mateusz

+0

我得到你的意思谢谢,虽然它是bos.write等,你混合起来。 :)我现在试过了,屏幕上仍然没有显示任何内容。另外当我在开始时手动将输入流设置为4096字节时,这不是问题,是不是会填满?或者它是否重要,因为它是一个流,这个初始数字根本无关紧要? – Paul

回答

1

对象按引用传递,因此,如果你这样做

bos = new ByteArrayOutputStream(data.length) 

你基本上丢掉以前的OutputStream并创建一个新的。

建议保持引用您的输入和输出流和写入数据到它,比如:

bos.write(data); 
+0

试过了,现在对于机器人写和阅读,仍然没有改变,但我认为我现在更多的是现在无论如何,谢谢。仍然很烦人,但没有显示,但:(当我将inputstream设置为4096字节时,这个bos.write(data)会覆盖那个固定的数字吗?还是这样做很重要,因为它是一个流?而你的4096字节是不是真的? – Paul

+0

@Mateusz我编辑了操作,注意到库有它自己的写入来写入终端的输入。所以我很困惑该怎么做。我将流传递给正常的输入/输出流写入方法,终端上不显示任何内容。或者我使用库的写入方法,我不知道如何将bos/bis连接到这些库。 bos.session.write(数据);不工作或session.bos.write(数据),这只是错误的语法? – Paul