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

sweetsnow24 2012-08-09 03:38:36
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();
}
}
}
}




}
代码如上就是一直接收不到串口发过来的数据,用工具打印信息的时候,信息是发送成功的。
...全文
446 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SueAnthony223 2013-10-21
  • 打赏
  • 举报
回复
后来解决了没?我也碰到这个问题
sweetsnow24 2013-01-19
  • 打赏
  • 举报
回复
引用 3 楼 colwer 的回复:
分给我吧
贴都结了。。。木有分啦。。。哈哈哈。。
colwer 2013-01-19
  • 打赏
  • 举报
回复
分给我吧
sweetsnow24 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
package peter.amlogic.serial;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.……
[/Quote]
示波器量是正常的。。
int tempv = in.read(buf);在这里设断点就会结束。。不会往下执行。。我调用ReadString()这个方法的时候是在线程里面调用的。。
xqhrs232 2012-08-09
  • 打赏
  • 举报
回复
自己多DEBUG,用示波器量量

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧