pwr.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  * Power Diagnostic Sample Application
6  *
7  * Date Author Comment
8  * 2019-01-23 Wanhan Hsieh Created it.
9  ******************************************************************************/
10 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <iothinx/iothinxio.h>
48 
50 int main(int argc, char **const argv)
51 {
52  int32_t rc;
53  uint32_t pwr_slot = 7;
54  uint8_t sys_channel = 0;
55  uint8_t field_channel = 0;
56  float alarm_voltage = 20.0f;
57  uint8_t sys_status = 0;
58  uint8_t field_status = 0;
59  char ch;
60 
61  while (-1 != (rc = getopt(argc, argv, "a:hs:")))
62  {
63  switch (rc)
64  {
65  case 'a':
66  alarm_voltage = atof(optarg);
67  break;
68  case 's':
69  pwr_slot = atoi(optarg);
70  break;
71  case 'h':
72  default:
73  printf("Power diagnostic sample program.\n\n");
74  printf("Usage: ./pwr [OPTIONS]\n\n");
75  printf("Options:\n");
76  printf("\t%-8s Slot of power module. Default slot = %d\n", "-s", pwr_slot);
77  printf("\t%-8s Field power alarm setting (voltage). Default setting = %.1f\n", "-a", alarm_voltage);
78  printf("\n");
79  return 0;
80  }
81  }
82 
83  printf("Power slot = %lu\n", pwr_slot);
84  printf("Alarm setting = %.1f\n", alarm_voltage);
85 
86  // initialize ioThinx I/O
88  if (rc != IOTHINX_ERR_OK)
89  {
90  printf("ioThinx_IO_Client_Init() = %d\n", rc);
91  return -1;
92  }
93 
94  // temporarily set config
95  rc = ioThinx_PWR_Config_SetAlarms(pwr_slot, sys_channel, 1, &alarm_voltage);
96  if (rc != IOTHINX_ERR_OK)
97  {
98  printf("ioThinx_PWR_Config_SetAlarms() = %d\n", rc);
99  }
100 
101  // reload config
103  if (rc != IOTHINX_ERR_OK)
104  {
105  printf("ioThinx_IO_Config_Reload() = %d\n", rc);
106  }
107 
108  do
109  {
110  rc = ioThinx_PWR_GetSysStatus(pwr_slot, sys_channel, 1, &sys_status);
111  if (rc != IOTHINX_ERR_OK)
112  {
113  printf("ioThinx_PWR_GetSysStatus() = %d\n", rc);
114  break;
115  }
116  rc = ioThinx_PWR_GetFieldStatus(pwr_slot, field_channel, 1, &field_status);
117  if (rc != IOTHINX_ERR_OK)
118  {
119  printf("ioThinx_PWR_GetFieldStatus() = %d\n", rc);
120  break;
121  }
122  printf("[%lu] system power status = %u\n", pwr_slot, sys_status);
123  printf("[%lu] field power status = %u\n", pwr_slot, field_status);
124  ch = getchar();
125  }
126  while (ch != 'q');
127 
128  return 0;
129 }
IOTHINX_ERR ioThinx_PWR_GetSysStatus(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
IOTHINX_ERR ioThinx_PWR_Config_SetAlarms(uint32_t slot, uint8_t start, uint8_t count, float buf[])
IOTHINX_ERR ioThinx_IO_Client_Init(void)
int main(int argc, char **const argv)
Definition: pwr.c:50
IOTHINX_ERR ioThinx_PWR_GetFieldStatus(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])