可怜的程序员同胞。如果你们就是出现 可以 写数据发出去,然而安卓端不能读数据的话。有一种可能,那就是
如果是用PC模拟串口发数据,请确定你有安装正确的串口驱动。
如果是用串口设备发数据,请仔细检查下你的串口设备程序有没有正确的把数据发出来
——————————————————————————————————————
/*
* Copyright 2009 Cedric Priscal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android_serialport_api.sample;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Arrays;
import android_serialport_api.SerialPort;
public class MainMenuextends Activity {
private final StringTAG ="打印数据";
private OutputStreammOutputStream;
private InputStreaminputStream;
private ReadThreadmReadThread;
private class ReadThreadextends Thread {
@Override
public void run() {
super.run();
while (!isInterrupted()) {
int size;
try {
byte[] buffer =new byte[64];
if (inputStream ==null)return;
size =inputStream.read(buffer);
if (size >0) {
String result =new String(buffer, 0, size);
Log.e(TAG, "result=" + result);
}
}catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
SerialPort serialPort =new SerialPort(new File("/dev/ttyS1"), 9600, 0);//your serial port dev
mOutputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
/*开启线程读取数据*/
/* Create a receiving thread */
mReadThread =new ReadThread();
mReadThread.start();
}catch (IOException e) {
Toast.makeText(MainMenu.this, "串口设备获取失败", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
final Button buttonWrite = (Button) findViewById(R.id.ButtonW);
buttonWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
byte[] mBuffer =new byte[64];
Arrays.fill(mBuffer, (byte)0x55);
try {
mOutputStream.write(mBuffer);
}catch (IOException e) {
e.printStackTrace();
}
}
});
final Button buttonAbout = (Button) findViewById(R.id.ButtonAbout);
buttonAbout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder =new AlertDialog.Builder(MainMenu.this);
builder.setTitle("About");
builder.setMessage(R.string.about_msg);
builder.show();
}
});
final Button buttonQuit = (Button) findViewById(R.id.ButtonQuit);
buttonQuit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MainMenu.this.finish();
}
});
}
}