主题 : 串口接收文件数据丢失 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 82264
精华: 0
发帖: 16
金钱: 80 两
威望: 16 点
综合积分: 32 分
注册时间: 2012-11-20
最后登录: 2013-06-14
楼主  发表于: 2012-11-20 14:54

 串口接收文件数据丢失

我在Android下想写一个串口接收文件的程序,通过Uart3来接收来自51单片机的文件数据,但是实际运行中,程序总是会丢失一小部分数据,比如300K的文件会丢失几百个字节。
因为之前的程序比较复杂,现在单独写了个简单的测试程序,通过定时器每隔几ms查询看串口有没有数据要读,如果有就关闭定时查询,进入while循环一直读取数据,并将数据写入文件,就这样一个简单的测试程序,还是会出现丢失数据的问题,想让大家帮忙看一下问题出在哪里(因为不知道串口缓冲有多大,所以我自己定义的buf是2048个字节)。

下面附上测试程序:

复制代码
  1. public class UartTestActivity extends Activity {
  2.     /** Called when the activity is first created. */
  3.     
  4.     private final int BAUDRATE_WIFI=115200;
  5.     private final int DATABITS_WIFI=8;
  6.     private final int STOPBITS_WIFI=1;
  7.     private final String SERIAL_PORT_WIFI="/dev/s3c2410_serial3";
  8.     private Handler mWifiHandler;
  9.     
  10.     private int serialToWifi;
  11.     Timer timer;
  12.     TimerTask task;
  13.     long totalLen=0;
  14.     
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.main);
  19.         
  20.         
  21.         try{
  22.             serialToWifi=HardwareControler.openSerialPort(SERIAL_PORT_WIFI, BAUDRATE_WIFI, DATABITS_WIFI, STOPBITS_WIFI);
  23.         }catch(Exception e){
  24.             Toast.makeText(this, "Wifi串口错误!", 500).show();
  25.         }
  26.         
  27.         mWifiHandler = new Handler(){
  28.             @Override
  29.             public void handleMessage(Message msg) {
  30.                 // TODO Auto-generated method stub
  31.                 int readLen;
  32.                 
  33.                 byte[] buf = new byte[2048];
  34.                 FileOutputStream fos = null;
  35.                 if(msg.arg1 == 1){
  36.                     try{
  37.                         if(HardwareControler.select(serialToWifi, 0, 0) == 1){
  38.                             stopRead();//////////////////////////////////////
  39.                             Log.i("ReadWifi", "有数据啦");
  40.                         
  41.                             fos = openFileOutput("update.txt",MODE_PRIVATE);
  42.                             
  43.                             
  44.                             long time = System.currentTimeMillis();
  45.                             
  46.                             while((System.currentTimeMillis() - time) < 60*1000){  //读1s,这里确定发送端肯定发送完毕了
  47.                                 if((readLen = HardwareControler.read(serialToWifi, buf, buf.length)) > 0){
  48.                                     totalLen += readLen;
  49.                                     Log.i("读到" + readLen + "字节", "总共" + totalLen + "字节");
  50.         
  51.                                     fos.write(buf, 0, readLen);
  52.                                 }
  53.                                 Log.i("读到0字节", "读到0字节");
  54.                             
  55.                             }
  56.                             fos.flush();
  57.                             fos.close();
  58.                         }
  59.                         //initRead();///////////////////////
  60.                     }
  61.                     catch(Exception e){
  62.                         e.printStackTrace();
  63.                     }
  64.                     
  65.                 }//end of if
  66.             }    //end of public void handlerMessage()        
  67.         };//end of new
  68.         
  69.         initRead();
  70.     }
  71.     
  72.     
  73.     public void initRead(){
  74.         timer = new Timer();
  75.         task = new TimerTask() {
  76.             
  77.             @Override
  78.             public void run() {
  79.                 // TODO Auto-generated method stub
  80.                 Message msg=mWifiHandler.obtainMessage();
  81.                 msg.arg1=1;
  82.                 mWifiHandler.sendMessage(msg);        
  83.             }
  84.         };
  85.         
  86.         timer.schedule(task, 1, 1);
  87.     }
  88.     
  89.     public void stopRead(){
  90.         if(timer != null){
  91.             timer.cancel();
  92.             timer = null;
  93.         }
  94.         if(task != null){
  95.             task.cancel();
  96.             task = null;
  97.         }
  98.     }
  99.     @Override
  100.     protected void onDestroy() {
  101.         // TODO Auto-generated method stub
  102.         stopRead();
  103.         super.onDestroy();
  104.     }
  105.     
  106.     
  107.     }