Android串口通信(基于Tiny6410平台)
友善之臂的Android系统有他们自己编写的一个串口通信程序,网上没有找到他的源代码,而且界面操作不在一个界面,不是很方便,这里我自己写了一个粗糙点的串口通信程序。

- package geekle.lab;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.text.method.ScrollingMovementMethod;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.WindowManager;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.friendlyarm.AndroidSDK.HardwareControler;
- public class SerialPortActivity extends Activity
- {
- private static final String[] serial_port={"/dev/s3c2410_serial0","/dev/s3c2410_serial1","/dev/s3c2410_serial2"};
- private static final String[] baud_rate={"4800","9600","19200","115200"};
- TextView chooseserialPortView;
- TextView choosebaudRateView;
- TextView commucationView;
- EditText editmsg;
- private Button stopButton;
- private Button sendButton;
- private Spinner choose_serialport;
- private Spinner choose_baudrate;
- private ArrayAdapter<String> serialportAdapter;
- private ArrayAdapter<String> baudrateAdaptera;
- private int fd = 0;
- String thread = "readThread";
- String choosed_serial = "/dev/s3c2410_serial2";
- int choosed_buad = 19200;
- byte[] buf= new byte[300];
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
- chooseserialPortView = (TextView)findViewById(R.id.choose_serialPort_text);
- choose_serialport = (Spinner)findViewById(R.id.choose_seriaPort_spinner);
- chooseserialPortView = (TextView)findViewById(R.id.choose_baudRate_text);
- choose_baudrate = (Spinner)findViewById(R.id.choose_baudRate_spinner);
- serialportAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,serial_port);//建立下拉控件的适配器
- baudrateAdaptera = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,baud_rate);
- serialportAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
- baudrateAdaptera.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
- choose_serialport.setAdapter(serialportAdapter);//连接控件和适配器
- choose_baudrate.setAdapter(baudrateAdaptera);
- choose_serialport.setSelection(2);
- choose_baudrate.setSelection(2);
- choose_serialport.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
- {
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- choosed_serial = serial_port[arg2];
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- });
- choose_baudrate.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
- {
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- choosed_buad = Integer.parseInt(baud_rate[arg2]);
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- });
- fd = HardwareControler.openSerialPort(choosed_serial,choosed_buad, 8, 1);//打开串口
- if (fd != -1) {
- Toast.makeText(getApplicationContext(), getResources().getString(R.string.open_serial_success)+choosed_serial, 1).show();
- } else {
- Toast.makeText(this, getResources().getString(R.string.open_fail), 1).show();
- }
- stopButton = (Button)findViewById(R.id.stopButton);
- stopButton.setOnClickListener(new ClickEvent());
- sendButton = (Button)findViewById(R.id.sendButton);//发送消息
- sendButton.setOnClickListener(new OnClickListener() {
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- HardwareControler.write(fd, editmsg.getText().toString().getBytes());
- commucationView.append(editmsg.getText()+"\n");
- }
- });
- commucationView = (TextView)findViewById(R.id.commucation_window);
- commucationView.setMovementMethod(ScrollingMovementMethod.getInstance()); //让textview实现滚动
- editmsg = (EditText)findViewById(R.id.editmsg);
- new readThread().start();//开始串口的监听线程
- }
- public class ClickEvent implements Button.OnClickListener//退出
- {
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- android.os.Process.killProcess(android.os.Process.myPid());
- System.exit(0);
- }
- }
- Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.arg1) {
- case 0:
- int len = HardwareControler.read(fd, buf, 300);
- String string = new String(buf, 0, len);
- commucationView.append(string+"\n");
- new readThread().start();//处理完消息后立即开启监听线程
- Log.d(thread,"接收到数据,新线程启动");
- break;
- case 1:
- HardwareControler.setLedState(1, 0);
- new readThread().start();
- // Log.d(thread,"没有数据,新线程启动");
- break;
- default:
- break;
- }
- }
- };
- class readThread extends Thread//读取串口信息线程
- {
- public void run()
- {
- Message msg = new Message();
- HardwareControler.setLedState(0, 0);
- if (HardwareControler.select(fd,5, 0)==1) {
- msg.arg1 = 0;
- }
- else {
- msg.arg1 =1;
- HardwareControler.setLedState(0, 1);
- }
- handler.sendMessage(msg);
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/choose_serialPort_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@+string/chooseserialPort" />
- <Spinner
- android:id="@+id/choose_seriaPort_spinner"
- android:layout_width="wrap_content"
- android:layout_height="40dp" >
- </Spinner>
- <TextView
- android:id="@+id/choose_baudRate_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@+string/choosebaudRate" />
- <Spinner
- android:id="@+id/choose_baudRate_spinner"
- android:layout_width="wrap_content"
- android:layout_height="40dp" >
- </Spinner>
- <TextView
- android:id="@+id/commucation_window"
- android:layout_width="fill_parent"
- android:layout_height="190dp" >
- </TextView>
- <EditText
- android:id="@+id/editmsg"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="edit here" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/sendButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="@+string/send" />
- <Button
- android:id="@+id/stopButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="@string/stopButton" />
- </LinearLayout>
- </LinearLayout>
Android串口通信(基于Tiny6410平台)的更多相关文章
- Android串口通信
前段时间因为工作需要研究了一下android的串口通信,网上有很多讲串口通信的文章,我在做的时候也参考了很多文章,现在就将我学习过程中的一些心得分享给大家,希望可以帮助大家在学习的时候少走一些弯路,有 ...
- Java串口通信--------基于RXTX (附带资源地址)
最近帮老师做了一个小项目,一个牧场公司想用传感器收集一些环境信息,记录到数据库里去,然后加以分析查看.这里面和传感器通信用到了串口通信,我也是接触了一下,把用到的东西分享出来. 准备工作: RXTX: ...
- Android串口通信(Android Studio)
gilhub上已有开源项目: https://github.com/cepr/android-serialport-api 可以直接使用
- Android串口开发
参考资料: https://www.jianshu.com/p/9249ed03e745 GitHUb地址: https://github.com/AIlll/AndroidSerialPort An ...
- 基于FPGA的红外遥控解码与PC串口通信
基于FPGA的红外遥控解码与PC串口通信 zouxy09@qq.com http://blog.csdn.net/zouxy09 这是我的<电子设计EDA>的课程设计作业(呵呵,这个月都拿 ...
- 【毕业设计】基于Android的家校互动平台开发(内含完整代码和所有文档)——爱吖校推(你关注的,我们才推)
☆ 写在前面 之前答应大家的毕业答辩之后把所有文档贡献出来,现在答辩已过,LZ信守承诺,把所有文档开源到了GitHub(这个地址包含所有的代码和文档以及PPT,外层为简单的代码).还望喜欢的朋友们,不 ...
- Android 串口蓝牙通信开发Java版本
Android串口BLE蓝牙通信Java版 0. 导语 Qt on Android 蓝牙通信开发 我们都知道,在物联网中,BLE蓝牙是通信设备的关键设备.在传统的物联网应用中,无线WIFI.蓝牙和Zi ...
- VS2008基于对话框的MFC上位机串口通信(C++实现)简单例程
首先,在 vs2008 环境下创建 MFC 运用程序 设置项目名称为 ComTest(这个地方随意命名,根据个人习惯),点击确定后,点击下一步 出现如下界面 选择"基于对话框"模式 ...
- android 串口开发第二篇:利用jni实现android和串口通信
一:串口通信简介 由于串口开发涉及到jni,所以开发环境需要支持ndk开发,如果未配置ndk配置的朋友,或者对jni不熟悉的朋友,请查看上一篇文章,android 串口开发第一篇:搭建ndk开发环境以 ...
随机推荐
- id,class,name区别
id,class,name区别 id:标签唯一标识,好比我们身份证号码,具有唯一性.JS常用document,getGlementBy(id). class:标签的类别,可重复使用,CSS常用. na ...
- ◆linux分区的加密与自动解密◆——Super孟再创辉煌
首先制作分区的加密挂载: 分区的自动解密:
- centos5.4下mysql主从复制
centos5.4下mysql主从复制配置分享. 本文转自:http://www.jbxue.com/article/771.html 安装环境:centos 5.4 mysql版本:mysql 5. ...
- centos问题集锦
一. 为什么新装的centos系统无法使用xshell,putty等工具连接? 原因:sshd服务没有启动. 解决: 1)使用命令rpm -qa | grep ssh查看是否已经安装了ssh 2)使用 ...
- 【zendstudio】如何利用zendstudio新建 或导入php项目
一.利用ZendStudio创建 PHP Project 1. 打开ZendStudio, 选择:File à New à PHP Project, 如下图所示: 于是弹出如下界面: 在”Projec ...
- Delphi 两个应用程序(进程)之间的通信
两个应用程序之间的通信实际上是两个进程之间的通信.由于本人知识有限,决定应用消息来实现.需要用到的知识: 1.RegisterWindowMessage(); //参数类型:pchar:返回值:Lon ...
- 使用dxNavBar动态创建应用程序菜单
一.如何动态创建dxNavBar内容: function TMain.GetAcitonByCaption(const aCategory,aCaption: string): Integer; va ...
- exec 和 source的区别
source 就是让 script 在当前 shell 内执行.而不是产生一个 sub-shell 来执行.由exec 也是让 script 在同一个行程上执行,但是原有行程则被结束了. source ...
- 深入剖析——float之个人见解
浮动的原本作用仅仅是为了实现文字的环绕效果. 以下分别是html与css代码,显示效果如下图.因为两个div使用了float浮动属性,所以脱离了标准文档流.让父元素撑开高度,我们需要清除浮动. < ...
- HTTP报文格式
请求报文 <method><SP><uri><SP><version><CRLF> <head-name><: ...