- Main
- Tutorials
- Library
- Samples

- File List
- Globals

Functions
do.c File Reference

DO 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

DO Sample

Copyright
  2019 Moxa Inc. All rights reserved.

Date
  2018-12-25

Author
  Eddy Chiu

Version
  V1.0

    Introduction:
    This is DO sample code.
    Example:
    1. Using default: ./do
    2. Setting DO slot and channel: ./do -s3 -c2
    Default:
    Slot of DO module = 1
    DO channel = 0
    Help:

    moxa@Moxa:~$ sudo ./do -h
    DO sample program.

    Usage: ./do [OPTIONS]

    Options:
        -s      Slot of DO module. Default slot = 1
        -c      DO channel. Default channel = 0

Library:
DO APIs

Definition in file do.c.

## Function Documentation

int main | ( | int | argc,
|  | char **const | argv
| ) |  |

/*******************************************************************************
* Copyright (C) 2019 Moxa Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* DO Sample Application
*
* Date Author Comment
* 2018-12-25 Eddy Chiu 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 do_slot = 2;
uint8_t do_channel = 0;
uint8_t do_mode = DO_MODE_DO;
uint32_t do_values = 0;
char ch;
while (-1 != (rc = getopt(argc, argv, "c:hs:")))
{
switch (rc)
{
case 's':
do_slot = atoi(optarg);
break;
case 'c':
do_channel = atoi(optarg);
break;
case 'h':
default:
printf("DO sample program.\n\n");
printf("Usage: ./do [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DO module. Default slot = %d\n", "-s", do_slot);
printf("\t%-8s DO channel. Default channel = %d\n", "-c", do_channel);
printf("\n");
return 0;
}
}
printf("DO slot = %lu\n", do_slot);
printf("DO channel = %u\n", do_channel);
// initialize ioThinx I/O
rc = ioThinx_IO_Client_Init();
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Client_Init() = %d\n", rc);
return -1;
}
// temporarily set config
rc = ioThinx_DO_Config_SetModes(do_slot, do_channel, 1, &do_mode);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DO_Config_SetModes() = %d\n", rc);
}
// reload config
rc = ioThinx_IO_Config_Reload();
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Config_Reload() = %d\n", rc);
}
rc = ioThinx_DO_GetValues(do_slot, &do_values);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DO_GetValues() = %d\n", rc);
}
// set value
do
{
// invert do value
if (do_values & (0x1 << do_channel))
{
do_values &= ~(0x1 << do_channel);
}
else
{
do_values |= 0x1 << do_channel;
}
rc = ioThinx_DO_SetValues(do_slot, do_values);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DO_SetValues() = %d\n", rc);
break;
}
printf("[%lu:%u] DO value = %x\n", do_slot, do_channel, (do_values >> do_channel) & 0x1);
ch = getchar();
}
while (ch != 'q');
return 0;
}

Definition at line 50 of file do.c.

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