-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardconnect.c
58 lines (49 loc) · 1 KB
/
hardconnect.c
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 <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
int fd;
int uart_device_open()
{
struct termios options;
if((fd=open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY))<0)
{
perror("open failed");
return -1;
}
tcgetattr(fd, &options);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CSIZE;
options.c_cflag &= ~CRTSCTS;
options.c_cflag |= CS8;
options.c_cflag &= ~CSTOPB;
options.c_iflag |= IGNPAR;
options.c_iflag &= ~(BRKINT | INPCK | ISTRIP | ICRNL | IXON);
options.c_cc[VMIN] = 12;
options.c_oflag = 0;
options.c_lflag = 0;
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
tcsetattr(fd,TCSANOW,&options);
printf("serial ok!\n");
return 0;
}
int main(int argc, const char *argv[])
{
uart_device_open();
//char c='1';
char c;
while(1)
{
c=getchar();
printf("command is %c\n", c);
if (c=='a'||c=='b'||c=='c'||c=='e')
{
write(fd,&c,1);
}
}
close(fd);
return 0;
}