首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

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

2013-01-28 
android 平台下串口接收数据的问题package peter.amlogic.serialimport java.io.FileInputStreamimport

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 < tempv)
{
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) {


// TODO Auto-generated catch block
e.printStackTrace();
}/////////////
}
if(in != null)
{
//try {
//in.close();
//in = null;
//} catch (IOException e) {
//// TODO Auto-generated catch block
//e.printStackTrace();
//}
}
if(out != null)
{
try
{
out.close();
out = null;
}
catch(IOException e)
{
e.printStackTrace();
}////////////
}
}
}

private static void sleep(int i) {
// TODO Auto-generated method stub

}

public static void WriteString()
{
FileWriter fw = null;
byte tempv = 'a';
try {
fw = new FileWriter(DEV);
for(int i=0; i<10; i++)
{
fw.write(tempv + i);
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(fw != null)
{
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}




}
代码如上就是一直接收不到串口发过来的数据,用工具打印信息的时候,信息是发送成功的。
[解决办法]
自己多DEBUG,用示波器量量

热点排行