项目名称:社区安防系统
设备:ADAM4150、烟雾传感器,火焰传感器,人体传感器、串口线
第三方库:新大陆公司的Analog4150库
实验现象:ADAM4150通过串口线连接到移动终端com6口。打开程序:当烟雾传感器检测到有烟雾时,界面提醒。当人体传感器检测到有人时会提醒。当火焰传感器检测到有火焰时,界面提醒同时出现火焰图片
步骤:
1、界面设计。在res/layout目录下的action_main.xml文件中进行修改。
2、新建一个BasePort.java调用新大陆提供的Analog4150库,实现串口打开和关闭功能。
import com.example.analoglib.Analog4150ServiceAPI;
public class BasePort {
public int openADAMPort(int com,int mode,int baudRate){
return Analog4150ServiceAPI.openPort(com, mode, baudRate);
}
public void closeADAMPort(){
Analog4150ServiceAPI.closeUart();
}
}
3、新建一个ADA4150.java文件,实现ADAM4150的控制功能和获取状态功能
import android.util.Log;
import com.example.analoglib.Analog4150ServiceAPI;
import com.example.analoglib.OnFireResponse;
import com.example.analoglib.OnPersonResponse;
import com.example.analoglib.OnSmorkResponse;
import com.example.analoglib.ReceiveThread;
public class ADAM4150 extends BasePort{
//风扇开关命令,这些命名是根据协议的
private final char[] open1Fan={0x01,0x05,0x00,0x10,0xFF,0x00,0x8D,0xFF};
private final char[] close1Fan={0x01,0x05,0x00,0x10,0x00,0x00,0xCC,0x0F};
private final char[] open2Fan={0x01,0x05,0x00,0x11,0xFF,0x00,0xDC,0x3F};
private final char[] close2Fan={0x01,0x05,0x00,0x11,0x00,0x00,0x9D,0xCF};
//创建ADAM4150类的构造方法,并实现人体、烟雾、火焰的实时取值,并用get方法返回
public static int mADAM4150_fd=0;
private boolean rePerson;
private boolean reFire;
private boolean reSmork;
public ADAM4150(int com,int mode,int baudRate){
//打开串口
mADAM4150_fd=openADAMPort(com, mode, baudRate);
ReceiveThread mReceiveThread=new ReceiveThread();
mReceiveThread.start();
//设置人体回调函数,人体传感器接入DI0
Analog4150ServiceAPI.getPerson("person", new OnPersonResponse() {
@Override
public void onValue(boolean arg0) {
rePerson=!arg0;
}
@Override
public void onValue(String arg0) {
Log.e("cjI",arg0);
}
});
Analog4150ServiceAPI.getFire("fire", new OnFireResponse() {
@Override
public void onValue(boolean arg0) {
reFire=!arg0;
}
@Override
public void onValue(String arg0) {
Log.e("cjI",arg0);
}
});
Analog4150ServiceAPI.getSmork("Smork", new OnSmorkResponse() {
@Override
public void onValue(boolean arg0) {
reSmork=!arg0;
}
@Override
public void onValue(String arg0) {
Log.e("cjI",arg0);
}
});
}
//获取传感器值,true或者false
public boolean getPerson(){
return rePerson;
}
public boolean getFire(){
return reFire;
}
public boolean getSmork(){
return reSmork;
}
//打开风扇
public void openFan1(){
Analog4150ServiceAPI.sendRelayControl(open1Fan);
}
public void openFan2(){
Analog4150ServiceAPI.sendRelayControl(open2Fan);
}
public void closeFan1(){
Analog4150ServiceAPI.sendRelayControl(close1Fan);
}
public void closeFan2(){
Analog4150ServiceAPI.sendRelayControl(close2Fan);
}
}
4、在MainActivity.java类中添加多线程逻辑控制界面的改变
import com.example.analoglib.Analog4150ServiceAPI;
import com.example.analoglib.AnalogHelper;
import com.example.analoglib.OnFireResponse;
import com.example.analoglib.OnPersonResponse;
import com.example.analoglib.OnReceiveCompleteListener;
import com.example.analoglib.OnSmorkResponse;
import com.example.analoglib.ReceiveThread;
import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ImageView;
public class MainActivity extends Activity {
private TextView mTvFire;
private TextView mTvPerson;
private TextView mTvSmoke;
private ImageView mImgFire;
private ADAM4150 mAdam4150;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();//初始化控件
}
private void initView() {
mTvFire = (TextView) findViewById(R.id.textView1);
mTvPerson = (TextView) findViewById(R.id.textView3);
mTvSmoke = (TextView) findViewById(R.id.textView2);
mImgFire=(ImageView) findViewById(R.id.imageView2);
//com6口,USB模式,9600波特率
mAdam4150=new ADAM4150(6,1,3);
mHandler.postDelayed(mRunnable,ms);
}
private int ms=1000;
//声明一个Handler对象
private Handler mHandler=new Handler();
//声明一个Runnable对象
private Runnable mRunnable=new Runnable() {
@Override
public void run() {
mHandler.postDelayed(mRunnable,ms);
mTvPerson.setText(mAdam4150.getPerson()?"无人":"有人");
mTvFire.setText(mAdam4150.getFire()?"无火":"有火");
mTvSmoke.setText(mAdam4150.getSmork()?"正常":"危险");
//如果有火,显示火焰图片。否则就不显示火焰图片
if(mAdam4150.getFire()){
mImgFire.setVisibility(View.GONE);
}else{
mImgFire.setVisibility(View.VISIBLE);
// long ms=500;
// Vibrate(MainActivity.this,ms);
}
Analog4150ServiceAPI.send4150();
}
};
//自定义Vibrate方法,调用系统自身的服务,实现震动功能
public static void Vibrate(final Activity activity,long milliseconds){
Vibrator vib=(Vibrator) activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(milliseconds);
}
}
5、这里要注意的是烟雾、火焰所接的ADAM4150口亮灭分别有和无,但是人体红外的亮灭是表示无有