-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.cpp
103 lines (74 loc) · 2.01 KB
/
uart.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//uart.cpp - uart interaction library
#include "uart.h"
Uart::Uart(int uart_no) {
uart = uart_no;
switch (uart) {
case 1:
tx_mux = string(MUX_DIR) + string(UART1_TX_MUX);
rx_mux = string(MUX_DIR) + string(UART1_RX_MUX);
ct_mux = string(MUX_DIR) + string(UART1_CT_MUX);
rt_mux = string(MUX_DIR) + string(UART1_RT_MUX);
tx_mode = UART1_MODE;
rx_mode = UART1_MODE;
ct_mode = UART1_MODE;
rt_mode = UART1_MODE;
file = UART1_FILE;
//default:
// cerr << "invalid uart no.: " << uart_no << '\n';
}
ofstream fd;
fd.open(tx_mux.c_str());
fd << tx_mode;
fd.close();
fd.open(rx_mux.c_str());
fd << rx_mode;
fd.close();
fd.open(ct_mux.c_str());
fd << ct_mode;
fd.close();
fd.open(rt_mux.c_str());
fd << rt_mode;
fd.close();
memset(&tio, 0, sizeof(tio));
tio.c_iflag = 0;
tio.c_oflag = 0;
tio.c_cflag = CS8|CREAD|CLOCAL;
tio.c_lflag = 0;
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 5;
com_fd = open(file.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
if (com_fd < 0) {
cerr << "Cannot open serial port\n";
exit(1);
}
cfsetospeed(&tio,B115200);
cfsetispeed(&tio,B115200);
tcsetattr(com_fd,TCSANOW,&tio);
fcntl(com_fd, F_SETFL, 0);
}
Uart::~Uart(void) {
close(com_fd);
}
bool Uart::transmit(char * message, int length) {
cout << "bd;iagjb\n";
int wr = write(com_fd, message, length);
cout << "adkmnvbdjkbgajki\n";
if (wr < 0) {
cerr << "Error transmitting to uart" << uart << '\n';
return false;
}
return true;
}
bool Uart::receive(char * message, int length) {
int rd = read(com_fd, message, length);
if (rd < 0) {
cerr << "Error receiving from uart" << uart << '\n';
return false;
}
return true;
}
int main() {
char buf[5] = {'h', 'e', 'l', 'l', 'o'};
Uart test = Uart(1);
test.transmit(buf, 5);
}