android读写串口

android读写串口(friendlyarm)

import com.friendlyarm.AndroidSDK.HardwareControler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

public class MapGuider_Activity extends Activity{

    private int serial_fd=0;
    private byte[] serial_RevBuf=new byte[100];
    private byte[] serial_SendBuf="fsjajd".getBytes();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapguider);
        serial_fd=HardwareControler.openSerialPort("/dev/s3c2410_serial1", 115200, 8, 1);
        if(serial_fd!=-1)
        {
            System.out.println("打开串口成功");
            HardwareControler.write(serial_fd, serial_SendBuf);
            lac_handler.post(RevicePosDataThread);        
            }
        else
        {
            System.out.println("指定的串口不存在或在其它进程中已被打开");    
        }
      }
    
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        HardwareControler.close(serial_fd);
        System.out.println("关闭串口");    
        lac_handler.removeCallbacks(RevicePosDataThread);
        super.onDestroy();
    }

    Handler lac_handler =new Handler(){

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
        }
        
    };
    Runnable RevicePosDataThread =new Runnable()
    {
        int err=0;
        String s;
        public void run() {
            // TODO Auto-generated method stub
            while(true)
            {
            //Looper.prepare();
            err=HardwareControler.select(serial_fd, 1, 0);
            if(err==1)//有数据可读
            {
                HardwareControler.read(serial_fd, serial_RevBuf, 10);
                s=new String(serial_RevBuf);
                System.out.println("接收到串口数据"+s);
            }
            else if(err==0) //无数据可读
            {
                System.out.println("没有接收到串口数据");    
            }
            else  //出错
            {
                System.out.println("接收到串口数据出错");    
            }
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         }
        }
        
    };

}

你可能感兴趣的:(c,android,String,Class,import,byte)