packagecom.hyperlcd.serialport;
importandroid.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
importandroid.text.TextUtils;
importandroid.view.Gravity;
importandroid.widget.CompoundButton;
importandroid.widget.EditText;
importandroid.widget.RadioButton;
importandroid.widget.RadioGroup;
importandroid.widget.TextView;
importandroid.widget.Toast;
importandroid_serialport_api.hyperlcd.LogInterceptorSerialPort;
importandroid_serialport_api.hyperlcd.ReadListener;
importandroid_serialport_api.hyperlcd.SerialPort;
importandroid_serialport_api.hyperlcd.SerialPortManager;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
privateRadioGroupserialRG;
privateTextViewserialTitle;
privateTextViewcodeTitle;
private String currentPort;
privateSerialPortserialPort;
privateReadListenerreadListener;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
private void initData() {
SerialPortManager.getInstances().initSerialPort();
SerialPortManager.getInstances().setLogInterceptor(new LogInterceptorSerialPort() {
public void log(@SerialPortManager.Type final String type, final String port, final booleanisAscii, final String log) {
Log.d("SerialPortLog", new StringBuffer()
.append("串口号:").append(port)
.append("\n数据格式:").append(isAscii ? "ascii" : "hexString")
.append("\n操作类型:").append(type)
.append("操作消息:").append(log).toString());
runOnUiThread(new Runnable() {
logTV.append(new StringBuffer()
.append(" ").append(port)
.append(" ").append(isAscii ? "ascii" : "hexString")
.append(" ").append(type)
.append("\n").toString());
serialPort = SerialPortManager.getInstances().getSerialPort();
readListener = new ReadListener() {
public void onRead(final String port, final booleanisAscii, final String read) {
Log.d("SerialPortRead", new StringBuffer()
.append(port).append("/").append(isAscii ? "ascii" : "hex")
.append(" read:").append(read).append("\n").toString());
runOnUiThread(new Runnable() {
readTV.append(new StringBuffer()
.append(port).append("/").append(isAscii ? "ascii" : "hex")
.append(" read:").append(read).append("\n").toString());
private void initView() {
codeRG = (RadioGroup) findViewById(R.id.rg_code);
serialRG = (RadioGroup) findViewById(R.id.rg_serial);
serialET = (EditText) findViewById(R.id.et_serial);
sendET = (EditText) findViewById(R.id.et_send);
serialTitle = (TextView) findViewById(R.id.title_serial);
codeTitle = (TextView) findViewById(R.id.title_code);
readTV = (TextView) findViewById(R.id.tv_read);
logTV = (TextView) findViewById(R.id.tv_log);
codeRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, @IdResintcheckedId) {
changeCode(checkedId == R.id.rb_ascii);
serialRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, @IdResintcheckedId) {
if (checkedId == R.id.rb_other) {
serialET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, booleanhasFocus) {
serialRG.check(R.id.rb_other);
findViewById(R.id.btn_open).setOnClickListener(this);
findViewById(R.id.btn_close).setOnClickListener(this);
findViewById(R.id.clear_send).setOnClickListener(this);
findViewById(R.id.btn_send).setOnClickListener(this);
findViewById(R.id.clear_read).setOnClickListener(this);
findViewById(R.id.clear_log).setOnClickListener(this);
codeRG.check(R.id.rb_ascii);
serialRG.check(R.id.rb_com0);
protected void onDestroy() {
SerialPortManager.getInstances().destroySerialPort();
public void onClick(View v) {
if (serialPort == null) {
if (TextUtils.isEmpty(currentPort)) {
String send = sendET.getText().toString().trim();
if (TextUtils.isEmpty(send)) {
serialPort.writeSerialService(currentPort, isAscii, send);
if (serialPort == null) {
String checkPort = getCurrentPort();
if (TextUtils.isEmpty(checkPort)) {
} else if (TextUtils.equals(checkPort, SerialPortManager.other)) {
checkPort = serialET.getText().toString().trim();
if (TextUtils.isEmpty(checkPort)) {
if (TextUtils.equals(currentPort, checkPort)) {
if (!TextUtils.isEmpty(currentPort)) {
serialPort.stopSerialPort(currentPort);
isAscii = codeRG.getCheckedRadioButtonId() == R.id.rb_ascii;
serialPort.startSerialPort(checkPort, isAscii, readListener);
serialTitle.setText("串口:");
serialTitle.append(currentPort);
codeTitle.setText("数据格式:");
codeTitle.append(isAscii ? "ASCII" : "HexString");
if (!TextUtils.isEmpty(currentPort)) {
serialPort.stopSerialPort(currentPort);
serialTitle.setText("串口");
codeTitle.setText("数据格式");
* @paramisAsciitrue:asciifalse:HexString
private void changeCode(booleanisAscii) {
if (TextUtils.isEmpty(currentPort)) {
serialPort.setReadCode(currentPort, isAscii);
codeTitle.setText("数据格式:");
codeTitle.append(isAscii ? "ASCII" : "HexString");
private String getCurrentPort() {
switch (serialRG.getCheckedRadioButtonId()) {
checkPort = SerialPortManager.ttyCOM0;
checkPort = SerialPortManager.ttyCOM1;
checkPort = SerialPortManager.ttyCOM2;
checkPort = SerialPortManager.ttyCOM3;
checkPort = SerialPortManager.ttyS0;
checkPort = SerialPortManager.ttyS1;
checkPort = SerialPortManager.ttyS2;
checkPort = SerialPortManager.ttyS3;
checkPort = SerialPortManager.other;
private void T(String message) {
toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
textView = new TextView(this);
textView.setTextColor(0xffffffff);
textView.setTextSize(30);
textView.setPadding(10, 5, 10, 5);
textView.setBackgroundResource(R.drawable.shape_toast_bg);
toast.setGravity(Gravity.CENTER, 0, 0);
textView.setText(message);