int com_fd = newPort.openPort(uart, 0, (char) longUart);这段代码错在哪里?

package com.example.newland.serialport;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.newland.jni.Convert;
import com.newland.jni.SerialPort;//串口类
import com.newland.jni.OnDataReceivedListener;//数据接收监听口
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private String strReceiveData="";//串口接收的数据
private Spinner spBaudRate,spUart;
private EditText etAck,etSend;
private TextView tvUartStatus;
private CheckBox chkSendHex,chkAckHex;
SerialPort newPort=new SerialPort();//定义一个串口
private int[] sectCom={0,1,2,3};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    initComponent();//初始化控件
    initSpinnerBaudRate();//初始化下拉框的值

    //PC机发送数据是不定时的,故采用响应模式来接收数据

    newPort.setCommReceivedMode(true);//设置串口通讯为响应模式

    //设置数据接收的监听器
    newPort.setOnDataReceivedListener(new OnDataReceivedListener() {
        public void onDataReceived(String data) {
            handleReceivedData(data);//处理接受的数据
        }
    });
}


/****************************************
 * 函数名称:initComponent
 * 功能描述:初始化界面
 * 参数及返回值说明:  *
 ****************************************/
private void initComponent(){
    //初始化控件
    tvUartStatus=(TextView)findViewById(R.id.tvUartStatus);
    etAck=(EditText) findViewById(R.id.etAck);
    etSend=(EditText)findViewById(R.id.etSend);
    spBaudRate=(Spinner)findViewById(R.id.spBaudRate);
    spUart=(Spinner)findViewById(R.id.spUart);
    chkSendHex=(CheckBox)findViewById(R.id.chkSendHex);
    chkAckHex=(CheckBox)findViewById(R.id.chkAckHex);
    chkSendHex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String str=etSend.getText().toString();
            try {
                if (isChecked) {
                    etSend.setText(Convert.stringToHexString(str));
                } else {
                    etSend.setText(Convert.hexStringToString(str));
                }
            }catch (Exception e){

            }

        }
    });
    //增加事件监听,实现字符串与十六进制互转
    chkAckHex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String str=etAck.getText().toString();
            try {
                if (isChecked) {
                    etAck.setText(Convert.stringToHexString(str));
                } else {
                    etAck.setText(Convert.hexStringToString(str));
                }
            }catch (Exception e){
                //TODO:handle exception

            }

        }
    });
}
/*******
 * 函数名称:initSpinnerBaudRate
 * 功能描述:初始化波特率的值
 * 参数及返回值说明:*
 *******/
private void initSpinnerBaudRate(){
    //下拉波特率的值
    String[] items={"1200","B2400","B4800","9600","19200","38400","57600","115200","230400","921600"};
    //第一步:为下拉列表定义一个适配器,这里就用到里前面定义的items。
    final ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,items);
    //第二步:为适配器配置下拉列表下拉时的菜单样式。
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //第三步:将适配器添加到下拉列表上
    spBaudRate.setAdapter(adapter);
    //默认选择第6项--57600
    spBaudRate.setSelection(6);
    //下拉串口的值
    String[] uarts={"COM0","COM1","COM2","COM3"};
    final ArrayAdapter<String>adapterUart=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,uarts);
    adapterUart.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spUart.setAdapter(adapterUart);
    spUart.setSelection(2);//默认选择第二项--COM2
}
/*******
 * 函数名称:myClick
 * 功能描述:按钮监听
 * 参数及返回值说明
 *******/

public void myClick(View v) {
    switch(v.getId()) {
        //打开串口
        case R.id.btnOpenUart:
            long longUart = spUart.getSelectedItemId();
            int uart = sectCom[(int) longUart];
            longUart = spBaudRate.getSelectedItemId();
            int com_fd = newPort.openPort(uart, 0, (char) longUart);
            if (com_fd > 0) {
                tvUartStatus.setText("打开成功!...");
            } else {
                tvUartStatus.setText("打开失败!...");}
            break;
        //关闭串口

        case R.id.btnCloseUart:
            newPort.closePort();
            tvUartStatus.setText("已关闭!...");
            break;
        //发送
        case R.id.btnSend:
            String msg = etSend.getText().toString();
            if (chkSendHex.isChecked()) {//以十六进制数发送
                newPort.sendCommand(Convert.hexStringToCharArray(msg));
            } else {//以字符串发送
                newPort.sendCommand(Convert.stringToCharArray(msg));
            }
            break;
        //清除按钮
        case R.id.btnClear:
            etAck.setText("");
            strReceiveData = "";
            break;
        //退出
        case R.id.btnQuit:
            System.exit(0);
        default:
            break;
    }
}
/************
 * 函数名称:handleReceiveData
 * 功能描述:处理数据
 * 参数说明:data,从串口中接收到的数据
 ************/

protected void handleReceivedData(final String data){
    runOnUiThread(new Runnable() {//通过runOnUiThread在线程中执行UI更新操作
        public void run() {
            strReceiveData = strReceiveData + data;
            if (chkAckHex.isChecked()) {//十六进制显示
                etAck.setText(Convert.dataToHexString(strReceiveData));
            }else {
                etAck.setText(Convert.dataToHexString(strReceiveData));}

        }
    });
}

@Override
protected void onDestroy(){
    super.onDestroy();
    newPort.onDestroy();
}

}

E/JIN: WARNING:could not load libuart.so
E/zygote: No implementation found for int com.newland.jni.Linuxc.openUart(int, int) (tried Java_com_newland_jni_Linuxc_openUart and Java_com_newland_jni_Linuxc_openUart__II)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.newland.serialport, PID: 25212
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6256) 
at android.view.View$PerformClick.run(View.java:24701) 
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Caused by: java.lang.UnsatisfiedLinkError: No implementation found for int com.newland.jni.Linuxc.openUart(int, int) (tried Java_com_newland_jni_Linuxc_openUart and Java_com_newland_jni_Linuxc_openUart__II)
at com.newland.jni.Linuxc.openUart(Native Method)
at com.newland.jni.SerialPort.openPort(SerialPort.java:87)
at com.example.newland.serialport.MainActivity.myClick(MainActivity.java:132)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6256) 
at android.view.View$PerformClick.run(View.java:24701) 
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Application terminated.

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问