应用程序使用Android平板电脑作为通过USB与PIC设备进行通信的手持终端。设备返回一个ASCII流,我在textView中显示。
进入Android的文本流以ASCII 0x0A(LF)终止。这没有问题,它会根据需要为每个字符串生成一个新行。但是,我还希望能够将传入的流分解为行以进行格式化/显示。我为此注入了一个CR(0x0D)到ASCII流中。
我尝试了多种方法让textView正确显示流 - 用' \ n'," \ n \ r"替换0x0D,nl =系统.getProperty(" line.separator&#34)。但是,在每种情况下,插入都不起作用。我用CatLog来检查字符串发生了什么,看起来没问题 - 我错过了什么?
代码是:
public void onAnimationUpdate(ValueAnimator timeAnim)
{
if ((mConnection != null) && mConnection.claimInterface(mInterface, true))
{
// Check serial port for incoming FORTH response on ep1i
inForthReq = new UsbRequest();
inForthReq.initialize(mConnection, ep1i);
inForthReq.queue(InBuffer, 8);
mConnection.requestWait();
InBytes = InBuffer.array();
// No data is indicated by a NAK - ASCII 0x15 = dec 21
if (InBytes[0] != 21)
{
lastChar = false;
int length = 8;
for (int i = 0; i < 8; i++)
{
Log.d(TAG, "FORTH response byte " + i + " = " + InBytes[i]);
// Check for end of string - LF = ASCII 0x0A = dec 10
if (InBytes[i] == 10)
{
lastChar = true;
length = i;
Log.d(TAG, "FORTH string terminated - length " + i);
}
// Check for CR in mid-stream
if (InBytes[i] == 13)
{
InBytes[i] = 10;
Log.d(TAG,"CR replaced with " + InBytes[i]);
}
}
rxString = new String(InBytes, 0, length);
INString = INString.concat(rxString);
Log.d(TAG, "+++ Returned FORTH string = -" + INString + "- +++");
if (lastChar)
{
replyString = INString;
ForthPanelFragment.traffic = ForthPanelFragment.traffic.append(Html.fromHtml("<font color=\"blue\">"
+ replyString + "</font>"));
ForthPanelFragment.traffic = ForthPanelFragment.traffic.append('\n');
if (ForthPanelFragment.traffic_panel != null)
((TextView) ForthPanelFragment.traffic_panel).setText(ForthPanelFragment.traffic);
INString = emptyString;
}
}
解释我在做什么 - USB调用返回一个8字节的数组。在转换为8个字符的字符串之前,会扫描特殊情况(如注释所示)。连续字符串将被连接,直到检测到LF,此时结果将添加到片段中的视图窗格中。
作为流量的一个例子 -
12-19 09:30:46.250 D / HMI / 4th terminal(6164):启动动画师
12-19 09:31:11.610 D / HMI / 4th terminal(6164):收到A 1 .LINE
12-19 09:31:11.610 D / HMI / 4th terminal(6164):来自PanelFragment。
12-19 09:31:11.610 D / HMI / 4th端子(6164):长度= 10 1 2-19 09:31:11.610 D / HMI /第4终端(6164):最后一个数据包= 2
12-19 09:31:11.610 D / HMI / 4th terminal(6164):数据包数量= 2
12-19 09:31:11.660 D / HMI / 4th terminal(6164):发送FORTH命令字符串A 1 .LIN
12-19 09:31:11.660 D / HMI / 4th terminal(6164):长度= 8:最后一个字符= 78
12-19 09:31:11.660 D / HMI / 4th terminal(6164):FORTH命令字符串OK
12-19 09:31:11.730 D / HMI / 4th terminal(6164):发送FORTH命令字符串E
12-19 09:31:11.730 D / HMI / 4th terminal(6164):Length = 2:last char = 10
12-19 09:31:11.760 D / HMI / 4th terminal(6164):FORTH命令字符串OK
12-19 09:31:11.820 D / HMI / 4th端子(6164):FORTH响应字节0 = 73
12-19 09:31:11.820 D / HMI / 4th terminal(6164):FORTH响应字节1 = 110
12-19 09:31:11.820 D / HMI / 4th terminal(6164):FORTH响应字节2 = 32
12-19 09:31:11.820 D / HMI /第4终端(6164):FORTH响应字节3 = 112
12-19 09:31:11.820 D / HMI / 4th terminal(6164):FORTH响应字节4 = 114
12-19 09:31:11.820 D / HMI / 4th端子(6164):FORTH响应字节5 = 111
12-19 09:31:11.820 D / HMI / 4th terminal(6164):FORTH响应字节6 = 116
12-19 09:31:11.820 D / HMI /第4终端(6164):FORTH响应字节7 = 101
12-19 09:31:11.820 D / HMI / 4th terminal(6164):+++返回FORTH字符串= -In prote- +++
12-19 09:31:11.920 D / HMI / 4th端子(6164):FORTH响应字节0 = 99
12-19 09:31:11.920 D / HMI / 4th端子(6164):FORTH响应字节1 = 116
12-19 09:31:11.920 D / HMI /第4终端(6164):FORTH响应字节2 = 101
12-19 09:31:11.920 D / HMI / 4th terminal(6164):FORTH响应字节3 = 100
12-19 09:31:11.920 D / HMI /第4终端(6164):FORTH响应字节4 = 32
12-19 09:31:11.920 D / HMI / 4th terminal(6164):FORTH响应字节5 = 100 12-19 09:31:11.920 D / HMI /第4终端(6164):FORTH响应字节6 = 105
12-19 09:31:11.920 D / HMI / 4th端子(6164):FORTH响应字节7 = 99
12-19 09:31:11.920 D / HMI / 4th terminal(6164):+++返回FORTH字符串= -In protected dic- +++
12-19 09:31:11.980 D / HMI / 4th terminal(6164):FORTH响应字节0 = 116
12-19 09:31:11.980 D / HMI / 4th terminal(6164):FORTH响应字节1 = 105
12-19 09:31:11.980 D / HMI / 4th端子(6164):FORTH响应字节2 = 111
12-19 09:31:11.980 D / HMI / 4th terminal(6164):FORTH响应字节3 = 110
12-19 09:31:11.980 D / HMI /第4终端(6164):FORTH响应字节4 = 97
12-19 09:31:11.980 D / HMI / 4th端子(6164):FORTH响应字节5 = 114
12-19 09:31:11.980 D / HMI /第4终端(6164):FORTH响应字节6 = 121
12-19 09:31:11.980 D / HMI / 4th端子(6164):FORTH响应字节7 = 33
12-19 09:31:11.980 D / HMI / 4th terminal(6164):+++返回FORTH string = -In protected dictionary! - +++
12-19 09:31:12.050 D / HMI / 4th terminal(6164):FORTH响应字节0 = 13
12-19 09:31:12.050 D / HMI / 4th端子(6164):LF替换为10
12-19 09:31:12.050 D / HMI / 4th terminal(6164):FORTH响应字节1 = 32
12-19 09:31:12.050 D / HMI / 4th terminal(6164):FORTH响应字节2 = 111
12-19 09:31:12.050 D / HMI / 4th terminal(6164):FORTH响应字节3 = 107
12-19 09:31:12.050 D / HMI / 4th terminal(6164):FORTH响应字节4 = 33
12-19 09:31:12.050 D / HMI / 4th terminal(6164):FORTH响应字节5 = 32
12-19 09:31:12.050 D / HMI / 4th terminal(6164):FORTH响应字节6 = 10
12-19 09:31:12.050 D / HMI / 4th terminal(6164):FORTH字符串终止 - 长度为6
12-19 09:31:12.050 D / HMI / 4th terminal(6164):FORTH响应字节7 = 0
12-19 09:31:12.050 D / HMI / 4th terminal(6164):+++返回FORTH字符串= - 受保护字典!
12-19 09:31:12.050 D / HMI / 4th terminal(6164):好的! - +++
12-19 09:31:18.390 D / HMI / 4th terminal(6164):FORTH响应字节0 = 0
12-19 09:31:18.390 D / HMI / 4th terminal(6164):FORTH响应字节1 = 0
12-19 09:31:18.390 D / HMI / 4th terminal(6164):FORTH响应字节2 = 0
12-19 09:31:18.390 D / HMI / 4th terminal(6164):FORTH响应字节3 = 0
12-19 09:31:18.390 D / HMI / 4th terminal(6164):FORTH响应字节4 = 0
12-19 09:31:18.390 D / HMI / 4th terminal(6164):FORTH响应字节5 = 0
12-19 09:31:18.390 D / HMI / 4th terminal(6164):FORTH响应字节6 = 0
12-19 09:31:18.390 D / HMI / 4th terminal(6164):FORTH响应字节7 = 0
12-19 09:31:18.390 D / HMI / 4th terminal(6164):+++返回FORTH字符串=-��������-+++
12-19 09:31:18.760 D / HMI / 4th terminal(6164):on onStop(活动)
在屏幕上显示为 -
在受保护的词典中! OK!
而我想要的是 -
在受保护的词典中!
OK!
有趣的是,CatLog列表确实在确定之前给了一个新行!