代码拉取完成,页面将自动刷新
package com.yian.serialtest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android_serialport_api.ComBean;
import android_serialport_api.SerialPort;
public class MainActivity extends Activity implements OnClickListener{
private Button openbtn;
private Button sendbtn;
private Button recvbtn;
private Button closebtn;
private SerialOperator mySerialOperator;
private DispQueueThread dispQueueThread;
private SerialPort sp;
private FileInputStream mInputStream;
private String outSend = "11224433";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button openbtn = (Button)findViewById(R.id.openbtn);
Button sendbtn = (Button)findViewById(R.id.sendbtn);
Button recvbtn = (Button)findViewById(R.id.recvbtn);
Button closebtn = (Button)findViewById(R.id.closebtn);
// Button btn = null ;
openbtn.setOnClickListener(this);
sendbtn.setOnClickListener(this);
recvbtn.setOnClickListener(this);
closebtn.setOnClickListener(this);
mySerialOperator = new SerialOperator();
try {
sp = new SerialPort(new File("/dev/ttys1"),115200, 0);
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mySerialOperator.setBaudRate("115200");
mySerialOperator.setPort("/dev/ttys1");
mInputStream=(FileInputStream)sp.getInputStream();
dispQueueThread = new DispQueueThread(){
@Override
public void run() {
super.run();
while (!isInterrupted()) {
int size = 0;
final ComBean ComData;
byte[] buffer = new byte[64];
if (mInputStream == null) return;
try {
size = mInputStream.read(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ComData = new ComBean("/dev/ttys1", buffer, size);
if (size > 0) {
mySerialOperator.onDataReceived(ComData);
}
}
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//打开串口
case R.id.openbtn:
try {
mySerialOperator.open();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
//发送数据
case R.id.sendbtn:
if (mySerialOperator != null && mySerialOperator.isOpen()) {
mySerialOperator.sendTxt(outSend);
}
break;
//接收数据
case R.id.recvbtn:
mySerialOperator.setDispQueueThread(dispQueueThread);
break;
//关闭串口
case R.id.closebtn:
if (mySerialOperator != null) {
mySerialOperator.stopSend();
mySerialOperator.close();
}
break;
default:
break;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。