/etc/inittab 这个文件已经修改过了,用串口调试软件已经调试有数据返回的,如下图
哪位仁兄给点帮助成分感谢,
代码用的是论坛里的代码,代码权限已加,如下public class MessageActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "MessageActivity";
private Socket socket ;
static PrintWriter mPrintWriter ;
static BufferedReader br ;
private Thread mThread=null;
private String msg="";
public TextView textview2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button2 = (Button) findViewById(R.id.button2);//设置按钮,按下后给服务器发送数据
textview2=(TextView)findViewById(R.id.textview2);//显示从服务器传来的数据
try
{
socket=new Socket("10.10.100.254",8899);
br =new BufferedReader(new InputStreamReader(socket.getInputStream())); //获得输出流
mPrintWriter=new PrintWriter(socket.getOutputStream(),true); //获得输入流
}
catch(Exception e)
{
Log.e(TAG,e.toString());
}
button2.setOnTouchListener(new Button.OnTouchListener() //给button2 设置监听
{
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
mPrintWriter.print("f");
mPrintWriter.print("\r");
mPrintWriter.print("\n");//前进
mPrintWriter.flush();
break;
case MotionEvent.ACTION_UP:
mPrintWriter.print("0");
mPrintWriter.print("\r");
mPrintWriter.print("\n");
mPrintWriter.flush();
}
return false;
}
});
mThread= new Thread(mRunnable); //创建一个新的线程
mThread.start(); //开启新的线程
}
private Runnable mRunnable = new Runnable() //通过Runnable接口创建线程,来处理从服务器传来的数据
{
public void run()
{
while (true)
{
try
{
if((msg=br.readLine())!=null)
{
mHandler.sendMessage(mHandler.obtainMessage());
}
}
catch(Exception e)
{
Log.e(TAG,e.toString());
}
}
}
};
Handler mHandler=new Handler() //处理传来的数据
{
public void handleMessage( Message Msg)
{
super.handleMessage(Msg);
try
{
textview2.setText(msg);
}
catch(Exception e)
{
Log.e(TAG,e.toString());
}
}
};
}
|