uart app示例

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

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

static int (void)
{
int fd = 0;


fd = open("/dev/ttymxc2", O_RDWR | O_NONBLOCK);
if (-1 == fd) {
printf("Can't Open /dev/ttymxc1n");
return -1;
}

// some check
if (fcntl(fd, F_SETFL, 0) < 0)
printf("fcntl failed!n");

if (isatty(STDIN_FILENO) == 0)
printf("standard input is not a terminal devicen");

return fd;
}
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
static int config_uart(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;

// clear
if (tcgetattr(fd, &oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;

// bit
switch (nBits) {
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}

// check
switch(nEvent) {
case 'O': // 奇校验
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= INPCK;
break;
case 'E': // 偶校验
newtio.c_iflag |= INPCK;
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N': // 无校验
newtio.c_cflag &= ~PARENB;
break;
}

// baud rate
switch (nSpeed) {
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 230400:
cfsetispeed(&newtio, B230400);
cfsetospeed(&newtio, B230400);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}

// stop bit
if (nStop == 1)
newtio.c_cflag &= ~CSTOPB;
else if (nStop == 2)
newtio.c_cflag |= CSTOPB;

// set
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if ((tcsetattr(fd,TCSANOW,&newtio)) != 0) {
perror("com set error");
return -1;
}

return 0;
}

void main(void)
{
int g_uart_fd = 0;

g_uart_fd = open_uart();
config_uart(g_uart_fd, 115200, 8, 'E', 1);

uint8_t send_buf[10] = {0xff};
while (1) {
write(g_uart_fd, send_buf, 10);
usleep(5*1000);
}

close(g_uart_fd);
}