简体   繁体   English

重新分配输入/输出流?

[英]Reassign input/output stream?

I am trying to connect to a terminal emulator using a library in android, this will connect to a serial device and should show me sent/received data. 我正在尝试使用android中的库连接到终端仿真器,这将连接到串行设备,并且应该向我显示发送/接收的数据。 To attach to a terminal session I need to provide an inputstream to setTermIn(InputStream) and an outputstream to setTermOut(OutputStream) . 附加到我需要提供终端会话inputstreamsetTermIn(InputStream)和一个outputstreamsetTermOut(OutputStream)

I initialize and attach some streams like so in onCreate() , these are just initial streams and are not attached to the data I want to be sending/receiving. 我在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);

I now want to assign the streams to data as I send and receive it, but I think I am doing it wrong. 我现在想在发送和接收数据时将流分配给数据,但是我认为我做错了。 In The sendData() method, which I call every time I press enter, I have: sendData()方法中,每次按Enter时都会调用该方法,它具有:

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

and in the onReceiveData() method, called every time data is received over serial: 并且在onReceiveData()方法中,每次通过串行接收数据时都会调用:

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

I am not seeing any data on my terminal screen, but I am sending and receiving it over serial successfully. 我没有在终端屏幕上看到任何数据,但是我已成功通过串行发送和接收数据。 So my question is, should I be setting the streams every single time I am sending and receiving data or just set them once. 所以我的问题是,我应该在每次发送和接收数据时都设置流,还是只设置一次。 Also do I need to attach them to the terminal session again mEmulatorView.attachSession(session) somewhere or should the new streams automatically be sent to the screen? 我是否还需要将它们再次附加到终端会话mEmulatorView.attachSession(session)某个地方,还是应该将新流自动发送到屏幕?

My theory is that my terminal is attached to the old streams and that is why I can't see data on the terminal screen. 我的理论是我的终端连接到旧的流,这就是为什么我无法在终端屏幕上看到数据的原因。 Would this be correct? 这是正确的吗?

I tried to set the new input/output streams in each method just once using a boolean and an if statement, but then I get warning messages in logcat 我尝试使用布尔值和if语句在每种方法中仅设置一次新的输入/输出流,但是随后我在logcat中收到警告消息

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

I've edited it to write and rad now based on answered but I notice that the library has it's own write method to feed data to the terminal, so I don't even know what the streams are for if that is that case, and I need this write to write to the emulator? 我已经根据答案将其编辑为write和rad,但是我注意到该库具有将数据馈送到终端的自己的write方法,因此,即使是这种情况,我什至都不知道流是干什么的,并且我需要此写操作才能写入模拟器吗?

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.

Objects in java are passed by reference, hence if you do Java中的对象是通过引用传递的,因此如果您这样做

bos = new ByteArrayOutputStream(data.length)

You're essentially throwing away the previous outputstream and creating a new one. 您实际上是在丢弃先前的输出流并创建一个新的输出流。

Try keeping reference to your input and output stream and write data into it, eg: 尝试保持对输入和输出流的引用,并将数据写入其中,例如:

bos.write(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM