-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialPortConnection.cpp
167 lines (127 loc) · 3.76 KB
/
serialPortConnection.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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/time.h>
#define BAUDRATE B9600 // fix 900bps to connect to Arduino
#define MODEMDEVICE "/dev/cu.usbmodemfa131"
#define BUFFSIZE 256
#define COULDNOTFORK -1
#define FALSE 0
#define TRUE 1
volatile int STOP = FALSE;
static int fd = -1;
/* Functions */
void serial_init(int fd);
void child_process();
void parent_process(pid_t result_pid);
/* Main */
int main(void);
// initiate serial port
void serial_init(int fd) {
struct termios tio;
memset(&tio, 0, sizeof(tio));
tio.c_cflag = CS8 | CLOCAL | CREAD; // Baudrate: CS8: 8n1 (8 bits, nonparity, stop bit 1), CLOCAL: local connection, no modem control, CREAD: receiving characters on
tio.c_cc[VTIME] = 0; // don't use timer between characters
tio.c_lflag = ICANON; // Canonical input on
tio.c_iflag = IGNPAR | ICRNL; // IGNPAR: ignore parity error data, ICRNL: CR corresponds to NL
// Configuration for Boudrate
cfsetispeed(&tio, BAUDRATE);
cfsetospeed(&tio, BAUDRATE);
// Configuration for devices
tcsetattr(fd, TCSANOW, &tio);
}
void child_process() {
/*
char buf[BUFFSIZE];
char input[BUFFSIZE];
int count;
int i;
int writecount = 0;
fprintf(stdout, "CHILD:RCV Start\n");
// infinite loop until receiving STOP signal
while (FALSE == STOP) {
memset(&buf, 0, sizeof(buf));
count = read(fd, &buf, BUFFSIZE);
if (count < 0) {
fprintf(stdout, "CHILD:Could not read from serial port\n");
STOP = TRUE;
} else {
fprintf(stdout, "CHILD:RCVD CHAR %s %d", buf, count);
}
}
fprintf(stdout, "CHILD:BYE!\n");
*/
}
void parent_process(pid_t result_pid) {
char input[BUFFSIZE];
int writecount = 0;
fprintf(stdout, "Parent:Waiting for Input\n");
int i = 0;
int inputcount = 0;
int data = 0;
int j = 0;
while (1) {
//sprintf(input2, "%d", channel);
memset(&input, 13, sizeof(input));
//fgets(input, sizeof(input), stdin);
//fflush(stdin);
data++;
sprintf(input, "%d", data);
for (i = 0; i < BUFFSIZE; i++) {
if (input[i] == 0) {
inputcount = i;
input[i] = 13;
input[i + 1] = 10;
break;
}
}
writecount = write(fd, &input, inputcount);
if (writecount < 0) {
fprintf(stdout, "Could not write to serial port %d\n", writecount);
break;
} else {
fprintf(stdout, "Send %d bytes\n", writecount);
}
usleep(15*1000); // usec
if(j >= 1000 - 1)
{
j = 0;
data = 0;
}else{
j++;
}
}
}
int main(void) {
pid_t result_pid;
struct termios oldtio, newtio;
char buf[255];
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (fd < 0) {
perror(MODEMDEVICE);
return (-1);
}
tcgetattr(fd, &oldtio);
memset(&newtio, 0, sizeof(newtio));
// ready serial port
serial_init(fd);
//fork
result_pid = fork();
if (result_pid == -1) {
fprintf(stderr, "fork failed.\n");
return COULDNOTFORK;
}
if (result_pid == 0) {
child_process();
} else {
fprintf(stderr, "fork completed");
parent_process(result_pid);
}
STOP = TRUE;
return 0;
}