gpt4 book ai didi

java - 使用 Telnet 正确读取行

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:32 24 4
gpt4 key购买 nike

我有一个 Android 应用程序,它应该接收和阅读 NMEA 语句

e.g. $GPGLL,3751.65,S,14507.36,E*77

使用 Telnet 协议(protocol)从远程 TCP 服务器。我正在使用 org.apache.commons.net.telnet.TelnetClient 库。

什么有效:

  • 连接到服务器

  • 正确阅读一些句子

问题:有一半以上的句子缺失。我猜这是一个时间问题,也许这也与每次迭代都重新启动的连接有关。

这是我的 MainActivity.java:

package com.example.clienttel;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import org.apache.commons.net.telnet.TelnetClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Go to ClientThread, where NMEA-sentences will be received
int i;
for (i = 0; i < 10000; i++) // 10000 for testing
new Thread(new ClientThread()).start();
}

class ClientThread implements Runnable {
//@Override
public void run() {
TelnetClient telnet = new TelnetClient();

// Variables
String ADDRESS = "194.66.82.11";
int PORT = 51000;
String NMEA = null;
final String TAG = "TestApp";

// Connect To Server
try {
telnet.connect(ADDRESS, PORT);
} catch (IOException e) {
e.printStackTrace();
}

// Process NMEA-sentences
InputStream inStream = telnet.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(inStream));

try {
NMEA = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}

// Ignore "...busy" sentences
if (NMEA != null) {
if(!(NMEA.equals("*** Serial port is busy ***"))) {
Log.i(TAG, NMEA);
}
}

// Disconnect From Server
try {
telnet.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

如有任何帮助,我们将不胜感激!

编辑:

我的代码现在看起来像这样:

package com.example.clienttel;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;

import org.apache.commons.net.telnet.TelnetClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends ActionBarActivity {
// Variables
public String ADDRESS = "194.66.82.11";
public int PORT = 50100;
public String NMEA = null;
public final String TAG = "TestApp";
public boolean first = true;


// Handler in mainthread
Handler handler = new Handler() {
public void handleMessage(Message msg) {
String dataString = "";
Bundle bundle = msg.getData();

Log.d("handleMessage", bundle.toString());
if (bundle.containsKey("outgoingString")) {
dataString = bundle.getString("outgoingString");
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ClientThread ct;
ct = new ClientThread();
ct.mhandler=handler;

int i;
for (i = 0; i < 10000; i++) { // 10000 for testing
// Go to ClientThread, where NMEA-sentences will be received
//new Thread(new ClientThread()).start();
ct.start();
}
}

class ClientThread implements Runnable {
public Handler mhandler = null;
@Override
public void run() {
TelnetClient telnet = new TelnetClient();

if (first) {
// Connect To Server in 1st Iteration
try {
telnet.connect(ADDRESS, PORT);
} catch (IOException e) {
e.printStackTrace();
}
first = false;
}

// Process NMEA-sentences
InputStream inStream = telnet.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(inStream));

try {
NMEA = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}

// Handler in ClientThread to send back
Bundle b = new Bundle();

b.putString("outgoingString", NMEA);

Message m = mhandler.obtainMessage();
m.setData(b);
mhandler.sendMessage(m);
}
}
}

但我不确定处理程序的位置

最佳答案

正如@EJP 所说,打开和关闭每一行的连接都会终止应用程序...

更好的方法是让线程处理通信并等待下一行,将数据作为 bundle 消息传递回主线程。

那里有很多示例,但本质上您是在主线程中创建一个处理程序:

Handler handler = new Handler()
{
public void handleMessage(Message msg)
{

String dataString = "";

Bundle bundle = msg.getData();

Log.d("handleMessage", bundle.toString());
if (bundle.containsKey("outgoingString"))
{
dataString = bundle.getString("outgoingString");

}

// you can handle other message types here....
}

并将它(处理程序)传递给您的 ClientThread(此处作为 mhandler),您可以在其中发回消息:

Bundle b = new Bundle();

b.putString("outgoingString", outgoingText);
Message m = mhandler.obtainMessage();
m.setData(b);
mhandler.sendMessage(m);

关于java - 使用 Telnet 正确读取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27724721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com