android串口无数据,android 平台上串口接收数据的有关问题

当前位置:我的异常网» Android » android 平台上串口接收数据的有关问题

android 平台上串口接收数据的有关问题

www.myexceptions.net  网友分享于:2013-01-29  浏览:185次

android 平台下串口接收数据的问题

package peter.amlogic.serial;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import peter.amlogic.player.PanelItemEventType;

import peter.amlogic.player.PlayerEvent;

import peter.amlogic.serial.message.BaseRecMessage;

import peter.amlogic.serial.message.BaseSendMessage;

import peter.amlogic.utility.DeviceInfo;

import android.util.Log;

public class SerailProcess

{

private static final String TAG = "Serial";

private static final String DEV = "/dev/ttyS1";

private static byte[] buf = new byte[1024];

private static FileInputStream in = null;

public static void ReadString()

{

FileReader fr = null;

//FileInputStream in = null;

FileOutputStream out = null;

try {

if(in == null)

{

in = new FileInputStream(DEV);

}

int tempv = in.read(buf);

//in.close();

//in = null;

if(tempv > 0)

{

Log.d(TAG, "the tempv is: " + tempv);

int i = 0;

/*

while(i 

{

Log.d(TAG, " the " + i + " value is: " + (int)buf[i]);

i++;

}

*/

int flag = 0;

while((tempv - flag) > 0)

{

BaseRecMessage brm = new BaseRecMessage();

int leng = brm.ParseMessage(buf, flag);

if(leng > 0)

{

PlayerEvent pe = new PlayerEvent();

pe.setEventType(PanelItemEventType.SERIAL_MSG);

pe.setEventArgs(brm);

DeviceInfo.getInstance().getPlayerEvent().offer(pe);

Log.d(TAG, "the cmd " + brm.getCmd() + " leng is: " + leng);

}

else

{

Log.d(TAG, "parse message occur error");

}

try

{

out = new FileOutputStream(DEV);

BaseSendMessage bsm = brm.getSendMessage();

if(bsm != null)

{

out.write(bsm.getSendBuf());

}

out.close();

out = null;

}

catch(Throwable te)

{

te.printStackTrace();

}

finally

{

if(out != null)

{

try

{

out.close();

out = null;

}

catch(IOException e)

{

e.printStackTrace();

}

}

}

flag += leng;

}

}

else

{

sleep(100);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally

{

if(fr != null)

{

try {

fr.close();

} catch (IOException e) {

文章评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值