我将Arduino Nano设置为TCP服务器。然后我写了一个Android应用程序打开套接字并发送数据,但它无法从服务器接收答案。 TCP服务器正在从Android应用程序接收消息,但有时会收到更多空消息:
Android日志:
05-27 04:08:04.673 7571-7571/com.livingroom.stefan.livinglight D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
05-27 04:08:04.768 7571-7571/com.livingroom.stefan.livinglight D/LivingLight: onBtnClick(): gathering data from Buttons
05-27 04:08:04.768 7571-7571/com.livingroom.stefan.livinglight D/LivingLight: onBtnClick(): data gathered: 00000000
05-27 04:08:04.843 7571-9973/com.livingroom.stefan.livinglight D/LivingLight: startCom(): open socket(192.168.188.25,23598)
05-27 04:08:04.843 7571-9973/com.livingroom.stefan.livinglight D/LivingLight: startCom(): sending data to device
05-27 04:08:04.848 7571-9973/com.livingroom.stefan.livinglight D/LivingLight: startCom(): data sent to device (00000000)
05-27 04:08:04.848 7571-9973/com.livingroom.stefan.livinglight D/LivingLight: startCom(): socket closed
从Arduino登录:
ESP8266: init()
ESP8266: connected to wifi
ESP8266: TCP Server started
AT+CIFSR
+CIFSR:STAIP,"192.168.188.25"
+CIFSR:STAMAC,"18:fe:34:a5:22:bb"
OK
got message:,9:00000000
0,CLOSED
set outputs: 00000000
got message:
got message:
got message:
got message:
got message:
要检查我的服务器或Android应用中是否存在此问题,我尝试从另外两台设备向我的Arduino发送数据。我的Raspberry Pi上的C代码和一个名为" SocketTest3"工作得很好。两者都正确地发送数据并从TCP服务器接收确认。这就是为什么我猜这个问题出现在我的Android代码中。
奇怪的是,只有来自Android设备的消息后面有0,CLOSED
。
我在这里找到了一些建议,比如刷新PrintWriter
或等待BufferedReader.ready()
。但他们都没有找到解决方案。方法PrintWriter.checkError()
是错误的。
我的Arduino代码是:
void loop() {
int client = esp8266.getId();
if (client >= 0) {
String msg = esp8266.readString();
// receive data from serial buffer
esp8266.sendData(client, "#\r\n");
// acknowledge received data
debug("got message:");
debug(msg);
// eg.: ,9:01101001
debug("\n");
int start = msg.indexOf(':')+1;
// gets index of : char
if((msg.charAt(start)-48)>=0) {
// checks if buffer contains data
debug("set outputs: ");
for(int i=0; i<N_LED; i++) {
// for each LED (starts with PIN 2)
digitalWrite((i+2),(msg.charAt(start+i)-48));
debug(digitalRead(i+2));
EEPROM.write(i,digitalRead(i+2));
}
debug("\n");
}
esp8266.closeConnection(client);
}
} //end loop
字符串准备txData
:
public void onBtnClick() {
txData = "";
if(tbAllOff.isChecked()) {
Log.d(TAG, "onBtnClick, allOff");
txData = "00000000";
} else if(tbAllOn.isChecked()) {
Log.d(TAG, "onBtnclick, allOn");
txData = "11111111";
} else {
Log.d(TAG, "onBtnClick, gathering data from Buttons");
for (int i=0; i<tbList.length; i++) {
txData += (tbList[i].isChecked())?1:0;
}
Log.d(TAG, "onBtnClick, data gathered: " + txData);
}
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB)
new Client().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
new Client().execute();
txt1.setText(rxData);
}
我的Android应用程序的套接字部分是:
private class Client extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String[] params) {
try {
Log.d(TAG,"AsyncTask.doInBackground(): send data: "+ txData);
rxData = startCom();
} catch(IOException e) {
e.printStackTrace();
rxData = "IOException: " + e;
Log.e(TAG,rxData);
}
return null;
}
private String startCom() throws IOException {
ipAddress = "192.168.188.25";
portNumber = 23598;
Socket socket = new Socket(ipAddress, portNumber);
Log.d(TAG,"startCom(): open socket(" + ipAddress + "," + portNumber + ")");
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(),true);
Log.d(TAG,"startCom(): sending data to device");
output.println(txData);
output.close();
Log.d(TAG,"startCom(): data sent to device (" + txData + ")");
String in = "#";
if(input.ready()) {
in = input.readLine();
Log.d(TAG, "startCom(): data received: " + in);
}
socket.close();
Log.d(TAG,"startCom(): socket closed");
return in;
}
}