- Main
- Tutorials
- Library
- Samples

- File List
- Globals

uart.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2019 Moxa Inc. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * UART Sample Application
6 *
7 * Date Author Comment
8 * 2019-01-22 Wanhan Hsieh Created it.
9 ******************************************************************************/
10
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <iothinx/iothinxio.h>
51
52 #define BUF_LEN 4096
53
55 int main(int argc, char **const argv)
56 {
57 int32_t rc;
58 char buf[BUF_LEN];
59 int fd;
60 uint32_t uart_slot = 0;
61 uint32_t uart_port = UART_PORT_1;
62 uint32_t uart_mode = UART_MODE_RS232;
63 uint32_t uart_baudrate = 115200;
64
65 while (-1 != (rc = getopt(argc, argv, "b:hm:p:")))
66 {
67 switch (rc)
68 {
69 case 'b':
70 uart_baudrate = atoi(optarg);
71 break;
72 case 'p':
73 uart_port = atoi(optarg);
74 break;
75 case 'm':
76 uart_mode = atoi(optarg);
77 break;
78 case 'h':
79 default:
80 printf("UART sample program.\n\n");
81 printf("Usage: ./uart [OPTIONS]\n\n");
82 printf("Options:\n");
83 printf("\t%-8s UART baudrate. Default baudrate = %d\n", "-b", uart_baudrate);
84 printf("\t%-8s UART mode. Default mode = %d\n", "-m", uart_mode);
85 printf("\t%-8s UART port. Default port = %d\n", "-p", uart_port);
86 printf("\n");
87 return 0;
88 }
89 }
90
91 printf("UART slot = %lu\n", uart_slot);
92 printf("UART port = %lu\n", uart_port);
93 printf("UART mode = %lu\n", uart_mode);
94 printf("UART baudrate = %lu\n", uart_baudrate);
95
96 rc = ioThinx_Uart_Open(uart_slot, uart_port, uart_mode, uart_baudrate, &fd);
97 if (rc != IOTHINX_ERR_OK)
98 {
99 printf("ioThinx_Uart_Open() = %d\n", rc);
100 return -1;
101 }
102 printf("fd = %d\n", fd);
103 printf("Start UART echo.\n");
104 while(1)
105 {
106 rc = read(fd, buf, BUF_LEN);
107 if(rc > 0)
108 {
109 rc = write(fd, buf, rc);
110 if (strncmp(buf, "quit", 4) == 0)
111 {
112 break;
113 }
114 }
115 }
116 printf("Stop UART echo.\n");
117 close(fd);
118
119 return 0;
120 }
UART_MODE_RS232
#define UART_MODE_RS232
Definition: iothinxio.h:1148
UART_PORT_1
#define UART_PORT_1
Definition: iothinxio.h:1156
IOTHINX_ERR_OK
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
BUF_LEN
#define BUF_LEN
Definition: uart.c:52
main
int main(int argc, char **const argv)
Definition: uart.c:55
ioThinx_Uart_Open
IOTHINX_ERR ioThinx_Uart_Open(uint32_t slot, uint32_t port, uint32_t mode, uint32_t baudrate, int *fd)