2 Star 0 Fork 0

程序员锡哥/cgutech-jni-serial

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
com_cgutech_core_utils_SerialPortHandler.cpp 3.18 KB
一键复制 编辑 原始数据 按行查看 历史
devis 提交于 2019-07-10 10:11 . 调同红绿灯
//
// 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;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/msgy/cgutech-jni-serial.git
git@gitee.com:msgy/cgutech-jni-serial.git
msgy
cgutech-jni-serial
cgutech-jni-serial
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385