android 串口開發第二篇:利用jni實現android和串口通訊

一:串口通訊簡介

  因爲串口開發涉及到jni,因此開發環境須要支持ndk開發,若是未配置ndk配置的朋友,或者對jni不熟悉的朋友,請查看上一篇文章,android 串口開發第一篇:搭建ndk開發環境以及第一個jni調用程序 ,串口通訊和java操做io相似,先打開串口,而後向串口發送或者讀取數據,最後關閉串口,因此基本思路就是:html

  1.對串口文件進行配置(波特率等),選擇串口文件,打開串口,設備不一樣 ,能夠讀寫的串口也不一樣.java

  2.讀寫串口 ,讀串口須要開一個子線程,而後死循環讀取串口發送的數據android

  3.關閉串口文件ios

       其中打開,關閉串口是在jni方法執行,讀寫操做是android程序執行。c++

二:代碼實現

  個人開發環境是android studio 2.3.3 串口開發我建立一個支持c++項目,而後在cpp目錄下,建立一個nateve-lib.cpp的程序,將串口打開,串口關閉的程序複製進去便可,native-lib程序中方法的命名規則須要根據你實際狀況,稍做修改,cpp中方法名格式爲,Java_包名_調用jni方法的類名_方法名,如Java_com_serialportdemo_SerialPort_open,此處必定要注意,android studio生成的是cpp程序,不是c程序,這兩個有一些區別的,好比:git

我對c也不熟悉,如下語法有誤請指出

*.c的語法 變量定義 jstring jstr2 = (*env) -> NewStringUTF(env, cstr); 方法定義 JNIEXPORT jstring JNICALL Java_com_serialportdemo_MainActivity_encode() JNIEXPORT jstring JNICALL Java_com_serialportdemo_MainActivity_decode() *.cpp的語法 jstring jstr2 =env->NewStringUTF(hello.c_str()); extern "C" //若是這裏不寫extern "C",程序編譯不會錯,但android沒法調用該方法,錯誤日誌是找不到該方法 JNIEXPORT jstring JNICALL Java_com_serialportdemo_MainActivity_encode() extern "C" JNIEXPORT jstring JNICALL Java_com_serialportdemo_MainActivity_decode()

 

串口打開,串口關閉代碼以下:github

//獲取波特率
static speed_t getBaudrate(jint baudrate)
{
    switch(baudrate) {
    case 0: return B0;
    case 50: return B50;
    case 75: return B75;
    case 110: return B110;
    case 134: return B134;
    case 150: return B150;
    case 200: return B200;
    case 300: return B300;
    case 600: return B600;
    case 1200: return B1200;
    case 1800: return B1800;
    case 2400: return B2400;
    case 4800: return B4800;
    case 9600: return B9600;
    case 19200: return B19200;
    case 38400: return B38400;
    case 57600: return B57600;
    case 115200: return B115200;
    case 230400: return B230400;
    case 460800: return B460800;
    case 500000: return B500000;
    case 576000: return B576000;
    case 921600: return B921600;
    case 1000000: return B1000000;
    case 1152000: return B1152000;
    case 1500000: return B1500000;
    case 2000000: return B2000000;
    case 2500000: return B2500000;
    case 3000000: return B3000000;
    case 3500000: return B3500000;
    case 4000000: return B4000000;
    default: return -1;
    }
}


//打開串口程序
extern "C"
JNIEXPORT jobject JNICALL
Java_com_serialportdemo_SerialPort_open(JNIEnv *env, jobject thiz, jstring path,jint baudrate) {
    int fd;
    speed_t speed;
    jobject mFileDescriptor;

    LOGD("init native Check arguments");
    /* Check arguments */
    {
        speed = getBaudrate(baudrate);
        if (speed == -1) {
            /* TODO: throw an exception */
            LOGE("Invalid baudrate");
            return NULL;
        }
    }

    LOGD("init native Opening device!");
    /* Opening device */
    {
        jboolean iscopy;
        const char *path_utf = env->GetStringUTFChars(path, &iscopy);
        LOGD("Opening serial port %s", path_utf);
//      fd = open(path_utf, O_RDWR | O_DIRECT | O_SYNC);
        fd = open(path_utf, O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY);
        LOGD("open() fd = %d", fd);
        env->ReleaseStringUTFChars(path, path_utf);
        if (fd == -1) {
            /* Throw an exception */
            LOGE("Cannot open port %d",baudrate);
            /* TODO: throw an exception */
            return NULL;
        }
    }

    LOGD("init native Configure device!");
    /* Configure device */
    {
        struct termios cfg;
        if (tcgetattr(fd, &cfg)) {
            LOGE("Configure device tcgetattr() failed 1");
            close(fd);
            return NULL;
        }

        cfmakeraw(&cfg);
        cfsetispeed(&cfg, speed);
        cfsetospeed(&cfg, speed);

        if (tcsetattr(fd, TCSANOW, &cfg)) {
            LOGE("Configure device tcsetattr() failed 2");
            close(fd);
            /* TODO: throw an exception */
            return NULL;
        }
    }

    /* Create a corresponding file descriptor */
    {
        jclass cFileDescriptor = env->FindClass("java/io/FileDescriptor");
        jmethodID iFileDescriptor = env->GetMethodID(cFileDescriptor,"<init>", "()V");
        jfieldID descriptorID = env->GetFieldID(cFileDescriptor,"descriptor", "I");
        mFileDescriptor = env->NewObject(cFileDescriptor,iFileDescriptor);
        env->SetIntField(mFileDescriptor, descriptorID, (jint) fd);
    }

    return mFileDescriptor;
}

//關閉串口程序
  extern "C"
JNIEXPORT jint JNICALL
Java_com_serialportdemo_SerialPort_close(JNIEnv * env, jobject thiz)
{
    jclass SerialPortClass = env->GetObjectClass(thiz);
    jclass FileDescriptorClass = env->FindClass("java/io/FileDescriptor");

    jfieldID mFdID = env->GetFieldID(SerialPortClass, "mFd", "Ljava/io/FileDescriptor;");
    jfieldID descriptorID = env->GetFieldID(FileDescriptorClass, "descriptor", "I");

    jobject mFd = env->GetObjectField(thiz, mFdID);
    jint descriptor = env->GetIntField(mFd, descriptorID);

    LOGD("close(fd = %d)", descriptor);
    close(descriptor);
    return 1;
}

  android 方法就簡單多了,首先來看串口操做類,在這個類中打開串口,測試沒有作關閉串口的操做,jni的open方法,返回一個java.io.FileDescriptor對像,串口操做類經過該對像,獲取文件的讀寫流操做對像.app

//加載so文件
 static {
        System.loadLibrary("native-lib");
    }

/**
     * @param  path 串口文件路徑
     * @param baudrate 波特率,不一樣設備波特率有區別
     * */
    public SerialPort(String path, int baudrate) throws SecurityException, IOException {
        File device = new File(path);
        Logger.d(serialPortMsg());
        if(!device.canRead() || !device.canWrite()) {
            try {
                Process su = Runtime.getRuntime().exec("/system/bin/su");
                String cmd = "chmod 777 " + device.getAbsolutePath() + "\n"
                        + "exit\n";
                su.getOutputStream().write(cmd.getBytes());
                if ((su.waitFor() != 0) || !device.canRead()
                        || !device.canWrite()) {
                    throw new SecurityException();
                } 

            } catch (Exception e) {
                e.getMessage();
            }

        }

        mFd = open(device.getAbsolutePath(), baudrate);
        Logger.d(TAG+"open commplete");
        if (mFd == null) {
            Logger.e(TAG, "native open returns null");
            throw new IOException();
        }

        mFileInputStream = new FileInputStream(mFd);
        mFileOutputStream = new FileOutputStream(mFd);
    }


//定義本地方法
public native FileDescriptor open(String path, int baudrate);
public native void close();

接下來須要定義一個讀取串口信息的線程,用於獲取串口發送給android的信息ide

class ReadSerialPortMsgThread implements  Runnable{
        @Override
        public void run() {
            int size;
            byte buff[] = new byte[1024];
           final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            while (true){
               try {
                   if(mInputStream==null){
                       return;
                   }
                   size = mInputStream.read(buff);
                   if(size<=0){
                       continue;
                   }
                   final String message = new String(buff,0,size);
                   Logger.d(TAG+"接收到串口回調  "+message);
                   seriapPortMsg.append(message);
                   if(buff[size - 1] == '\n'){
                       log.post(new Runnable() {
                           @Override
                           public void run() {
                               log.setText(sdf.format(new Date())+"接收到串口發送的指令  "+message);
                           }
                       });
                   }
               }catch (Exception e){
                   e.printStackTrace();
               }finally {
                   try {
                       Thread.sleep(1000);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
            }
        }
    }

以上代碼完成了對串口的讀操做,串口寫操做比較簡單,就是獲得串口的OutputStream,而後調用writer方法便可,代碼以下:post

  

 @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.sendMsg:
                String msg = serMsg.getText().toString()+"\r\n";
                if(msg!=null&&!msg.equals("")){
                    byte [] buff = msg.getBytes();
                    try {
                        mOutputStream.write(buff,0,buff.length);
                        Logger.d(TAG+"msg 輸出完成");
                    } catch (IOException e) {
                        e.printStackTrace();
                        Logger.e(TAG+e.getMessage());
                    }
                }
        }
    }

到此爲止,讀寫操做的代碼所有完成,個人測試串口設備一直在向android發送信息,以下圖

三:注意事項

   String SERIALPORT_NO3 = "/dev/ttyS3",int BAUDRATE=115200;  這是我設備定義的串口文件路徑和波特率,這個信息位置須要根據實際狀況做修改。

   

完整demo代碼:https://github.com/jlq023/serialport

相關文章
相關標籤/搜索