我正在使用终端仿真器库来创建终端,然后使用它将通过串行输入的数据发送到串行设备。图书馆可以在这里看到。
当我将数据输入终端时,正在发送/接收一系列奇怪的字符。我认为 unicode 替换字符是通过串行发送的,串行设备不知道它是什么并返回 ~0。
当我写“测试”时,终端中出现的屏幕截图:
以及显示发送的字符串和接收的数据的日志。
我创建了一个 EmulatorView,它是终端视图。它在这里提到了钻石。
private void sendText(CharSequence text) {
int n = text.length();
char c;
try {
for(int i = 0; i < n; i++) {
c = text.charAt(i);
if (Character.isHighSurrogate(c)) {
int codePoint;
if (++i < n) {
codePoint = Character.toCodePoint(c, text.charAt(i));
} else {
// Unicode Replacement Glyph, aka white question mark in black diamond.
codePoint = '\ufffd';
}
mapAndSend(codePoint);
} else {
mapAndSend(c);
}
}
} catch (IOException e) {
Log.e(TAG, "error writing ", e);
}
}
有没有什么办法解决这一问题?任何人都可以在库类中看到为什么会这样吗?,如果我愿意,我如何在 java 中引用 � 来解析它?我不能说如果 (!str.contains("�") 我接受它。
当我在终端中输入时,它会运行:
public void write(byte[] bytes, int offset, int count) {
String str;
try {
str = new String(bytes, "UTF-8");
Log.d(TAG, "data received in write: " +str );
GraphicsTerminalActivity.sendOverSerial(str.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception" );
e.printStackTrace();
}
// appendToEmulator(bytes, 0, bytes.length);
return;
}
这就是我所说的发送数据。sendData(Byte[] data) 是一个库方法。
public static void sendOverSerial(byte[] data) {
String str;
try {
str = new String(data,"UTF-8");
if(mSelectedAdapter !=null && data !=null){
Log.d(TAG, "send over serial string==== " + str);
mSelectedAdapter.sendData(str.getBytes("UTF-8"));
}
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception");
e.printStackTrace();
}
}
发送数据后,将在此处收到回复:
public void onDataReceived(int id, byte[] data) {
try {
dataReceived = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception");
e.printStackTrace();
}
try {
dataReceivedByte = dataReceived.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception");
e.printStackTrace();
}
statusBool = true;
Log.d(TAG, "in data received " + dataReceived);
((MyBAIsWrapper) bis).renew(data);
runOnUiThread(new Runnable(){
@Override
public void run() {
mSession.appendToEmulator(dataReceivedByte, 0, dataReceivedByte.length);
}});
viewHandler.post(updateView);
}
写入字符的库类的相关部分:
类的相关部分:
private void sendText(CharSequence text) {
int n = text.length();
char c;
try {
for(int i = 0; i < n; i++) {
c = text.charAt(i);
if (Character.isHighSurrogate(c)) {
int codePoint;
if (++i < n) {
codePoint = Character.toCodePoint(c, text.charAt(i));
} else {
// Unicode Replacement Glyph, aka white question mark in black diamond.
codePoint = '\ufffd';
}
mapAndSend(codePoint);
} else {
mapAndSend(c);
}
}
} catch (IOException e) {
Log.e(TAG, "error writing ", e);
}
}
private void mapAndSend(int c) throws IOException {
int result = mKeyListener.mapControlChar(c);
if (result < TermKeyListener.KEYCODE_OFFSET) {
mTermSession.write(result);
} else {
mKeyListener.handleKeyCode(result - TermKeyListener.KEYCODE_OFFSET, getKeypadApplicationMode());
}
clearSpecialKeyStatus();
}