commit | author | age
|
1e563e
|
1 |
/********************************************************************************* |
G |
2 |
* Copyright: (C) 2024 LingYun IoT System Studio |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: ttyS_test.c |
|
6 |
* Description: This file is comport loop back test program |
|
7 |
* |
|
8 |
* Version: 1.0.0(05/24/2024) |
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
10 |
* ChangeLog: 1, Release initial version on "05/24/2024 07:38:43 PM" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
|
|
14 |
#include <stdio.h> |
|
15 |
#include <stdlib.h> |
|
16 |
#include <string.h> |
|
17 |
#include <unistd.h> |
|
18 |
#include <fcntl.h> |
|
19 |
#include <errno.h> |
|
20 |
#include <termios.h> |
|
21 |
#include <pthread.h> |
|
22 |
|
|
23 |
#define DEV_NAME "/dev/ttymxc1" |
|
24 |
#define MSG "Hello, LingYun IoT System Studio!" |
|
25 |
|
|
26 |
int main(int argc, char **argv) |
|
27 |
{ |
|
28 |
struct termios tty; |
|
29 |
int serial_fd; |
|
30 |
int bytes; |
|
31 |
char read_buf[64]; |
|
32 |
|
|
33 |
/*+-------------------------+ |
|
34 |
| Open Serial Port | |
|
35 |
+-------------------------+*/ |
|
36 |
serial_fd = open(DEV_NAME, O_RDWR ); |
|
37 |
if (serial_fd == -1) |
|
38 |
{ |
|
39 |
printf("Open '%s' failed: %s\n", DEV_NAME, strerror(errno)); |
|
40 |
return 0; |
|
41 |
} |
|
42 |
|
|
43 |
/*+-------------------------+ |
|
44 |
| Configure Serial Port | |
|
45 |
+-------------------------+*/ |
|
46 |
|
|
47 |
tcgetattr(serial_fd, &tty);// read current serial port settings |
|
48 |
|
|
49 |
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common) |
|
50 |
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common) |
|
51 |
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size |
|
52 |
tty.c_cflag |= CS8; // 8 bits per byte (most common) |
|
53 |
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common) |
|
54 |
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1) |
|
55 |
|
|
56 |
tty.c_lflag &= ~ICANON; |
|
57 |
tty.c_lflag &= ~ECHO; // Disable echo |
|
58 |
tty.c_lflag &= ~ECHOE; // Disable erasure |
|
59 |
tty.c_lflag &= ~ECHONL; // Disable new-line echo |
|
60 |
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP |
|
61 |
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl |
|
62 |
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes |
|
63 |
|
|
64 |
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars) |
|
65 |
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed |
|
66 |
|
|
67 |
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received. |
|
68 |
tty.c_cc[VMIN] = 0; |
|
69 |
|
|
70 |
/* Set in/out baud rate to be 115200 */ |
|
71 |
cfsetispeed(&tty, B115200); |
|
72 |
cfsetospeed(&tty, B115200); |
|
73 |
|
|
74 |
tcsetattr(serial_fd, TCSANOW, &tty); |
|
75 |
|
|
76 |
/*+-------------------------+ |
|
77 |
| Write to Serial Port | |
|
78 |
+-------------------------+*/ |
|
79 |
write(serial_fd, MSG, strlen(MSG)); |
|
80 |
printf("Send MSG : %s\n", MSG); |
|
81 |
|
|
82 |
/*+-------------------------+ |
|
83 |
| Read from Serial Port | |
|
84 |
+-------------------------+*/ |
|
85 |
/* |
|
86 |
* The behaviour of read() (e.g. does it block?, how long does it block for?) |
|
87 |
* depends on the configuration settings above, specifically VMIN and VTIME |
|
88 |
*/ |
|
89 |
memset(&read_buf, '\0', sizeof(read_buf)); |
|
90 |
bytes = read(serial_fd, &read_buf, sizeof(read_buf)); |
|
91 |
if (bytes< 0) |
|
92 |
{ |
|
93 |
printf("Error reading: %s", strerror(errno)); |
|
94 |
goto cleanup; |
|
95 |
} |
|
96 |
|
|
97 |
printf("Received MSG: %s\n", read_buf); |
|
98 |
|
|
99 |
/*+-------------------------+ |
|
100 |
| close Serial Port | |
|
101 |
+-------------------------+*/ |
|
102 |
|
|
103 |
cleanup: |
|
104 |
close(serial_fd); |
|
105 |
return 0; // success |
|
106 |
} |