-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
130 lines (111 loc) · 3.9 KB
/
main.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
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
#include "obd_pids.h"
#include <stdlib.h>
#include <string>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
string def = "/dev/ttyUSB0";
string filename, spf;
printf("\n------------------OBDLink SX Interface---------------\n");
printf("Enter the name of the serial port file or type default.\n");
cout << "Current default = " << def << '\n';
cin >> filename;
if(filename == "default"){
spf = def;
}
else {
spf = filename;
}
/* --------------------- Open the Serial Port ---------------------*/
int fd; //File Descriptor
//openPort(fd);
printf("\nOpening Serial Port......\n");
fd = open(spf.c_str(), O_RDWR | O_NOCTTY);
//Check for Errors
if (fd < 0) {
printf("Error %i from open: %s\n", errno, strerror(errno));
}
/* ------------------- Make new Termios Structure ---------------*/
struct termios tty;
memset(&tty, 0, sizeof tty);
if(tcgetattr(fd, &tty) != 0) {
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
}
/* ---------------------- Set Attributes of Termios Structure------*/
tty.c_cflag &= ~PARENB; //Disable Parity Bit
tty.c_cflag &= ~CSTOPB; // Clear Stop Field
tty.c_cflag |= CS8; // 8 bits per byte
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
tty.c_cflag &= ~CSIZE; // Clears the mask for setting the data size
tty.c_cflag &= ~HUPCL;
tty.c_lflag |= ECHOE;
tty.c_lflag &= ~(ICANON | ECHO | ISIG); // Disable Canonical Mode
tty.c_iflag |= IXON;
tty.c_iflag &= ~( IXOFF | IXANY);
tty.c_iflag |= IGNPAR;
tty.c_iflag |= BRKINT;
tty.c_iflag &= ICRNL;
tty.c_oflag &= ~OPOST;
tty.c_oflag &= ~ONLCR;
tty.c_cc[VTIME] = 20;
tty.c_cc[VMIN] = 0;
cfsetospeed(&tty, B115200);
// CHECK FOR ERRORS IN THE TERMIOS STRUCT CONFIGURATIONS
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
}
cout << "\n\n\tSERIAL PORT CONFIGURED..\n";
/*-------------------- MAIN MENU LOOP -----------------------------*/
char iter = 'N';
char* COMMAND;
int ans, delay;
int ans2 = 0;
while(iter == 'N' || iter == 'n'){
system("clear");
cout << "SELECT A COMMAND: \n\n";
cout << "1.)\tGET SPEED\n";
cout << "2.)\tGET RPM\n";
cout << "3.)\tGET DEVICE INFO\n";
cin >> ans;
do{
if(ans == 1){
COMMAND = PID_SPEED;
cout << "\nThis is the command you sent = " << COMMAND;
}
else if(ans == 2){
COMMAND = PID_RPM;
cout << "\nThis is the command you sent = " << COMMAND;
}
else if(ans == 3){
COMMAND = DEVICE_INFO;
cout << "\nThis is the command you sent = " << COMMAND;
}
else{
cout << ("You didn't press the right thing man.");
}
} while (ans2 == 1);
//FLUSH THE RX TERMINAL
tcflush(fd, TCIFLUSH);
//WRITE TO THE SERIAL PORT
write(fd, COMMAND, sizeof(COMMAND));
if (delay){
long int sleep = delay * 1000000L;
usleep(sleep); //sleep time in the united states
}
// ASSIGN MEMORY AND RECORD TO THE BUFFER
char answer[256];
memset(&answer, '\0', sizeof(answer));
//READ FROM SERIAL PORT AND RECORD # OF BYTES
int bytes_read = read(fd, &answer, sizeof(answer));
printf(" read buffer = %s\n bytes read = %d\n", answer, bytes_read);
printf(" Would You Like to Exit the Program? (Y/N)");
cin >> iter;
}
}