-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial.cpp
More file actions
58 lines (54 loc) · 1.37 KB
/
serial.cpp
File metadata and controls
58 lines (54 loc) · 1.37 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
// #include <curl/curl.h>
int fd, cfd;
void sigIntHandler(int sig)
{
fprintf(stdout, "CNT-C recieved.\n");
close(fd);
exit(0);
}
ssize_t write_to_string(void* ptr, size_t size, size_t count, void* stream)
{
printf("%s",((char*)ptr));
return size*count;
}
int main(int argc, char** argv)
{
if(argc!=2)
{
fprintf(stdout,"USAGE: %s [command string]\n", argv[0]);
}
signal(SIGINT, sigIntHandler);
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd==-1)
{
fprintf(stderr, "Could not open Arduino serial port.");
fflush(stderr);
exit(1);
}
fcntl(fd, F_SETFL, 0);
int writeBytes = write(fd, argv[1], strlen(argv[1]));
fprintf(stdout, "Sent %s %d bytes.\n", argv[1], writeBytes);
fprintf(stdout, "===================\n");
char b[1];
int i=0;
do {
int n = read(cfd, b, 1); // read a char at a time
if( n==-1) printf("Error reading response from card reader.\n"); // couldn't read
if( n==0 ) {
usleep( 10 * 1000 ); // wait 10 msec try again
continue;
}
fputc(b[0],stdout);
fflush(stdout);
i++;
} while( b[0] != '^' );
close(fd);
}