ao.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  * AO Sample Application
6  *
7  * Date Author Comment
8  * 2019-07-26 Koting Hsu Created it.
9  ******************************************************************************/
10 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <iothinx/iothinxio.h>
50 
52 int main(int argc, char **const argv)
53 {
54  int32_t rc;
55  uint32_t ao_slot = 8;
56  uint8_t ao_channel_start = 0;
57  uint8_t ao_channel_count = 4;
58  uint8_t ao_ranges[4];
59  uint16_t ao_raws[4];
60  float ao_engs[4];
61  uint8_t ao_statuss[4];
62  int32_t i;
63  char ch;
64 
65  while (-1 != (rc = getopt(argc, argv, "c:n:hs:")))
66  {
67  switch (rc)
68  {
69  case 's':
70  ao_slot = atoi(optarg);
71  break;
72  case 'c':
73  ao_channel_start = atoi(optarg);
74  break;
75  case 'n':
76  ao_channel_count = atoi(optarg);
77  break;
78  case 'h':
79  default:
80  printf("AO sample program.\n\n");
81  printf("Usage: ./ao [OPTIONS]\n\n");
82  printf("Options:\n");
83  printf("\t%-8s Slot of AO module. Default slot = %d\n", "-s", ao_slot);
84  printf("\t%-8s AO start channel. Default channel = %d\n", "-c", ao_channel_start);
85  printf("\t%-8s AO channel count. Default count = %d\n", "-n", ao_channel_count);
86  printf("\n");
87  return 0;
88  }
89  }
90 
91  printf("AO slot = %lu\n", ao_slot);
92  printf("AO start channel = %u\n", ao_channel_start);
93  printf("AO channel count = %u\n", ao_channel_count);
94 
95  // initialize ioThinx I/O
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 < ao_channel_count; i++)
105  {
106  ao_ranges[i] = AO_RANGE_4_20mA;
107  }
108  rc = ioThinx_AO_Config_SetRanges(ao_slot, ao_channel_start, ao_channel_count, ao_ranges);
109  if (rc != IOTHINX_ERR_OK)
110  {
111  printf("ioThinx_AO_Config_SetRanges() = %d\n", rc);
112  }
113 
114  // reload config
116  if (rc != IOTHINX_ERR_OK)
117  {
118  printf("ioThinx_IO_Config_Reload() = %d\n", rc);
119  }
120 
121  // set value
122  for (i = 0; i < ao_channel_count; i++)
123  {
124  ao_raws[i] = 0x800;
125  }
126  rc = ioThinx_AO_SetRaws(ao_slot, ao_channel_start, ao_channel_count, ao_raws);
127  if (rc != IOTHINX_ERR_OK)
128  {
129  printf("ioThinx_AO_Config_SetRanges() = %d\n", rc);
130  }
131 
132  // wait for signal stable
133  sleep(1);
134 
135  // get value
136  do
137  {
138  rc = ioThinx_AO_GetEngs(ao_slot, ao_channel_start, ao_channel_count, ao_engs);
139  if (rc != IOTHINX_ERR_OK)
140  {
141  printf("ioThinx_AO_GetEngs() = %d\n", rc);
142  break;
143  }
144  rc = ioThinx_AO_GetStatuss(ao_slot, ao_channel_start, ao_channel_count, ao_statuss);
145  if (rc != IOTHINX_ERR_OK)
146  {
147  printf("ioThinx_AO_GetStatuss() = %d\n", rc);
148  break;
149  }
150  for (i = 0; i < ao_channel_count; i++)
151  {
152  printf("[%lu:%u] eng = %.3f, status = %d\n", ao_slot, ao_channel_start+i, ao_engs[i], ao_statuss[i]);
153  }
154  ch = getchar();
155  }
156  while (ch != 'q');
157 
158  return 0;
159 }
IOTHINX_ERR ioThinx_AO_SetRaws(uint32_t slot, uint8_t start, uint8_t count, uint16_t buf[])
#define AO_RANGE_4_20mA
Definition: iothinxio.h:709
int main(int argc, char **const argv)
Definition: ao.c:52
IOTHINX_ERR ioThinx_AO_GetStatuss(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
IOTHINX_ERR ioThinx_AO_Config_SetRanges(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
IOTHINX_ERR ioThinx_IO_Client_Init(void)
IOTHINX_ERR ioThinx_AO_GetEngs(uint32_t slot, uint8_t start, uint8_t count, float buf[])