- Main
- Tutorials
- Library
- Samples

- File List
- Globals

serial.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 * Serial 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 <fcntl.h>
51 #include <sys/termios.h>
52 #include <iothinx/iothinxio.h>
53
54 #define SERIAL_BAUDRATE_300 B300
55 #define SERIAL_BAUDRATE_600 B600
56 #define SERIAL_BAUDRATE_1200 B1200
57 #define SERIAL_BAUDRATE_1800 B1800
58 #define SERIAL_BAUDRATE_2400 B2400
59 #define SERIAL_BAUDRATE_4800 B4800
60 #define SERIAL_BAUDRATE_9600 B9600
61 #define SERIAL_BAUDRATE_19200 B19200
62 #define SERIAL_BAUDRATE_38400 B38400
63 #define SERIAL_BAUDRATE_57600 B57600
64 #define SERIAL_BAUDRATE_115200 B115200
65
66 #define SERIAL_FLOW_CONTROL_NO
67 //#define SERIAL_FLOW_CONTROL_HW
68 //#define SERIAL_FLOW_CONTROL_SW
69 //#define SERIAL_FLOW_CONTROL_BOTH
70
71 #define SERIAL_PARITY_NONE
72 //#define SERIAL_PARITY_ODD
73 //#define SERIAL_PARITY_EVEN
74
75 //#define SERIAL_DATA_BITS_7
76 #define SERIAL_DATA_BITS_8
77
78 #define SERIAL_STOP_BIT_1
79 //#define SERIAL_STOP_BIT_2
80
82 int main(int argc, char **const argv)
83 {
84 #define BUF_LEN 256
85 #define NAME_LEN 16
86 struct termios tio;
87 int32_t rc;
88 char buf[BUF_LEN], devname[NAME_LEN];
89 int fd;
90 uint32_t interface = SERIAL_INTERFACE_RS232;
91 uint32_t port = SERIAL_PORT_1;
92 uint32_t slot = 0;
93 uint32_t module_count;
94
95 rc = ioThinx_Misc_GetModuleCountML(&module_count);
96
97 if (rc != IOTHINX_ERR_OK)
98 {
99 printf("Error:ioThinx_Serial_SetInterface() = %d\n", rc);
100 return -1;
101 }
102
103 printf("total slot = %lu\n", module_count);
104
105 while (-1 != (rc = getopt(argc, argv, "i:p:s:h")))
106 {
107 switch (rc)
108 {
109 case 'i':
110 interface = atoi(optarg);
111 break;
112 case 'p':
113 port = atoi(optarg);
114 break;
115 case 's':
116 slot = atoi(optarg);
117 break;
118 case 'h':
119 default:
120 printf("Serial sample program.\n\n");
121 printf("Usage: ./serial [OPTIONS]\n\n");
122 printf("Options:\n");
123 printf("\t%-8s Interface. Default interface = %d\n", "-i", interface);
124 printf("\t%-8s Port. Default port = %d\n", "-p", port);
125 printf("\t%-8s Slot. Default slot = %d\n", "-s", slot);
126 printf("\n");
127 return 0;
128 }
129 }
130
131 rc = ioThinx_Serial_SetInterface(slot, port, interface);
132
133 if (rc != IOTHINX_ERR_OK)
134 {
135 printf("Error:ioThinx_Serial_SetInterface() = %d\n", rc);
136 return -1;
137 }
138
139 rc = ioThinx_Serial_GetInterface(slot, port, &interface);
140
141 if (rc != IOTHINX_ERR_OK)
142 {
143 printf("Error:ioThinx_Serial_GetInterface() = %d\n", rc);
144 return -1;
145 }
146
147 printf("port = %lu\n", port);
148 printf("slot = %lu\n", slot);
149 printf("interface = %lu\n", interface);
150 rc = ioThinx_Serial_GetDevName(slot, port, devname);
151
152 if (rc != IOTHINX_ERR_OK)
153 {
154 printf("Error:ioThinx_Serial_GetDevName() = %d\n", rc);
155 return -1;
156 }
157
158 printf("devname = %s\n", devname);
159 fd = open(devname, O_RDWR);
160
161 if(fd < 0)
162 {
163 printf("Error:open = %d\n", rc);
164 return -1;
165 }
166
167 rc = tcgetattr(fd, &tio);
168
169 if(rc < 0)
170 {
171 printf("Error:tcgetattr = %d\n", rc);
172 return -1;
173 }
174
175 //baudrate
176 tio.c_cflag &= ~CBAUD;
177 tio.c_cflag |= SERIAL_BAUDRATE_115200;
178
179 //flow control
180 #if defined(SERIAL_FLOW_CONTROL_NO)
181 tio.c_cflag &= ~CRTSCTS;
182 tio.c_iflag &= ~(IXON | IXOFF);
183 #elif defined(SERIAL_FLOW_CONTROL_HW)
184 tio.c_cflag |= CRTSCTS;
185 tio.c_iflag &= ~(IXON | IXOFF);
186 #elif defined(SERIAL_FLOW_CONTROL_SW)
187 tio.c_cflag &= ~CRTSCTS;
188 tio.c_iflag |= (IXON | IXOFF);
189 #elif defined(SERIAL_FLOW_CONTROL_BOTH)
190 tio.c_cflag |= CRTSCTS;
191 tio.c_iflag |= (IXON | IXOFF);
192 #else
193 printf("No definition of flow control\n");
194 return -1;
195 #endif
196
197 //parity
198 #if defined(SERIAL_PARITY_NONE)
199 tio.c_cflag &= ~PARENB;
200 tio.c_iflag &= ~INPCK;
201 #elif defined(SERIAL_PARITY_ODD)
202 tio.c_cflag |= PARENB;
203 tio.c_cflag |= PARODD;
204 tio.c_iflag |= INPCK;
205 #elif defined(SERIAL_PARITY_EVEN)
206 tio.c_cflag |= PARENB;
207 tio.c_cflag &= ~PARODD;
208 tio.c_iflag |= INPCK;
209 #else
210 printf("No definition of parity\n");
211 return -1;
212 #endif
213
214 //data bits
215 #if defined(SERIAL_DATA_BITS_7)
216 tio.c_cflag &= ~CSIZE;
217 tio.c_cflag |= CS7;
218 #elif defined(SERIAL_DATA_BITS_8)
219 tio.c_cflag &= ~CSIZE;
220 tio.c_cflag |= CS8;
221 #else
222 printf("No definition of data bits\n");
223 return -1;
224 #endif
225
226 //stop bit
227 #if defined(SERIAL_STOP_BIT_1)
228 tio.c_cflag &= ~CSTOPB;
229 #elif defined(SERIAL_STOP_BIT_2)
230 tio.c_cflag |= CSTOPB;
231 #else
232 printf("No definition of stop bit\n");
233 return -1;
234 #endif
235
236 rc = tcsetattr(fd, TCSANOW, &tio);
237
238 if(rc < 0)
239 {
240 printf("Error:tcsetattr = %d\n", rc);
241 return -1;
242 }
243
244 printf("Start Serial echo.\n");
245
246 while(1)
247 {
248 rc = read(fd, buf, BUF_LEN);
249
250 if(rc > 0)
251 {
252 rc = write(fd, buf, rc);
253 if (strncmp(buf, "quit", 4) == 0)
254 {
255 break;
256 }
257 }
258 }
259
260 printf("Stop Serial echo.\n");
261 tcflush(fd, TCIOFLUSH);
262 close(fd);
263 return 0;
264 }
main
int main(int argc, char **const argv)
Definition: serial.c:82
NAME_LEN
#define NAME_LEN
ioThinx_Misc_GetModuleCountML
IOTHINX_ERR ioThinx_Misc_GetModuleCountML(uint32_t *p_module_count)
IOTHINX_ERR_OK
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
SERIAL_PORT_1
#define SERIAL_PORT_1
Definition: iothinxio.h:1263
SERIAL_INTERFACE_RS232
#define SERIAL_INTERFACE_RS232
Definition: iothinxio.h:1272
ioThinx_Serial_SetInterface
IOTHINX_ERR ioThinx_Serial_SetInterface(uint32_t slot, uint32_t port, uint32_t interface)
BUF_LEN
#define BUF_LEN
ioThinx_Serial_GetInterface
IOTHINX_ERR ioThinx_Serial_GetInterface(uint32_t slot, uint32_t port, uint32_t *interface)
ioThinx_Serial_GetDevName
IOTHINX_ERR ioThinx_Serial_GetDevName(uint32_t slot, uint32_t port, uint8_t *name)
SERIAL_BAUDRATE_115200
#define SERIAL_BAUDRATE_115200
Definition: serial.c:64