计算机串口设置失败,请大神帮我看看这是怎么回事,一用就说串口打开失败,再电脑上试...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

package com.example.fandemonew;

import com.example.demo.library.SerialPort;

import com.example.demo.library.ShareData;

import com.example.demo.library.ZigBeeService;

import com.newland.jni.Linuxc;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.Toast;

public class MainActivity extends Activity {

private char[] fanOpenCommand= {0xFF,0xF5,0x05,0x02,0x34,0x12,0x00,0x01,0x00};

private char[] fanCloseCommand= {0xFF,0xF5,0x05,0x02,0x34,0x12,0x00,0x02,0x00};

private char[] fan2OpenCommand= {0xFF,0xF5,0x05,0x02,0x01,0x00,0x00,0x01,0x03};

private char[] fan2CloseCommand= {0xFF,0xF5,0x05,0x02,0x01,0x00,0x00,0x02,0x02};

private String mControlName;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

OpenZigBee();

initView();

}

private void OpenZigBee() {

if (ShareData.com_fdZigBee > 0)

SerialPort.closePort(ShareData.com_fdZigBee);

// 读取串口设置值,无线传感网默认值为0

ShareData.com_fdZigBee = SerialPort.openPort(3, 0, 5);

if (ShareData.com_fdZigBee == -1) {

Toast.makeText(this, "串口打开失败", Toast.LENGTH_LONG).show();

}

Intent intent = new Intent(this, ZigBeeService.class);

this.startService(intent);

}

private void initView(){

String[] strs = {"请选择","风扇1","风扇2"};

Spinner sp = (Spinner) findViewById(R.id.sp);

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, strs);

sp.setAdapter(adapter);

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override

public void onItemSelected(AdapterView> parent, View arg1,

int position, long arg3) {

String itemName = parent.getItemAtPosition(position).toString();

mControlName = itemName;

}

@Override

public void onNothingSelected(AdapterView> arg0) {

// TODO Auto-generated method stub

}

});

findViewById(R.id.open_btn).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

if("风扇1".equals(mControlName)){

sendCMD(fanOpenCommand);

}else if("风扇2".equals(mControlName)){

sendCMD(fan2OpenCommand);

}

}

});

findViewById(R.id.close_btn).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

if("风扇1".equals(mControlName)){

sendCMD(fanCloseCommand);

}else if("风扇2".equals(mControlName)){

sendCMD(fan2CloseCommand);

}

}

});

}

private static void sendCMD(char[] cmd) {

String strcmd = String.valueOf(cmd);

Linuxc.sendMsgUartHex(ShareData.com_fdZigBee, strcmd, strcmd.length());

}

@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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值