代码拉取完成,页面将自动刷新
//
// Created by root on 19-7-1.
//
#include <cstring>
#include "com_cgutech_core_utils_SerialPortHandler.h"
#include "SerialPort.h"
#include "SerialPortForBoost.h"
struct SerialPortHandler {
SerialPortForBoost serialPort;
jobject receiver;
};
/*
* Class: com_cgutech_core_utils_SerialPortHandler
* Method: Open
* Signature: (Ljava/lang/String;I)J
*/
JNIEXPORT jlong JNICALL Java_com_cgutech_core_utils_SerialPortHandler_Open
(JNIEnv *env, jclass cls, jstring name, jint baudrate) {
SerialPortHandler* portHandler = new SerialPortHandler();
memset(portHandler, 0, sizeof(SerialPortHandler));
jboolean isCopy = 0;
const char* strName = env->GetStringUTFChars(name, &isCopy);
printf("open serial:%s\n", strName);
char cName[128] = {0};
strcpy(cName, strName);
if(!portHandler->serialPort.Open(cName, baudrate)) {
delete portHandler;
printf("open serial failed:%s\n", strName);
env->ReleaseStringUTFChars(name, strName);
return 0;
}
env->ReleaseStringUTFChars(name, strName);
return (jlong)portHandler;
}
/*
* Class: com_cgutech_core_utils_SerialPortHandler
* Method: Write
* Signature: (J[B)Z
*/
JNIEXPORT jboolean JNICALL Java_com_cgutech_core_utils_SerialPortHandler_Write
(JNIEnv *env, jclass cls, jlong obj, jbyteArray data) {
if(obj == 0) {
return false;
}
SerialPortHandler* portHandler = (SerialPortHandler*)obj;
jboolean isCopy = 0;
int len = env->GetArrayLength(data);
if(len <= 0) {
return false;
}
jbyte *cData = env->GetByteArrayElements(data, & isCopy);
bool res = portHandler->serialPort.Write((unsigned char*)cData, len);
env->ReleaseByteArrayElements(data, cData, 0);
return res;
}
/*
* Class: com_cgutech_core_utils_SerialPortHandler
* Method: Read
* Signature: (J)[B
*/
JNIEXPORT jbyteArray JNICALL Java_com_cgutech_core_utils_SerialPortHandler_Read
(JNIEnv *env, jclass cls, jlong obj) {
if(obj == 0) {
return nullptr;
}
SerialPortHandler* portHandler = (SerialPortHandler*)obj;
unsigned char buffer[8] = {0};
int len = portHandler->serialPort.Read(buffer, sizeof(buffer));
if(len <= 0) {
return nullptr;
}
jbyteArray arr = env->NewByteArray(len);
env->SetByteArrayRegion(arr, 0, len, (const jbyte *)buffer);
return arr;
}
/*
* Class: com_cgutech_core_utils_SerialPortHandler
* Method: Close
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_cgutech_core_utils_SerialPortHandler_Close
(JNIEnv *env, jclass cls, jlong obj) {
if(obj == 0) {
return;
}
SerialPortHandler* portHandler = (SerialPortHandler*)obj;
portHandler->serialPort.Close();
delete portHandler;
}
/*
* Class: com_cgutech_core_utils_SerialPortHandler
* Method: SetDataReceiver
* Signature: (JLcom/cgutech/core/utils/SerialPortHandler/UartDataReceiver;)V
*/
JNIEXPORT void JNICALL Java_com_cgutech_core_utils_SerialPortHandler_SetDataReceiver
(JNIEnv *env, jclass cls, jlong obj, jobject receiver) {
if(obj == 0) {
return;
}
SerialPortHandler* portHandler = (SerialPortHandler*)obj;
portHandler->receiver = receiver;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。