From f7a18ec4494b9c5c9ef3fd440bbf68ffc6425e18 Mon Sep 17 00:00:00 2001 From: yy1717 <fctom1215@outlook.com> Date: 星期四, 08 十二月 2022 15:40:56 +0800 Subject: [PATCH] 智慧驾培首次提交 --- lib/src/main/cpp/common/serial_port.cpp | 160 +++++++++++++++++++--------------------------------- 1 files changed, 59 insertions(+), 101 deletions(-) diff --git a/lib/src/main/cpp/common/serial_port.cpp b/lib/src/main/cpp/common/serial_port.cpp index a41832b..a7c36a0 100644 --- a/lib/src/main/cpp/common/serial_port.cpp +++ b/lib/src/main/cpp/common/serial_port.cpp @@ -4,7 +4,7 @@ #include <jni.h> #include <string> - +#include <mutex> #include <sys/stat.h> #include <stdbool.h> #include <stdint.h> @@ -15,16 +15,22 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> -#include <pthread.h> #include "../jni_log.h" #include "serial_port.h" using namespace std; -static volatile int serial_port_fd[2] = {0, 0}; -static pthread_mutex_t mutex[2] = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER}; +SerialPort::SerialPort(struct serial_config cfg) +{ + this->cfg = cfg; +} -int setRTS(int fd, int level) +SerialPort::~SerialPort() +{ + UninitSerialPort(); +} + +int SerialPort::setRTS(int level) { int status; @@ -47,7 +53,7 @@ /********************************************************************* * PUBLIC FUNCTIONS */ -static int SetSerialPort(int fd, int speed, int databits, char parity, int stopbits, int flowctrl) { +int SerialPort::SetSerialPort(int fd, int speed, int databits, char parity, int stopbits, int flowctrl) { int status = 0; struct termios opt; int speed_arr[] = {B921600, B576000, B500000, B460800, B230400, B115200, B38400, B19200, @@ -142,7 +148,7 @@ return status; } -static int OpenSerialPort(const char *name) { +int SerialPort::OpenSerialPort(const char *name) { int uart_fd; // uart_fd = open(name, O_RDWR /*| O_NONBLOCK/*| O_NOCTTY | O_NDELAY*/); @@ -151,14 +157,13 @@ return uart_fd; } -static void CloseSerialPort(int fd) { +void SerialPort::CloseSerialPort(int fd) { close(fd); } -int WriteSerialPort(int id, const void *buf, int len) { +int SerialPort::WriteSerialPort(const void *buf, int len) { int ret = -1; int fds_ret; - int fd = GetSerialPort(id); struct timeval tv; fd_set wrfds; @@ -173,7 +178,7 @@ FD_ZERO(&wrfds); //clean FD_SET(fd, &wrfds); //set - pthread_mutex_lock(&mutex[id]); + lock_guard<std::mutex> lock(mtx); fds_ret = select(fd + 1, NULL, &wrfds, NULL, &tv); @@ -194,113 +199,66 @@ LOGE("Serial Port error 2\n"); } - pthread_mutex_unlock(&mutex[id]); - return ret; } -int GetSerialPort(int id) { - return serial_port_fd[id]; -} - -int InitSerialPort(int id, int baud, int dataBits, char parity, int stopBits, int flowctrl) +int SerialPort::InitSerialPort(void) { - char name[32]; +// char name[32]; +// +// if (id == UART_0) { +// strcpy(name, "/dev/ttyCH341USB5"); +// } else if (id == UART_1) { +// strcpy(name, "/dev/ttyCH341USB6"); +// } else { +// return -1; +// } - if (id == UART_0) { - strcpy(name, "/dev/ttyHSL0"); - } else if (id == UART_1) { - strcpy(name, "/dev/ttyHSL1"); - } else { + fd = OpenSerialPort(cfg.name); + if (fd <= 0) { return -1; } - UninitSerialPort(id); - - serial_port_fd[id] = OpenSerialPort(name); - if (serial_port_fd[id] <= 0) { - return -1; - } - - if (SetSerialPort(serial_port_fd[id], baud, dataBits, parity, stopBits, flowctrl) != 0) { + if (SetSerialPort(fd, cfg.baud, cfg.data_bit, cfg.verify_bit, cfg.stop_bit, cfg.flow_ctrl) != 0) { return -2; } - pthread_mutex_init(&mutex[id], NULL); return 0; } -void UninitSerialPort(int id) +void SerialPort::UninitSerialPort(void) { - if (serial_port_fd[id] > 0) { - CloseSerialPort(serial_port_fd[id]); - pthread_mutex_destroy(&mutex[id]); - serial_port_fd[id] = 0; + if (fd > 0) { + CloseSerialPort(fd); + fd = 0; } } -int ReadSerialPort(int id, uint8_t *out, uint16_t length) +int SerialPort::ReadSerialPort(uint8_t *out, uint16_t length) { - if (serial_port_fd[id] <= 0) return 0; - return read(serial_port_fd[id], out, length); -} + if (fd <= 0) return 0; -//extern "C" -//JNIEXPORT jint JNICALL -//Java_com_example_yy_jnicallback_MyService_InitSerialPort( -// JNIEnv *env, -// jobject /* this */, -// jstring name, -// jint baud, -// jint dataBits, -// jbyte parity, -// jint stopBits) { -// -// const char *s = env->GetStringUTFChars(name, 0); -// char item_value[128]; -// strcpy(item_value, s); -// env->ReleaseStringUTFChars(name, s); -// LOGD("serial port = %s", item_value); -// -// if (serial_port_fd > 0) { -// CloseSerialPort(serial_port_fd); -// pthread_mutex_destroy(&mutex); -// serial_port_fd = 0; -// } -// -// serial_port_fd = OpenSerialPort(item_value); -// if (serial_port_fd <= 0) { -// return -1; -// } -// -// if (SetSerialPort(serial_port_fd, baud, dataBits, parity, stopBits) != 0) { -// return -2; -// } -// pthread_mutex_init(&mutex, NULL); -// return 0; -//} -// -//extern "C" -//JNIEXPORT void JNICALL -//Java_com_example_yy_jnicallback_MyService_MonitSerialPort( -// JNIEnv *env, -// jobject /* this */) { -// if (serial_port_fd > 0) { -// uint8_t UartRxBuf[4096]; -// -///* uint8_t pkt[] = {0x7E, 0x80, 0x02, 0x00, 0x00, 0x26, 0x00, 0x00, 0x01, 0x38, 0x20, 0x20, -// 0x55, 0x45, 0x04, 0x4E, -// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x02, 0x61, 0x63, -// 0x0C, 0x06, 0xEA, 0x04, -// 0xFE, 0x00, 0x00, 0x01, 0x63, 0x00, 0xB4, 0x13, 0x03, 0x05, 0x18, 0x18, -// 0x52, 0x01, 0x04, 0x00, -// 0x00, 0x00, 0x00, 0x05, 0x02, 0x00, 0x00, 0x4D, 0x7E}; -// WriteSerialPort(serial_port_fd, pkt, sizeof(pkt));*/ -// -// int length = read(serial_port_fd, UartRxBuf, sizeof(UartRxBuf)); -// -// if (length > 0) { -// Parse(DATA_ACCESS_MCU, UartRxBuf, length); -// } -// } -//} + struct timeval tv; + fd_set wrfds; + + tv.tv_sec = 5; + tv.tv_usec = 0; + + FD_ZERO(&wrfds); //clean + FD_SET(fd, &wrfds); //set + + int fds_ret = select(fd + 1, &wrfds, NULL, NULL, &tv); + + if (fds_ret < 0) { + return -1; + } + else if(fds_ret == 0) { + // timeout + return -2; + } + else if(FD_ISSET(fd, &wrfds)) { + return read(fd, out, length); + } + + return -3; +} -- Gitblit v1.8.0