- Main
- Tutorials
- Library
- Samples

- File List
- Globals

Macros | Functions
serial.c File Reference

Serial Sample More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include <unistd.h>`
`#include <fcntl.h>`
`#include <sys/termios.h>`
`#include <iothinx/iothinxio.h>`

Go to the source code of this file.

## Macros
#define | SERIAL_BAUDRATE_300 B300
#define | SERIAL_BAUDRATE_600 B600
#define | SERIAL_BAUDRATE_1200 B1200
#define | SERIAL_BAUDRATE_1800 B1800
#define | SERIAL_BAUDRATE_2400 B2400
#define | SERIAL_BAUDRATE_4800 B4800
#define | SERIAL_BAUDRATE_9600 B9600
#define | SERIAL_BAUDRATE_19200 B19200
#define | SERIAL_BAUDRATE_38400 B38400
#define | SERIAL_BAUDRATE_57600 B57600
#define | SERIAL_BAUDRATE_115200 B115200
#define | SERIAL_FLOW_CONTROL_NO
#define | SERIAL_PARITY_NONE
#define | SERIAL_DATA_BITS_8
#define | SERIAL_STOP_BIT_1
#define | BUF_LEN 256
#define | NAME_LEN 16

## Functions
int | main (int argc, char **const argv)

## Detailed Description

Serial Sample

Copyright
  2019 Moxa Inc. All rights reserved.

Date
  2019-01-22

Author
  Wanhan Hsieh

Version
  V1.0

    Introduction:
    This is Serial sample code.
    Example:
    1. Using default: ./serial
    2. Setting slot, port and interface: ./serial -s0 -p0 -i0
    Default:
    Default interface = 0
    Default port = 0
    Default slot = 0
    Help:

    moxa@Moxa:~$ sudo ./serial -h
    Serial sample program.

    Usage: ./serial [OPTIONS]

    Options:
            -i       Interface. Default interface = 0
            -p       Port. Default port = 0
            -s       Slot. Default slot = 0

Library:
Serial APIs

Definition in file serial.c.

## Macro Definition Documentation

#define SERIAL_BAUDRATE_300 B300

Definition at line 54 of file serial.c.

#define SERIAL_BAUDRATE_600 B600

Definition at line 55 of file serial.c.

#define SERIAL_BAUDRATE_1200 B1200

Definition at line 56 of file serial.c.

#define SERIAL_BAUDRATE_1800 B1800

Definition at line 57 of file serial.c.

#define SERIAL_BAUDRATE_2400 B2400

Definition at line 58 of file serial.c.

#define SERIAL_BAUDRATE_4800 B4800

Definition at line 59 of file serial.c.

#define SERIAL_BAUDRATE_9600 B9600

Definition at line 60 of file serial.c.

#define SERIAL_BAUDRATE_19200 B19200

Definition at line 61 of file serial.c.

#define SERIAL_BAUDRATE_38400 B38400

Definition at line 62 of file serial.c.

#define SERIAL_BAUDRATE_57600 B57600

Definition at line 63 of file serial.c.

#define SERIAL_BAUDRATE_115200 B115200

Definition at line 64 of file serial.c.

#define SERIAL_FLOW_CONTROL_NO

Definition at line 66 of file serial.c.

#define SERIAL_PARITY_NONE

Definition at line 71 of file serial.c.

#define SERIAL_DATA_BITS_8

Definition at line 76 of file serial.c.

#define SERIAL_STOP_BIT_1

Definition at line 78 of file serial.c.

#define BUF_LEN 256

#define NAME_LEN 16

## Function Documentation

int main | ( | int | argc,
|  | char **const | argv
| ) |  |

/*******************************************************************************
* Copyright (C) 2019 Moxa Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Serial Sample Application
*
* Date Author Comment
* 2019-01-22 Wanhan Hsieh Created it.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/termios.h>
#include <iothinx/iothinxio.h>
#define SERIAL_BAUDRATE_300 B300
#define SERIAL_BAUDRATE_600 B600
#define SERIAL_BAUDRATE_1200 B1200
#define SERIAL_BAUDRATE_1800 B1800
#define SERIAL_BAUDRATE_2400 B2400
#define SERIAL_BAUDRATE_4800 B4800
#define SERIAL_BAUDRATE_9600 B9600
#define SERIAL_BAUDRATE_19200 B19200
#define SERIAL_BAUDRATE_38400 B38400
#define SERIAL_BAUDRATE_57600 B57600
#define SERIAL_BAUDRATE_115200 B115200
#define SERIAL_FLOW_CONTROL_NO
//#define SERIAL_FLOW_CONTROL_HW
//#define SERIAL_FLOW_CONTROL_SW
//#define SERIAL_FLOW_CONTROL_BOTH
#define SERIAL_PARITY_NONE
//#define SERIAL_PARITY_ODD
//#define SERIAL_PARITY_EVEN
//#define SERIAL_DATA_BITS_7
#define SERIAL_DATA_BITS_8
#define SERIAL_STOP_BIT_1
//#define SERIAL_STOP_BIT_2
int main(int argc, char **const argv)
{
#define BUF_LEN 256
#define NAME_LEN 16
struct termios tio;
int32_t rc;
char buf[BUF_LEN], devname[NAME_LEN];
int fd;
uint32_t interface = SERIAL_INTERFACE_RS232;
uint32_t port = SERIAL_PORT_1;
uint32_t slot = 0;
uint32_t module_count;
rc = ioThinx_Misc_GetModuleCountML(&module_count);
if (rc != IOTHINX_ERR_OK)
{
printf("Error:ioThinx_Serial_SetInterface() = %d\n", rc);
return -1;
}
printf("total slot = %lu\n", module_count);
while (-1 != (rc = getopt(argc, argv, "i:p:s:h")))
{
switch (rc)
{
case 'i':
interface = atoi(optarg);
break;
case 'p':
port = atoi(optarg);
break;
case 's':
slot = atoi(optarg);
break;
case 'h':
default:
printf("Serial sample program.\n\n");
printf("Usage: ./serial [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Interface. Default interface = %d\n", "-i", interface);
printf("\t%-8s Port. Default port = %d\n", "-p", port);
printf("\t%-8s Slot. Default slot = %d\n", "-s", slot);
printf("\n");
return 0;
}
}
rc = ioThinx_Serial_SetInterface(slot, port, interface);
if (rc != IOTHINX_ERR_OK)
{
printf("Error:ioThinx_Serial_SetInterface() = %d\n", rc);
return -1;
}
rc = ioThinx_Serial_GetInterface(slot, port, &interface);
if (rc != IOTHINX_ERR_OK)
{
printf("Error:ioThinx_Serial_GetInterface() = %d\n", rc);
return -1;
}
printf("port = %lu\n", port);
printf("slot = %lu\n", slot);
printf("interface = %lu\n", interface);
rc = ioThinx_Serial_GetDevName(slot, port, devname);
if (rc != IOTHINX_ERR_OK)
{
printf("Error:ioThinx_Serial_GetDevName() = %d\n", rc);
return -1;
}
printf("devname = %s\n", devname);
fd = open(devname, O_RDWR);
if(fd < 0)
{
printf("Error:open = %d\n", rc);
return -1;
}
rc = tcgetattr(fd, &tio);
if(rc < 0)
{
printf("Error:tcgetattr = %d\n", rc);
return -1;
}
//baudrate
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= SERIAL_BAUDRATE_115200;
//flow control
#if defined(SERIAL_FLOW_CONTROL_NO)
tio.c_cflag &= ~CRTSCTS;
tio.c_iflag &= ~(IXON | IXOFF);
#elif defined(SERIAL_FLOW_CONTROL_HW)
tio.c_cflag |= CRTSCTS;
tio.c_iflag &= ~(IXON | IXOFF);
#elif defined(SERIAL_FLOW_CONTROL_SW)
tio.c_cflag &= ~CRTSCTS;
tio.c_iflag |= (IXON | IXOFF);
#elif defined(SERIAL_FLOW_CONTROL_BOTH)
tio.c_cflag |= CRTSCTS;
tio.c_iflag |= (IXON | IXOFF);
#else
printf("No definition of flow control\n");
return -1;
#endif
//parity
#if defined(SERIAL_PARITY_NONE)
tio.c_cflag &= ~PARENB;
tio.c_iflag &= ~INPCK;
#elif defined(SERIAL_PARITY_ODD)
tio.c_cflag |= PARENB;
tio.c_cflag |= PARODD;
tio.c_iflag |= INPCK;
#elif defined(SERIAL_PARITY_EVEN)
tio.c_cflag |= PARENB;
tio.c_cflag &= ~PARODD;
tio.c_iflag |= INPCK;
#else
printf("No definition of parity\n");
return -1;
#endif
//data bits
#if defined(SERIAL_DATA_BITS_7)
tio.c_cflag &= ~CSIZE;
tio.c_cflag |= CS7;
#elif defined(SERIAL_DATA_BITS_8)
tio.c_cflag &= ~CSIZE;
tio.c_cflag |= CS8;
#else
printf("No definition of data bits\n");
return -1;
#endif
//stop bit
#if defined(SERIAL_STOP_BIT_1)
tio.c_cflag &= ~CSTOPB;
#elif defined(SERIAL_STOP_BIT_2)
tio.c_cflag |= CSTOPB;
#else
printf("No definition of stop bit\n");
return -1;
#endif
rc = tcsetattr(fd, TCSANOW, &tio);
if(rc < 0)
{
printf("Error:tcsetattr = %d\n", rc);
return -1;
}
printf("Start Serial echo.\n");
while(1)
{
rc = read(fd, buf, BUF_LEN);
if(rc > 0)
{
rc = write(fd, buf, rc);
if (strncmp(buf, "quit", 4) == 0)
{
break;
}
}
}
printf("Stop Serial echo.\n");
tcflush(fd, TCIOFLUSH);
close(fd);
return 0;
}

Definition at line 82 of file serial.c.

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 }
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