lsiosr.h
Go to the documentation of this file.
1 /*******************************************************
2 @company: Copyright (C) 2018, Leishen Intelligent System
3 @product: serial
4 @filename: lsiosr.h
5 @brief:
6 @version: date: author: comments:
7 @v1.0 18-8-21 fu new
8 *******************************************************/
9 #ifndef LSIOSR_H
10 #define LSIOSR_H
11 
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <termios.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdint.h>
22 #include <fstream>
23 #include <iostream>
24 //波特率
25 #define BAUD_2400 2400
26 #define BAUD_4800 4800
27 #define BAUD_9600 9600
28 #define BAUD_57600 57600
29 #define BAUD_115200 115200
30 #define BAUD_230400 230400
31 #define BAUD_460800 460800
32 
33 //奇偶校验位
34 #define PARITY_ODD 'O' //奇数
35 #define PARITY_EVEN 'E' //偶数
36 #define PARITY_NONE 'N' //无奇偶校验位
37 //停止位
38 #define STOP_BIT_1 1
39 #define STOP_BIT_2 2
40 //数据位
41 #define DATA_BIT_7 7
42 #define DATA_BIT_8 8
43 
44 namespace ls
45 {
46 class LSIOSR{
47 public:
48  static LSIOSR* instance(std::string name, int speed, int fd = 0);
49 
50  ~LSIOSR();
51 
52  /* 从串口中读取数据 */
53  int read(char *buffer, int length, int timeout = 30);
54 
55  /* 向串口传数据 */
56  int send(const char* buffer, int length, int timeout = 30);
57 
58  /* 串口初始化 */
59  int init();
60 
61  int close();
62 
63  /* 获取串口号 */
64  std::string getPort();
65 
66  /* 设置串口号 */
67  int setPort(std::string name);
68 
69 private:
70  LSIOSR(std::string name, int speed, int fd);
71 
72  int waitWritable(int millis);
73  int waitReadable(int millis);
74 
75  /* 串口配置的函数 */
76  int setOpt(int nBits, uint8_t nEvent, int nStop);
77 
78  std::string port_;
79 
81 
82  int fd_;
83 };
84 }
85 #endif
86 
Definition: ls01b.h:17
std::string port_
Definition: lsiosr.h:78
int waitWritable(int millis)
Definition: lsiosr.cpp:250
int waitReadable(int millis)
Definition: lsiosr.cpp:201
int close()
Definition: lsiosr.cpp:402
int read(char *buffer, int length, int timeout=30)
Definition: lsiosr.cpp:127
static LSIOSR * instance(std::string name, int speed, int fd=0)
Definition: lsiosr.cpp:13
int setPort(std::string name)
Definition: lsiosr.cpp:412
int fd_
Definition: lsiosr.h:82
int init()
Definition: lsiosr.cpp:381
std::string getPort()
Definition: lsiosr.cpp:407
LSIOSR(std::string name, int speed, int fd)
Definition: lsiosr.cpp:19
int setOpt(int nBits, uint8_t nEvent, int nStop)
Definition: lsiosr.cpp:29
int baud_rate_
Definition: lsiosr.h:80
int send(const char *buffer, int length, int timeout=30)
Definition: lsiosr.cpp:299


ls01b_v2
Author(s): fu
autogenerated on Sat Sep 28 2019 03:51:19