Macros | Functions
uart.c File Reference

UART Sample More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iothinx/iothinxio.h>

Go to the source code of this file.

Macros

#define BUF_LEN   4096
 

Functions

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

Detailed Description

UART Sample

Date
2019-01-22
Author
Wanhan Hsieh
Version
V1.0
Introduction:
This is UART sample code.
Example:
1. Using default: ./uart
2. Setting port, mode and baudrate: ./uart -p1 -m1 -b9600
Default:
Default baudrate = 115200
Default mode = 0
Default port = 0
Help:
moxa@Moxa:~$ sudo ./uart -h
UART sample program.

Usage: ./uart [OPTIONS]

Options:
        -b       UART baudrate. Default baudrate = 115200
        -m       UART mode. Default mode = 0
        -p       UART port. Default port = 0

Library:
UART APIs

Definition in file uart.c.

Macro Definition Documentation

#define BUF_LEN   4096

Definition at line 52 of file uart.c.

Function Documentation

int main ( int  argc,
char **const  argv 
)
/*******************************************************************************
* Copyright (C) 2019 Moxa Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* UART 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 <iothinx/iothinxio.h>
#define BUF_LEN 4096
int main(int argc, char **const argv)
{
int32_t rc;
char buf[BUF_LEN];
int fd;
uint32_t uart_slot = 0;
uint32_t uart_port = UART_PORT_1;
uint32_t uart_mode = UART_MODE_RS232;
uint32_t uart_baudrate = 115200;
while (-1 != (rc = getopt(argc, argv, "b:hm:p:")))
{
switch (rc)
{
case 'b':
uart_baudrate = atoi(optarg);
break;
case 'p':
uart_port = atoi(optarg);
break;
case 'm':
uart_mode = atoi(optarg);
break;
case 'h':
default:
printf("UART sample program.\n\n");
printf("Usage: ./uart [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s UART baudrate. Default baudrate = %d\n", "-b", uart_baudrate);
printf("\t%-8s UART mode. Default mode = %d\n", "-m", uart_mode);
printf("\t%-8s UART port. Default port = %d\n", "-p", uart_port);
printf("\n");
return 0;
}
}
printf("UART slot = %lu\n", uart_slot);
printf("UART port = %lu\n", uart_port);
printf("UART mode = %lu\n", uart_mode);
printf("UART baudrate = %lu\n", uart_baudrate);
rc = ioThinx_Uart_Open(uart_slot, uart_port, uart_mode, uart_baudrate, &fd);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Uart_Open() = %d\n", rc);
return -1;
}
printf("fd = %d\n", fd);
printf("Start UART 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 UART echo.\n");
close(fd);
return 0;
}

Definition at line 55 of file uart.c.

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 }
#define UART_MODE_RS232
Definition: iothinxio.h:1148
#define UART_PORT_1
Definition: iothinxio.h:1156
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
#define BUF_LEN
Definition: uart.c:52
IOTHINX_ERR ioThinx_Uart_Open(uint32_t slot, uint32_t port, uint32_t mode, uint32_t baudrate, int *fd)