Functions
relay.c File Reference

Relay 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

Relay Sample

Date
2019-01-17
Author
Wanhan Hsieh
Version
V1.0
Introduction:
This is relay sample code.
Example:
1. Using default: ./relay
2. Setting Relay slot and channel: ./relay -s4 -c2
Default:
Slot of relay module = 3
Relay channel = 0
Help:
moxa@Moxa:~$ sudo ./relay -h
Relay sample program.

Usage: ./relay [OPTIONS]

Options:
        -s       Slot of relay module. Default slot = 3
        -c       Relay channel. Default channel = 0

Library:
Relay APIs

Definition in file relay.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
*
* Relay Sample Application
*
* Date Author Comment
* 2019-01-17 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 relay_slot = 3;
uint8_t relay_channel = 0;
uint32_t relay_values = 0;
uint32_t relay_count = 0;
char ch;
while (-1 != (rc = getopt(argc, argv, "c:hs:")))
{
switch (rc)
{
case 's':
relay_slot = atoi(optarg);
break;
case 'c':
relay_channel = atoi(optarg);
break;
case 'h':
default:
printf("Relay sample program.\n\n");
printf("Usage: ./relay [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of relay module. Default slot = %d\n", "-s", relay_slot);
printf("\t%-8s Relay channel. Default channel = %d\n", "-c", relay_channel);
printf("\n");
return 0;
}
}
printf("Relay slot = %lu\n", relay_slot);
printf("Relay channel = %u\n", relay_channel);
// initialize ioThinx I/O
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Client_Init() = %d\n", rc);
return -1;
}
rc = ioThinx_Relay_GetValues(relay_slot, &relay_values);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Relay_GetValues() = %d\n", rc);
}
// set value
do
{
// invert relay value
if (relay_values & (0x1 << relay_channel))
{
relay_values &= ~(0x1 << relay_channel);
}
else
{
relay_values |= 0x1 << relay_channel;
}
rc = ioThinx_Relay_SetValues(relay_slot, relay_values);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DO_GetValues() = %d\n", rc);
}
printf("[%lu:%u] relay value = %x\n", relay_slot, relay_channel, (relay_values >> relay_channel) & 0x1);
rc = ioThinx_Relay_GetCurrentCounts(relay_slot, relay_channel, 1, &relay_count);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_Relay_GetCurrentCounts() = %d\n", rc);
}
printf("[%lu:%u] relay count = %lu\n", relay_slot, relay_channel, relay_count);
ch = getchar();
}
while (ch != 'q');
return 0;
}

Definition at line 50 of file relay.c.

51 {
52  int32_t rc;
53  uint32_t relay_slot = 3;
54  uint8_t relay_channel = 0;
55  uint32_t relay_values = 0;
56  uint32_t relay_count = 0;
57  char ch;
58 
59  while (-1 != (rc = getopt(argc, argv, "c:hs:")))
60  {
61  switch (rc)
62  {
63  case 's':
64  relay_slot = atoi(optarg);
65  break;
66  case 'c':
67  relay_channel = atoi(optarg);
68  break;
69  case 'h':
70  default:
71  printf("Relay sample program.\n\n");
72  printf("Usage: ./relay [OPTIONS]\n\n");
73  printf("Options:\n");
74  printf("\t%-8s Slot of relay module. Default slot = %d\n", "-s", relay_slot);
75  printf("\t%-8s Relay channel. Default channel = %d\n", "-c", relay_channel);
76  printf("\n");
77  return 0;
78  }
79  }
80 
81  printf("Relay slot = %lu\n", relay_slot);
82  printf("Relay channel = %u\n", relay_channel);
83 
84  // initialize ioThinx I/O
86  if (rc != IOTHINX_ERR_OK)
87  {
88  printf("ioThinx_IO_Client_Init() = %d\n", rc);
89  return -1;
90  }
91 
92  rc = ioThinx_Relay_GetValues(relay_slot, &relay_values);
93  if (rc != IOTHINX_ERR_OK)
94  {
95  printf("ioThinx_Relay_GetValues() = %d\n", rc);
96  }
97 
98  // set value
99  do
100  {
101  // invert relay value
102  if (relay_values & (0x1 << relay_channel))
103  {
104  relay_values &= ~(0x1 << relay_channel);
105  }
106  else
107  {
108  relay_values |= 0x1 << relay_channel;
109  }
110 
111  rc = ioThinx_Relay_SetValues(relay_slot, relay_values);
112  if (rc != IOTHINX_ERR_OK)
113  {
114  printf("ioThinx_DO_GetValues() = %d\n", rc);
115  }
116  printf("[%lu:%u] relay value = %x\n", relay_slot, relay_channel, (relay_values >> relay_channel) & 0x1);
117 
118  rc = ioThinx_Relay_GetCurrentCounts(relay_slot, relay_channel, 1, &relay_count);
119  if (rc != IOTHINX_ERR_OK)
120  {
121  printf("ioThinx_Relay_GetCurrentCounts() = %d\n", rc);
122  }
123  printf("[%lu:%u] relay count = %lu\n", relay_slot, relay_channel, relay_count);
124  ch = getchar();
125  }
126  while (ch != 'q');
127 
128  return 0;
129 }
IOTHINX_ERR ioThinx_Relay_GetCurrentCounts(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
IOTHINX_ERR ioThinx_Relay_GetValues(uint32_t slot, uint32_t *p_values)
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
IOTHINX_ERR ioThinx_Relay_SetValues(uint32_t slot, uint32_t values)
IOTHINX_ERR ioThinx_IO_Client_Init(void)