yy1717
2022-12-08 f7a18ec4494b9c5c9ef3fd440bbf68ffc6425e18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// Created by YY on 2017/9/5.
//
 
#ifndef JNICALLBACK_SERIAL_PORT_H
#define JNICALLBACK_SERIAL_PORT_H
 
#include <stdint.h>
#include <mutex>
 
struct serial_config {
    char name[64];
    int baud;
    int data_bit;
    char verify_bit;
    int stop_bit;
    int flow_ctrl;
};
 
class SerialPort {
private:
    int fd = -1;
    std::mutex mtx;
    struct serial_config cfg;
    int setRTS(int level);
    int SetSerialPort(int fd, int speed, int databits, char parity, int stopbits, int flowctrl);
    int OpenSerialPort(const char *name);
    void CloseSerialPort(int fd);
    void UninitSerialPort(void);
public:
    SerialPort(struct serial_config cfg);
    virtual ~SerialPort();
    int InitSerialPort(void);
    int WriteSerialPort(const void *buf, int len);
    int ReadSerialPort(uint8_t *out, uint16_t length);
};
 
typedef int (SerialPort::*pSerialPortClassFun)(const void *buf, int len);
 
#endif //JNICALLBACK_SERIAL_PORT_H