Application is using an Android tablet as a hand-held terminal communicating via USB with a PIC device. The device is returning an ASCII stream which I am displaying in a textView.
The text stream into Android is terminated with ASCII 0x0A (LF). There is no problem with this and it generates a new line for each string as required. However, I also want to be able to break an incoming stream into lines for formatting/display purposes. I am injecting a CR (0x0D) into the ASCII stream for this.
I have tried a number of ways to get the textView to display the stream correctly - replacing the 0x0D with '\n', "\n\r", and nl = System.getProperty("line.separator"). However, in every case the insertion does nothing. I have used CatLog to check what is happening to the string and it appears to be OK - what am I missing?
The code is:
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;
}
}
To explain what I am doing - the USB call returns an 8-byte array. This is scanned for special cases (as indicated by the comments) before being converted to an 8-character string. Successive strings are concatenated until the LF is detected, when the result is added to the view pane in a fragment.
As an example of the traffic -
12-19 09:30:46.250 D/HMI/4th terminal(6164): Starting animator
12-19 09:31:11.610 D/HMI/4th terminal(6164): Received A 1 .LINE
12-19 09:31:11.610 D/HMI/4th terminal(6164): from PanelFragment.
12-19 09:31:11.610 D/HMI/4th terminal(6164): Length = 10 1 2-19 09:31:11.610 D/HMI/4th terminal(6164): Last Packet = 2
12-19 09:31:11.610 D/HMI/4th terminal(6164): No of Packets = 2
12-19 09:31:11.660 D/HMI/4th terminal(6164): Sending FORTH command string A 1 .LIN
12-19 09:31:11.660 D/HMI/4th terminal(6164): Length = 8: last char = 78
12-19 09:31:11.660 D/HMI/4th terminal(6164): FORTH command string OK
12-19 09:31:11.730 D/HMI/4th terminal(6164): Sending FORTH command string 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 command string OK
12-19 09:31:11.820 D/HMI/4th terminal(6164): FORTH response byte 0 = 73
12-19 09:31:11.820 D/HMI/4th terminal(6164): FORTH response byte 1 = 110
12-19 09:31:11.820 D/HMI/4th terminal(6164): FORTH response byte 2 = 32
12-19 09:31:11.820 D/HMI/4th terminal(6164): FORTH response byte 3 = 112
12-19 09:31:11.820 D/HMI/4th terminal(6164): FORTH response byte 4 = 114
12-19 09:31:11.820 D/HMI/4th terminal(6164): FORTH response byte 5 = 111
12-19 09:31:11.820 D/HMI/4th terminal(6164): FORTH response byte 6 = 116
12-19 09:31:11.820 D/HMI/4th terminal(6164): FORTH response byte 7 = 101
12-19 09:31:11.820 D/HMI/4th terminal(6164): +++ Returned FORTH string = -In prote- +++
12-19 09:31:11.920 D/HMI/4th terminal(6164): FORTH response byte 0 = 99
12-19 09:31:11.920 D/HMI/4th terminal(6164): FORTH response byte 1 = 116
12-19 09:31:11.920 D/HMI/4th terminal(6164): FORTH response byte 2 = 101
12-19 09:31:11.920 D/HMI/4th terminal(6164): FORTH response byte 3 = 100
12-19 09:31:11.920 D/HMI/4th terminal(6164): FORTH response byte 4 = 32
12-19 09:31:11.920 D/HMI/4th terminal(6164): FORTH response byte 5 = 100 12-19 09:31:11.920 D/HMI/4th terminal(6164): FORTH response byte 6 = 105
12-19 09:31:11.920 D/HMI/4th terminal(6164): FORTH response byte 7 = 99
12-19 09:31:11.920 D/HMI/4th terminal(6164): +++ Returned FORTH string = -In protected dic- +++
12-19 09:31:11.980 D/HMI/4th terminal(6164): FORTH response byte 0 = 116
12-19 09:31:11.980 D/HMI/4th terminal(6164): FORTH response byte 1 = 105
12-19 09:31:11.980 D/HMI/4th terminal(6164): FORTH response byte 2 = 111
12-19 09:31:11.980 D/HMI/4th terminal(6164): FORTH response byte 3 = 110
12-19 09:31:11.980 D/HMI/4th terminal(6164): FORTH response byte 4 = 97
12-19 09:31:11.980 D/HMI/4th terminal(6164): FORTH response byte 5 = 114
12-19 09:31:11.980 D/HMI/4th terminal(6164): FORTH response byte 6 = 121
12-19 09:31:11.980 D/HMI/4th terminal(6164): FORTH response byte 7 = 33
12-19 09:31:11.980 D/HMI/4th terminal(6164): +++ Returned FORTH string = -In protected dictionary!- +++
12-19 09:31:12.050 D/HMI/4th terminal(6164): FORTH response byte 0 = 13
12-19 09:31:12.050 D/HMI/4th terminal(6164): LF replaced with 10
12-19 09:31:12.050 D/HMI/4th terminal(6164): FORTH response byte 1 = 32
12-19 09:31:12.050 D/HMI/4th terminal(6164): FORTH response byte 2 = 111
12-19 09:31:12.050 D/HMI/4th terminal(6164): FORTH response byte 3 = 107
12-19 09:31:12.050 D/HMI/4th terminal(6164): FORTH response byte 4 = 33
12-19 09:31:12.050 D/HMI/4th terminal(6164): FORTH response byte 5 = 32
12-19 09:31:12.050 D/HMI/4th terminal(6164): FORTH response byte 6 = 10
12-19 09:31:12.050 D/HMI/4th terminal(6164): FORTH string terminated - length 6
12-19 09:31:12.050 D/HMI/4th terminal(6164): FORTH response byte 7 = 0
12-19 09:31:12.050 D/HMI/4th terminal(6164): +++ Returned FORTH string = -In protected dictionary!
12-19 09:31:12.050 D/HMI/4th terminal(6164): ok! - +++
12-19 09:31:18.390 D/HMI/4th terminal(6164): FORTH response byte 0 = 0
12-19 09:31:18.390 D/HMI/4th terminal(6164): FORTH response byte 1 = 0
12-19 09:31:18.390 D/HMI/4th terminal(6164): FORTH response byte 2 = 0
12-19 09:31:18.390 D/HMI/4th terminal(6164): FORTH response byte 3 = 0
12-19 09:31:18.390 D/HMI/4th terminal(6164): FORTH response byte 4 = 0
12-19 09:31:18.390 D/HMI/4th terminal(6164): FORTH response byte 5 = 0
12-19 09:31:18.390 D/HMI/4th terminal(6164): FORTH response byte 6 = 0
12-19 09:31:18.390 D/HMI/4th terminal(6164): FORTH response byte 7 = 0
12-19 09:31:18.390 D/HMI/4th terminal(6164): +++ Returned FORTH string = -��������- +++
12-19 09:31:18.760 D/HMI/4th terminal(6164): In onStop(activity)
This displays on the screen as -
In protected dictionary! ok!
whereas what I want is -
In protected dictionary!
ok!
Interestingly, the CatLog listing does give a new line before the ok!.