Functions
di.c File Reference

DI Sample More...

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

Go to the source code of this file.

Functions

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

Detailed Description

DI Sample

Date
2018-12-04
Author
Wanhan Hsieh
Version
V1.0
Introduction:
This is DI sample code.
Example:
1. Using default: ./di
2. Setting DI slot and channel: ./di -s5 -c2
Default:
Slot of DI module = 1
DI channel = 0
DI filter = 2
Help:
moxa@Moxa:~$ sudo ./di -h
DI sample program.

Usage: ./di [OPTIONS]

Options:
    -s      Slot of DI module. Default slot = 1
    -c      DI channel. Default channel = 0
    -f      Filter of DI module. Default filter = 2
            (One unit = 500us)

Library:
DI APIs

Definition in file di.c.

Function Documentation

◆ main()

int main ( int  argc,
char **const  argv 
)
/*******************************************************************************
* Copyright (C) 2019 Moxa Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* DI Sample Application
*
* Date Author Comment
* 2018-12-04 Wanhan Hsieh Created it.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iothinx/iothinxio.h>
int main(int argc, char **const argv)
{
int32_t rc;
uint32_t di_slot = 1;
uint8_t di_channel = 0;
uint16_t di_filter = 2;
uint32_t di_values = 0;
char ch;
while (-1 != (rc = getopt(argc, argv, "c:f:hs:")))
{
switch (rc)
{
case 'c':
di_channel = atoi(optarg);
break;
case 'f':
di_filter = atoi(optarg);
break;
case 's':
di_slot = atoi(optarg);
break;
case 'h':
default:
printf("DI sample program.\n\n");
printf("Usage: ./di [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DI module. Default slot = %d\n", "-s", di_slot);
printf("\t%-8s DI channel. Default channel = %d\n", "-c", di_channel);
printf("\t%-8s Filter of DI module. Default filter = %d\n", "-f", di_filter);
printf("\t%-8s (One unit = 500us)\n", "");
printf("\n");
return 0;
}
}
printf("DI slot = %lu\n", di_slot);
printf("DI channel = %u\n", di_channel);
printf("DI filter = %u\n", di_filter);
// initialize ioThinx I/O
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Client_Init() = %d\n", rc);
return -1;
}
// temporarily set config
rc = ioThinx_DI_Config_SetFilters(di_slot, di_channel, 1, &di_filter);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DI_Config_SetFilters() = %d\n", rc);
}
// reload config
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Config_Reload() = %d\n", rc);
}
do
{
rc = ioThinx_DI_GetValues(di_slot, &di_values);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DI_GetValues() = %d\n", rc);
break;
}
printf("[%lu] di_values = 0x%x\n", di_slot, di_values);
printf("[%lu:%u] di_val = %x\n", di_slot, di_channel, (di_values >> di_channel) & 0x1);
ch = getchar();
}
while (ch != 'q');
return 0;
}

Definition at line 53 of file di.c.

54 {
55  int32_t rc;
56  uint32_t di_slot = 1;
57  uint8_t di_channel = 0;
58  uint16_t di_filter = 2;
59  uint32_t di_values = 0;
60  char ch;
61 
62  while (-1 != (rc = getopt(argc, argv, "c:f:hs:")))
63  {
64  switch (rc)
65  {
66  case 'c':
67  di_channel = atoi(optarg);
68  break;
69  case 'f':
70  di_filter = atoi(optarg);
71  break;
72  case 's':
73  di_slot = atoi(optarg);
74  break;
75  case 'h':
76  default:
77  printf("DI sample program.\n\n");
78  printf("Usage: ./di [OPTIONS]\n\n");
79  printf("Options:\n");
80  printf("\t%-8s Slot of DI module. Default slot = %d\n", "-s", di_slot);
81  printf("\t%-8s DI channel. Default channel = %d\n", "-c", di_channel);
82  printf("\t%-8s Filter of DI module. Default filter = %d\n", "-f", di_filter);
83  printf("\t%-8s (One unit = 500us)\n", "");
84  printf("\n");
85  return 0;
86  }
87  }
88 
89  printf("DI slot = %lu\n", di_slot);
90  printf("DI channel = %u\n", di_channel);
91  printf("DI filter = %u\n", di_filter);
92 
93  // initialize ioThinx I/O
95  if (rc != IOTHINX_ERR_OK)
96  {
97  printf("ioThinx_IO_Client_Init() = %d\n", rc);
98  return -1;
99  }
100 
101  // temporarily set config
102  rc = ioThinx_DI_Config_SetFilters(di_slot, di_channel, 1, &di_filter);
103  if (rc != IOTHINX_ERR_OK)
104  {
105  printf("ioThinx_DI_Config_SetFilters() = %d\n", rc);
106  }
107 
108  // reload config
110  if (rc != IOTHINX_ERR_OK)
111  {
112  printf("ioThinx_IO_Config_Reload() = %d\n", rc);
113  }
114 
115  do
116  {
117  rc = ioThinx_DI_GetValues(di_slot, &di_values);
118  if (rc != IOTHINX_ERR_OK)
119  {
120  printf("ioThinx_DI_GetValues() = %d\n", rc);
121  break;
122  }
123  printf("[%lu] di_values = 0x%x\n", di_slot, di_values);
124  printf("[%lu:%u] di_val = %x\n", di_slot, di_channel, (di_values >> di_channel) & 0x1);
125  ch = getchar();
126  }
127  while (ch != 'q');
128 
129  return 0;
130 }
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
IOTHINX_ERR ioThinx_DI_Config_SetFilters(uint32_t slot, uint8_t start, uint8_t count, uint16_t buf[])
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
IOTHINX_ERR ioThinx_DI_GetValues(uint32_t slot, uint32_t *p_values)
IOTHINX_ERR ioThinx_IO_Client_Init(void)