- Main
- Tutorials
- Library
- Samples

- File List
- Globals

Functions
counter.c File Reference

Counter 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

Counter Sample

Copyright
  2019 Moxa Inc. All rights reserved.

Date
  2018-12-04

Author
  Wanhan Hsieh

Version
  V1.0

    Introduction:
    This is counter sample code.
    Example:
    1. Using default: ./counter
    2. Setting DI slot and counter channel: ./counter -s2 -c3 -n1
    Default:
    Slot of DI module = 1
    Counter start channel = 0
    Counter channel count = 2
    Help:

    moxa@Moxa:~$ sudo ./counter -h
    Counter sample program.

    Usage: ./counter [OPTIONS]

    Options:
        -s       Slot of DI module. Default slot = 1
        -c       Counter start channel. Default channel = 0
        -n       Counter channel count. Default count = 2

Library:
DI APIs

Definition in file counter.c.

## Function Documentation

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

/*******************************************************************************
* Copyright (C) 2019 Moxa Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Counter 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_modes[16];
uint8_t counter_channel_start = 0;
uint8_t counter_channel_count = 2;
uint32_t counter_starts = 0;
uint8_t counter_triggers[4];
uint32_t counter_values[4];
int32_t i;
char ch;
while (-1 != (rc = getopt(argc, argv, "c:hn:s:")))
{
switch (rc)
{
case 'c':
counter_channel_start = atoi(optarg);
break;
case 'n':
counter_channel_count = atoi(optarg);
break;
case 's':
di_slot = atoi(optarg);
break;
case 'h':
default:
printf("Counter sample program.\n\n");
printf("Usage: ./counter [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DI module. Default slot = %d\n", "-s", di_slot);
printf("\t%-8s Counter start channel. Default channel = %d\n", "-c", counter_channel_start);
printf("\t%-8s Counter channel count. Default count = %d\n", "-n", counter_channel_count);
printf("\n");
return 0;
}
}
printf("DI slot = %lu\n", di_slot);
printf("Counter start channel = %u\n", counter_channel_start);
printf("Counter channel count = %u\n", counter_channel_count);
// initialize ioThinx I/O
rc = ioThinx_IO_Client_Init();
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Client_Init() = %d\n", rc);
return -1;
}
// temporarily set config
for (i = 0; i < counter_channel_count; i++)
{
di_modes[i] = DI_MODE_COUNTER;
}
rc = ioThinx_DI_Config_SetModes(di_slot, counter_channel_start, counter_channel_count, di_modes);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DI_Config_SetModes() = %d\n", rc);
}
for (i = 0; i < counter_channel_count; i++)
{
counter_triggers[i] = CNT_TRIGGER_BOTH;
}
rc = ioThinx_DI_Config_SetCntTriggers(di_slot, counter_channel_start, counter_channel_count, counter_triggers);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DI_Config_SetCntTriggers() = %d\n", rc);
}
for (i = 0; i < counter_channel_count; i++)
{
counter_values[i] = 0;
}
rc = ioThinx_DI_Config_SetCntValues(di_slot, counter_channel_start, counter_channel_count, counter_values);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DI_Config_SetCntValues() = %d\n", rc);
}
// reload config
rc = ioThinx_IO_Config_Reload();
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Config_Reload() = %d\n", rc);
}
// start counter
counter_starts = 0;
for (i = 0; i < counter_channel_count; i++)
{
counter_starts |= 0x1 << (counter_channel_start + i);
}
rc = ioThinx_DI_SetCntStarts(di_slot, counter_starts);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DI_SetCntStarts() = %d\n", rc);
}
// get value
do
{
rc = ioThinx_DI_GetCntValues(di_slot, counter_channel_start, counter_channel_count, counter_values);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DI_GetCntValues() = %d\n", rc);
break;
}
for (i = 0; i < counter_channel_count; i++)
{
printf("[%lu:%u] counter = %d\n", di_slot, counter_channel_start+i, counter_values[i]);
}
ch = getchar();
}
while (ch != 'q');
return 0;
}

Definition at line 52 of file counter.c.

53 {
54 int32_t rc;
55 uint32_t di_slot = 1;
56 uint8_t di_modes[16];
57 uint8_t counter_channel_start = 0;
58 uint8_t counter_channel_count = 2;
59 uint32_t counter_starts = 0;
60 uint8_t counter_triggers[4];
61 uint32_t counter_values[4];
62 int32_t i;
63 char ch;
64
65 while (-1 != (rc = getopt(argc, argv, "c:hn:s:")))
66 {
67 switch (rc)
68 {
69 case 'c':
70 counter_channel_start = atoi(optarg);
71 break;
72 case 'n':
73 counter_channel_count = atoi(optarg);
74 break;
75 case 's':
76 di_slot = atoi(optarg);
77 break;
78 case 'h':
79 default:
80 printf("Counter sample program.\n\n");
81 printf("Usage: ./counter [OPTIONS]\n\n");
82 printf("Options:\n");
83 printf("\t%-8s Slot of DI module. Default slot = %d\n", "-s", di_slot);
84 printf("\t%-8s Counter start channel. Default channel = %d\n", "-c", counter_channel_start);
85 printf("\t%-8s Counter channel count. Default count = %d\n", "-n", counter_channel_count);
86 printf("\n");
87 return 0;
88 }
89 }
90
91 printf("DI slot = %lu\n", di_slot);
92 printf("Counter start channel = %u\n", counter_channel_start);
93 printf("Counter channel count = %u\n", counter_channel_count);
94
95 // initialize ioThinx I/O
96 rc = ioThinx_IO_Client_Init();
97 if (rc != IOTHINX_ERR_OK)
98 {
99 printf("ioThinx_IO_Client_Init() = %d\n", rc);
100 return -1;
101 }
102
103 // temporarily set config
104 for (i = 0; i < counter_channel_count; i++)
105 {
106 di_modes[i] = DI_MODE_COUNTER;
107 }
108 rc = ioThinx_DI_Config_SetModes(di_slot, counter_channel_start, counter_channel_count, di_modes);
109 if (rc != IOTHINX_ERR_OK)
110 {
111 printf("ioThinx_DI_Config_SetModes() = %d\n", rc);
112 }
113
114 for (i = 0; i < counter_channel_count; i++)
115 {
116 counter_triggers[i] = CNT_TRIGGER_BOTH;
117 }
118 rc = ioThinx_DI_Config_SetCntTriggers(di_slot, counter_channel_start, counter_channel_count, counter_triggers);
119 if (rc != IOTHINX_ERR_OK)
120 {
121 printf("ioThinx_DI_Config_SetCntTriggers() = %d\n", rc);
122 }
123
124 for (i = 0; i < counter_channel_count; i++)
125 {
126 counter_values[i] = 0;
127 }
128 rc = ioThinx_DI_Config_SetCntValues(di_slot, counter_channel_start, counter_channel_count, counter_values);
129 if (rc != IOTHINX_ERR_OK)
130 {
131 printf("ioThinx_DI_Config_SetCntValues() = %d\n", rc);
132 }
133
134 // reload config
135 rc = ioThinx_IO_Config_Reload();
136 if (rc != IOTHINX_ERR_OK)
137 {
138 printf("ioThinx_IO_Config_Reload() = %d\n", rc);
139 }
140
141 // start counter
142 counter_starts = 0;
143 for (i = 0; i < counter_channel_count; i++)
144 {
145 counter_starts |= 0x1 << (counter_channel_start + i);
146 }
147 rc = ioThinx_DI_SetCntStarts(di_slot, counter_starts);
148 if (rc != IOTHINX_ERR_OK)
149 {
150 printf("ioThinx_DI_SetCntStarts() = %d\n", rc);
151 }
152
153 // get value
154 do
155 {
156 rc = ioThinx_DI_GetCntValues(di_slot, counter_channel_start, counter_channel_count, counter_values);
157 if (rc != IOTHINX_ERR_OK)
158 {
159 printf("ioThinx_DI_GetCntValues() = %d\n", rc);
160 break;
161 }
162 for (i = 0; i < counter_channel_count; i++)
163 {
164 printf("[%lu:%u] counter = %d\n", di_slot, counter_channel_start+i, counter_values[i]);
165 }
166 ch = getchar();
167 }
168 while (ch != 'q');
169
170 return 0;
171 }
ioThinx_DI_SetCntStarts
IOTHINX_ERR ioThinx_DI_SetCntStarts(uint32_t slot, uint32_t starts)
ioThinx_DI_Config_SetModes
IOTHINX_ERR ioThinx_DI_Config_SetModes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
ioThinx_DI_GetCntValues
IOTHINX_ERR ioThinx_DI_GetCntValues(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
IOTHINX_ERR_OK
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
CNT_TRIGGER_BOTH
#define CNT_TRIGGER_BOTH
Definition: iothinxio.h:97
DI_MODE_COUNTER
#define DI_MODE_COUNTER
Definition: iothinxio.h:87
ioThinx_IO_Config_Reload
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
ioThinx_DI_Config_SetCntTriggers
IOTHINX_ERR ioThinx_DI_Config_SetCntTriggers(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
ioThinx_DI_Config_SetCntValues
IOTHINX_ERR ioThinx_DI_Config_SetCntValues(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
ioThinx_IO_Client_Init
IOTHINX_ERR ioThinx_IO_Client_Init(void)