forked from AkBKukU/gpstesting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp.bak
More file actions
105 lines (77 loc) · 2.96 KB
/
main.cpp.bak
File metadata and controls
105 lines (77 loc) · 2.96 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
#include <stdio.h>
#include <fcntl.h> /* File Control Definitions */
#include <termios.h>/* POSIX Terminal Control Definitions*/
#include <unistd.h> /* UNIX Standard Definitions */
#include <errno.h> /* ERROR Number Definitions */
#include <iostream>
#include <cstring>
int main(int argc, char** args )
{
int fd;
fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY);
if( fd == 1)
printf("\n Error! in Opening ttyUSB0\n");
else
printf("\n ttyUSB0 Opened Successfully\n");
struct termios SerialPortSettings;
tcgetattr(fd, &SerialPortSettings);
cfsetospeed(&SerialPortSettings,B9600);
SerialPortSettings.c_cflag |= PARENB; /*SET Parity Bit PARENB*/
SerialPortSettings.c_cflag &= ~CSTOPB; //Stop bits = 1
SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the Mask */
SerialPortSettings.c_cflag |= CS7; /* Set the data bits = 8 */
SerialPortSettings.c_cflag &= ~CRTSCTS; /* No Hardware flow Control */
SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines */
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); /* Disable XON/XOFF flow control both i/p and o/p */
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* Non Cannonical mode */
SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/
/* Setting Time outs */
SerialPortSettings.c_cc[VMIN] = 10; /* Read at least 10 characters */
SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly */
if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/
printf("\n ERROR ! in Setting attributes");
else
printf("\n BaudRate = 9600 \n StopBits = 1 \n Parity = none");
/*------------------------------- Read data from serial port -----------------------------*/
tcflush(fd, TCIFLUSH); /* Discards old data in the rx buffer */
int bytes_to_read = 2000;
char read_buffer[bytes_to_read]; /* Buffer to store the data received */
int bytes_read = 0; /* Number of bytes read by the read() system call */
int i = 0;
bytes_read = read(fd,&read_buffer,bytes_to_read); /* Read the data */
printf("\n\n Bytes Rxed -%d", bytes_read); /* Print the number of bytes read */
printf("\n\n ");
for(i=0;i<bytes_read;i++) /*printing only the received characters*/
printf("%c",read_buffer[i]);
printf("\n +----------------------------------+\n\n\n");
printf("Initiating Loop...");
while("false")
{
/*
std::memset(read_buffer,0,bytes_to_read);
bytes_read = 0;
bytes_read = read(fd,&read_buffer,bytes_to_read); // Read the data
for(i=0;i<bytes_read;i++)
printf("%c",read_buffer[i]);
*/
char buffer[100];
ssize_t length = read(fd, &buffer, sizeof(buffer));
if (length == -1)
{
printf("Error reading from serial port\n");
break;
}
else if (length == 0)
{
printf("No more data\n");
break;
}
else
{
buffer[length] = '\0';
printf("%s", buffer);
}
}
close(fd); /* Close the serial port */
return 0;
}