-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuart.cpp
More file actions
280 lines (216 loc) · 8.28 KB
/
uart.cpp
File metadata and controls
280 lines (216 loc) · 8.28 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/************************************/
/* @auteur Mathieu Bahin */
/* @date_création mars 2020 */
/* @version 1.0 */
/* @email bahin.mathieu@gmail.com */
/************************************/
#include "uart.h"
#include <stdio.h>
#include <unistd.h> // Used for UART
#include <sys/fcntl.h> // Used for UART
#include <termios.h> // Used for UART
#include <string>
using namespace std;
Uart :: Uart (){
int ii, jj, kk;
// SETUP SERIAL WORLD
struct termios port_options; // Create the structure
tcgetattr(fid, &port_options); // Get the current attributes of the Serial port
//------------------------------------------------
// OPEN THE UART
//------------------------------------------------
// The flags (defined in fcntl.h):
// Access modes (use 1 of these):
// O_RDONLY - Open for reading only.
// O_RDWR - Open for reading and writing.
// O_WRONLY - Open for writing only.
// O_NDELAY / O_NONBLOCK (same function)
// - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
// if there is no input immediately available (instead of blocking). Likewise, write requests can also return
// immediately with a failure status if the output can't be written immediately.
// Caution: VMIN and VTIME flags are ignored if O_NONBLOCK flag is set.
// O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.fid = open("/dev/ttyTHS1", O_RDWR | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode
fid = open(uart_target, O_RDWR | O_NOCTTY );
tcflush(fid, TCIFLUSH);
tcflush(fid, TCIOFLUSH);
usleep(1000000); // 1 sec delay
if (fid == -1)
{
printf("**Error - Unable to open UART**. \n=>Ensure it is not in use by another application\n=>Ensure proper privilages are granted to accsess /dev/.. by run as a sudo\n");
}
//------------------------------------------------
// CONFIGURE THE UART
//------------------------------------------------
// flags defined in /usr/include/termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html
// Baud rate:
// - B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200,
// B230400, B460800, B500000, B576000, B921600, B1000000, B1152000,
// B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
// CSIZE: - CS5, CS6, CS7, CS8
// CLOCAL - Ignore modem status lines
// CREAD - Enable receiver
// IGNPAR = Ignore characters with parity errors
// ICRNL - Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters - don't use for bianry comms!)
// PARENB - Parity enable
// PARODD - Odd parity (else even)
port_options.c_cflag &= ~PARENB; // Disables the Parity Enable bit(PARENB),So No Parity
port_options.c_cflag &= ~CSTOPB; // CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit
port_options.c_cflag &= ~CSIZE; // Clears the mask for setting the data size
port_options.c_cflag |= CS8; // Set the data bits = 8
port_options.c_cflag &= ~CRTSCTS; // No Hardware flow Control
port_options.c_cflag |= CREAD | CLOCAL; // Enable receiver,Ignore Modem Control lines
port_options.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable XON/XOFF flow control both input & output
port_options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Non Cannonical mode
port_options.c_oflag &= ~OPOST; // No Output Processing
port_options.c_lflag = 0; // enable raw input instead of canonical,
port_options.c_cc[VMIN] = VMINX; // Read at least 1 character
port_options.c_cc[VTIME] = 0; // Wait indefinetly
cfsetispeed(&port_options,BAUDRATE); // Set Read Speed
cfsetospeed(&port_options,BAUDRATE); // Set Write Speed
// Set the attributes to the termios structure
int att = tcsetattr(fid, TCSANOW, &port_options);
if (att != 0 )
{
printf("\nERROR in Setting port attributes");
}
else
{
printf("\nSERIAL Port Good to Go.\n");
}
// Flush Buffers
tcflush(fid, TCIFLUSH);
tcflush(fid, TCIOFLUSH);
usleep(500000); // 0.5 sec delay
}
void Uart :: sendUart(unsigned char *msg){
//--------------------------------------------------------------
// TRANSMITTING BYTES
//--------------------------------------------------------------
unsigned char tx_buffer[20];
unsigned char *p_tx_buffer;
p_tx_buffer = &tx_buffer[0];
// so that i have the number of bytes to write
// by doing p_tx - tx
for (int i = 0; i < 20; i++) {
*p_tx_buffer++ = msg[i];
}
//printf("%x%x%x%x%x\n",p_tx_buffer[0],p_tx_buffer[1],p_tx_buffer[2],p_tx_buffer[3],p_tx_buffer[4]);
printf("fid 1=%d\n", fid );
if (fid != -1)
{
int count = write(fid, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
usleep(1000); // .001 sec delay
printf("Count = %d\n", count);
if (count < 0) printf("UART TX error\n");
}
usleep(1000000); // 1 sec delay
}
bool Uart :: sendUart_fb(unsigned char *msg){
//--------------------------------------------------------------
// TRANSMITTING BYTES WITH LOGICAL FEED BACK
//--------------------------------------------------------------
unsigned char tx_buffer[20];
unsigned char *p_tx_buffer;
p_tx_buffer = &tx_buffer[0];
// so that i have the number of bytes to write
// by doing p_tx - tx
for (int i = 0; i < 20; i++) {
*p_tx_buffer++ = msg[i];
}
//printf("%x%x%x%x%x\n",p_tx_buffer[0],p_tx_buffer[1],p_tx_buffer[2],p_tx_buffer[3],p_tx_buffer[4]);
printf("fid 1=%d\n", fid );
if (fid != -1)
{
int count = write(fid, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
usleep(1000); // .001 sec delay
printf("Count = %d\n", count);
if (count < 0)
{
printf("UART TX error\n");
return false;
}
return true;
}
else
{
return false;
}
usleep(1000000); // 1 sec delay
}
void Uart :: readUart(){
//--------------------------------------------------------------
// RECEIVING BYTES - AND BUILD MESSAGE RECEIVED
//--------------------------------------------------------------
unsigned char rx_buffer[VMINX];
bool pickup = true;
int ii;
int rx_length;
int nread = 0;
tcflush(fid, TCIOFLUSH);
usleep(1000); // .001 sec delay
printf("Ready to receive message.\n");
for (ii=0; ii<NSERIAL_CHAR; ii++) serial_message[ii]=' ';
while (pickup && fid != -1)
{
nread++;
rx_length = read(fid, (void*)rx_buffer, VMINX); // Filestream, buffer to store in, number of bytes to read (max)
printf("Event %d, rx_length=%d, Read=%s\n", nread, rx_length, rx_buffer );
if (rx_length < 0)
{
//An error occured (will occur if there are no bytes)
}
if (rx_length == 0)
{
//No data waiting
}
if (rx_length>=0)
{
if (nread<=NSERIAL_CHAR){
serial_message[nread-1] = rx_buffer[0]; // Build message 1 character at a time
printf("%x ",serial_message[nread-1]);
}
if (rx_buffer[0]=='#') pickup=false; // # symbol is terminator
}
}
printf("\nMessage Received:");
}
void Uart :: closeUart(){
//-------------------------------------------
// CLOSE THE SERIAL PORT
//-------------------------------------------
close(fid);
}
int main(int argc, char *argv[]) {
Uart u;
int i;
unsigned char m[256];
m[0]= (unsigned char) 1;
m[1]= (unsigned char) 0;
m[2]= (unsigned char) 0;
m[3]= (unsigned char) 0;
m[4]= (unsigned char) 1;
m[5]= (unsigned char) '#';
m[6]= (unsigned char) '\0';
while (1) {
u.sendUart(m);
printf("sent\n");
//feed back function test
if(u.sendUart_fb(m))
{
printf("data send with feed back\n");
}
else
{
printf("data failed to send with feedback\n");
}
// end of feed back function test
usleep(10000);
u.readUart();
while (u.serial_message[i]!='#') {
printf("%x ",u.serial_message[i]);
i++;
}
printf("\n");
}
return 0;
}