码迷,mamicode.com
首页 > 其他好文 > 详细

社区安防系统

时间:2017-05-30 17:50:24      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:import   alt   false   ted   this   help   extend   end   port   

项目名称:社区安防系统

设备:ADAM4150、烟雾传感器,火焰传感器,人体传感器、串口线

第三方库:新大陆公司的Analog4150库

实验现象:ADAM4150通过串口线连接到移动终端com6口。打开程序:当烟雾传感器检测到有烟雾时,界面提醒。当人体传感器检测到有人时会提醒。当火焰传感器检测到有火焰时,界面提醒同时出现火焰图片

步骤:

1、界面设计。在res/layout目录下的action_main.xml文件中进行修改。

技术分享

2、新建一个BasePort.java调用新大陆提供的Analog4150库,实现串口打开和关闭功能。

  1. import com.example.analoglib.Analog4150ServiceAPI;
  2. public class BasePort {
  3. public int openADAMPort(int com,int mode,int baudRate){
  4. return Analog4150ServiceAPI.openPort(com, mode, baudRate);
  5. }
  6. public void closeADAMPort(){
  7. Analog4150ServiceAPI.closeUart();
  8. }
  9. }

3、新建一个ADA4150.java文件,实现ADAM4150的控制功能和获取状态功能

  1. import android.util.Log;
  2. import com.example.analoglib.Analog4150ServiceAPI;
  3. import com.example.analoglib.OnFireResponse;
  4. import com.example.analoglib.OnPersonResponse;
  5. import com.example.analoglib.OnSmorkResponse;
  6. import com.example.analoglib.ReceiveThread;
  7. public class ADAM4150 extends BasePort{
  8. //风扇开关命令,这些命名是根据协议的
  9. private final char[] open1Fan={0x01,0x05,0x00,0x10,0xFF,0x00,0x8D,0xFF};
  10. private final char[] close1Fan={0x01,0x05,0x00,0x10,0x00,0x00,0xCC,0x0F};
  11. private final char[] open2Fan={0x01,0x05,0x00,0x11,0xFF,0x00,0xDC,0x3F};
  12. private final char[] close2Fan={0x01,0x05,0x00,0x11,0x00,0x00,0x9D,0xCF};
  13. //创建ADAM4150类的构造方法,并实现人体、烟雾、火焰的实时取值,并用get方法返回
  14. public static int mADAM4150_fd=0;
  15. private boolean rePerson;
  16. private boolean reFire;
  17. private boolean reSmork;
  18. public ADAM4150(int com,int mode,int baudRate){
  19. //打开串口
  20. mADAM4150_fd=openADAMPort(com, mode, baudRate);
  21. ReceiveThread mReceiveThread=new ReceiveThread();
  22. mReceiveThread.start();
  23. //设置人体回调函数,人体传感器接入DI0
  24. Analog4150ServiceAPI.getPerson("person", new OnPersonResponse() {
  25. @Override
  26. public void onValue(boolean arg0) {
  27. rePerson=!arg0;
  28. }
  29. @Override
  30. public void onValue(String arg0) {
  31. Log.e("cjI",arg0);
  32. }
  33. });
  34. Analog4150ServiceAPI.getFire("fire", new OnFireResponse() {
  35. @Override
  36. public void onValue(boolean arg0) {
  37. reFire=!arg0;
  38. }
  39. @Override
  40. public void onValue(String arg0) {
  41. Log.e("cjI",arg0);
  42. }
  43. });
  44. Analog4150ServiceAPI.getSmork("Smork", new OnSmorkResponse() {
  45. @Override
  46. public void onValue(boolean arg0) {
  47. reSmork=!arg0;
  48. }
  49. @Override
  50. public void onValue(String arg0) {
  51. Log.e("cjI",arg0);
  52. }
  53. });
  54. }
  55. //获取传感器值,true或者false
  56. public boolean getPerson(){
  57. return rePerson;
  58. }
  59. public boolean getFire(){
  60. return reFire;
  61. }
  62. public boolean getSmork(){
  63. return reSmork;
  64. }
  65. //打开风扇
  66. public void openFan1(){
  67. Analog4150ServiceAPI.sendRelayControl(open1Fan);
  68. }
  69. public void openFan2(){
  70. Analog4150ServiceAPI.sendRelayControl(open2Fan);
  71. }
  72. public void closeFan1(){
  73. Analog4150ServiceAPI.sendRelayControl(close1Fan);
  74. }
  75. public void closeFan2(){
  76. Analog4150ServiceAPI.sendRelayControl(close2Fan);
  77. }
  78. }

4、在MainActivity.java类中添加多线程逻辑控制界面的改变

  1. import com.example.analoglib.Analog4150ServiceAPI;
  2. import com.example.analoglib.AnalogHelper;
  3. import com.example.analoglib.OnFireResponse;
  4. import com.example.analoglib.OnPersonResponse;
  5. import com.example.analoglib.OnReceiveCompleteListener;
  6. import com.example.analoglib.OnSmorkResponse;
  7. import com.example.analoglib.ReceiveThread;
  8. import android.app.Activity;
  9. import android.app.Service;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.os.Message;
  13. import android.os.Vibrator;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.TextView;
  17. import android.widget.ImageView;
  18. public class MainActivity extends Activity {
  19. private TextView mTvFire;
  20. private TextView mTvPerson;
  21. private TextView mTvSmoke;
  22. private ImageView mImgFire;
  23. private ADAM4150 mAdam4150;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. initView();//初始化控件
  29. }
  30. private void initView() {
  31. mTvFire = (TextView) findViewById(R.id.textView1);
  32. mTvPerson = (TextView) findViewById(R.id.textView3);
  33. mTvSmoke = (TextView) findViewById(R.id.textView2);
  34. mImgFire=(ImageView) findViewById(R.id.imageView2);
  35. //com6口,USB模式,9600波特率
  36. mAdam4150=new ADAM4150(6,1,3);
  37. mHandler.postDelayed(mRunnable,ms);
  38. }
  39. private int ms=1000;
  40. //声明一个Handler对象
  41. private Handler mHandler=new Handler();
  42. //声明一个Runnable对象
  43. private Runnable mRunnable=new Runnable() {
  44. @Override
  45. public void run() {
  46. mHandler.postDelayed(mRunnable,ms);
  47. mTvPerson.setText(mAdam4150.getPerson()?"无人":"有人");
  48. mTvFire.setText(mAdam4150.getFire()?"无火":"有火");
  49. mTvSmoke.setText(mAdam4150.getSmork()?"正常":"危险");
  50. //如果有火,显示火焰图片。否则就不显示火焰图片
  51. if(mAdam4150.getFire()){
  52. mImgFire.setVisibility(View.GONE);
  53. }else{
  54. mImgFire.setVisibility(View.VISIBLE);
  55. // long ms=500;
  56. // Vibrate(MainActivity.this,ms);
  57. }
  58. Analog4150ServiceAPI.send4150();
  59. }
  60. };
  61. //自定义Vibrate方法,调用系统自身的服务,实现震动功能
  62. public static void Vibrate(final Activity activity,long milliseconds){
  63. Vibrator vib=(Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
  64. vib.vibrate(milliseconds);
  65. }
  66. }

5、这里要注意的是烟雾、火焰所接的ADAM4150口亮灭分别有和无,但是人体红外的亮灭是表示无有

社区安防系统

标签:import   alt   false   ted   this   help   extend   end   port   

原文地址:http://www.cnblogs.com/aqyl/p/6920675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!