Functions
ao.c File Reference

AO 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

AO Sample

Date
2019-07-26
Author
Koting Hsu
Version
V1.0
Introduction:
This is AO sample code.
Example:
1. Using default: ./ao
2. Setting AO slot and channel: ./ao -s8 -c2 -n1
Default:
Slot of AO module = 8
AO start channel = 0
AO channel count = 4
Help:
moxa@Moxa:~$ sudo ./ao -h
AO sample program.

Usage: ./ao [OPTIONS]

Options:
    -s       Slot of AO module. Default slot = 8
    -c       AO start channel. Default channel = 0
    -n       AO channel count. Default count = 4

Library:
AO APIs

Definition in file ao.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
*
* AO Sample Application
*
* Date Author Comment
* 2019-07-26 Koting Hsu 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 ao_slot = 8;
uint8_t ao_channel_start = 0;
uint8_t ao_channel_count = 4;
uint8_t ao_ranges[4];
uint16_t ao_raws[4];
float ao_engs[4];
uint8_t ao_statuss[4];
int32_t i;
char ch;
while (-1 != (rc = getopt(argc, argv, "c:n:hs:")))
{
switch (rc)
{
case 's':
ao_slot = atoi(optarg);
break;
case 'c':
ao_channel_start = atoi(optarg);
break;
case 'n':
ao_channel_count = atoi(optarg);
break;
case 'h':
default:
printf("AO sample program.\n\n");
printf("Usage: ./ao [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of AO module. Default slot = %d\n", "-s", ao_slot);
printf("\t%-8s AO start channel. Default channel = %d\n", "-c", ao_channel_start);
printf("\t%-8s AO channel count. Default count = %d\n", "-n", ao_channel_count);
printf("\n");
return 0;
}
}
printf("AO slot = %lu\n", ao_slot);
printf("AO start channel = %u\n", ao_channel_start);
printf("AO channel count = %u\n", ao_channel_count);
// initialize ioThinx I/O
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Client_Init() = %d\n", rc);
return -1;
}
// temporarily set config
for (i = 0; i < ao_channel_count; i++)
{
ao_ranges[i] = AO_RANGE_4_20mA;
}
rc = ioThinx_AO_Config_SetRanges(ao_slot, ao_channel_start, ao_channel_count, ao_ranges);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_AO_Config_SetRanges() = %d\n", rc);
}
// reload config
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Config_Reload() = %d\n", rc);
}
// set value
for (i = 0; i < ao_channel_count; i++)
{
ao_raws[i] = 0x800;
}
rc = ioThinx_AO_SetRaws(ao_slot, ao_channel_start, ao_channel_count, ao_raws);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_AO_Config_SetRanges() = %d\n", rc);
}
// wait for signal stable
sleep(1);
// get value
do
{
rc = ioThinx_AO_GetEngs(ao_slot, ao_channel_start, ao_channel_count, ao_engs);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_AO_GetEngs() = %d\n", rc);
break;
}
rc = ioThinx_AO_GetStatuss(ao_slot, ao_channel_start, ao_channel_count, ao_statuss);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_AO_GetStatuss() = %d\n", rc);
break;
}
for (i = 0; i < ao_channel_count; i++)
{
printf("[%lu:%u] eng = %.3f, status = %d\n", ao_slot, ao_channel_start+i, ao_engs[i], ao_statuss[i]);
}
ch = getchar();
}
while (ch != 'q');
return 0;
}

Definition at line 52 of file ao.c.

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
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[])