Functions
pwr.c File Reference

Power Diagnostic 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

Power Diagnostic Sample

Date
2023-12-04
Author
David CL Tsai
Version
V2.0
Introduction:
This is power diagnostic sample code.
Example:
1. Using default: ./pwr
2. Setting power slot and system power alarm: ./pwr -s8 -a12.3
Default:
Slot of pwr module = 1
Diagnostic config = 0
Help:
moxa@Moxa:~$ sudo ./pwr -h
Power diagnostic sample program.

Usage: ./pwr [OPTIONS]

Options:
        -s       Slot of power module. Default slot = 7
        -a       System power alarm setting (voltage). Default setting = 20.0

Library:
Power APIs

Definition in file pwr.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
*
* Power Diagnostic Sample Application
*
* Date Author Comment
* 2019-01-23 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 pwr_slot = 7;
uint8_t sys_channel = 0;
uint8_t field_channel = 0;
float alarm_voltage = 20.0f;
uint8_t sys_status = 0;
uint8_t field_status = 0;
char ch;
while (-1 != (rc = getopt(argc, argv, "a:hs:")))
{
switch (rc)
{
case 'a':
alarm_voltage = atof(optarg);
break;
case 's':
pwr_slot = atoi(optarg);
break;
case 'h':
default:
printf("Power diagnostic sample program.\n\n");
printf("Usage: ./pwr [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of power module. Default slot = %d\n", "-s", pwr_slot);
printf("\t%-8s Field power alarm setting (voltage). Default setting = %.1f\n", "-a", alarm_voltage);
printf("\n");
return 0;
}
}
printf("Power slot = %lu\n", pwr_slot);
printf("Alarm setting = %.1f\n", alarm_voltage);
// initialize ioThinx I/O
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Client_Init() = %d\n", rc);
return -1;
}
// temporarily set config
rc = ioThinx_PWR_Config_SetAlarms(pwr_slot, sys_channel, 1, &alarm_voltage);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_PWR_Config_SetAlarms() = %d\n", rc);
}
// reload config
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Config_Reload() = %d\n", rc);
}
do
{
rc = ioThinx_PWR_GetSysStatus(pwr_slot, sys_channel, 1, &sys_status);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_PWR_GetSysStatus() = %d\n", rc);
break;
}
rc = ioThinx_PWR_GetFieldStatus(pwr_slot, field_channel, 1, &field_status);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_PWR_GetFieldStatus() = %d\n", rc);
break;
}
printf("[%lu] system power status = %u\n", pwr_slot, sys_status);
printf("[%lu] field power status = %u\n", pwr_slot, field_status);
ch = getchar();
}
while (ch != 'q');
return 0;
}

Definition at line 50 of file pwr.c.

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)
IOTHINX_ERR ioThinx_PWR_GetFieldStatus(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])