| | |
| | | |
| | | #include <jni.h> |
| | | #include <string> |
| | | |
| | | #include <mutex> |
| | | #include <sys/stat.h> |
| | | #include <stdbool.h> |
| | | #include <stdint.h> |
| | |
| | | #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; |
| | | |
| | |
| | | /********************************************************************* |
| | | * 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, |
| | |
| | | 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*/); |
| | |
| | | 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; |
| | |
| | | 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); |
| | | |
| | |
| | | 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; |
| | | } |