加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MainActivity.java.back 3.39 KB
一键复制 编辑 原始数据 按行查看 历史
Henry 提交于 2022-05-07 10:46 . Initial commit
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;
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化