/
test_baud.c
104 lines (80 loc) · 2.12 KB
/
test_baud.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
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
/*
* Allows to set arbitrary speed for the serial device on Linux.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h> /* Error number definitions */
#include "/usr/include/asm-generic/termbits.h"
#include "/usr/include/asm-generic/ioctls.h"
// #include <termios.h> /* POSIX terminal control definitions */
int write_port(int fd, unsigned char *buf, size_t len)
{
int num;
num = write(fd, buf, len);
if (num < 0)
printf("write failed! (%s)\n", strerror(errno));
return num;
}
int read_port(int fd, unsigned char *buf, size_t len, struct timeval *tout)
{
fd_set inputs;
int num, ret;
num = 0 ;
FD_ZERO(&inputs);
FD_SET(fd, &inputs);
ret = select(fd+1, &inputs, (fd_set *)NULL, (fd_set *)NULL, tout);
//printf("select = %d\n", ret);
if (ret < 0) {
perror("select error!!");
return ret;
}
if (ret > 0) {
if (FD_ISSET(fd, &inputs)) {
num = read(fd, buf, len);
}
}
return num;
}
int main(int argc, char* argv[])
{
int fd, speed;
struct timeval tout;
unsigned char send_ch, recv_ch;
if (argc != 2) {
printf("%s device speed\n\nSet speed for a serial device.\nFor instance:\n %s /dev/ttyUSB0 250000\n", argv[0], argv[0]);
return -1;
}
fd = open(argv[1], O_RDWR);
speed = atoi(argv[2]);
struct termios2 tio;
ioctl(fd, TCGETS2, &tio);
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= BOTHER;
tio.c_ispeed = speed;
tio.c_ospeed = speed;
int r = ioctl(fd, TCSETS2, &tio);
// close(fd);
if (r == 0) {
printf("Changed successfully.\n");
} else {
perror("ioctl");
}
// Test code
sleep(2);
printf("Test start\n");
send_ch = '0';
for (;;){
write_port(fd, &send_ch, 1);
send_ch++;
if (send_ch > 0x7f) send_ch = '0';
tout.tv_sec = 1;
tout.tv_usec = 100*1000;
read_port(fd, &recv_ch, 1, &tout);
if (recv_ch == 'X') break;
sleep(1);
}
return 0;
}