Functions
tc.c File Reference

TC 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

TC Sample

Date
2018-12-27
Author
Eddy Chiu
Version
V1.0
Introduction:
This is TC sample code.
Example:
1. Using default: ./tc
2. Setting TC slot and channel: ./tc -s7 -c2 -n3
Default:
Slot of TC module = 1
TC channel = 0
Help:
moxa@Moxa:~$ sudo ./tc -h
TC sample program.

Usage: ./tc [OPTIONS]

Options:
    -s       Slot of TC module. Default slot = 6
    -c       TC start channel. Default channel = 0
    -n       TC channel count. Default count = 2

Library:
TC APIs

Definition in file tc.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
*
* TC Sample Application
*
* Date Author Comment
* 2018-12-27 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 tc_slot = 6;
uint8_t tc_channel_start = 0;
uint8_t tc_channel_count = 2;
uint8_t tc_types[8];
float tc_values[8];
uint8_t tc_statuss[8];
int32_t i;
char ch;
while (-1 != (rc = getopt(argc, argv, "c:n:hs:")))
{
switch (rc)
{
case 's':
tc_slot = atoi(optarg);
break;
case 'c':
tc_channel_start = atoi(optarg);
break;
case 'n':
tc_channel_count = atoi(optarg);
break;
case 'h':
default:
printf("TC sample program.\n\n");
printf("Usage: ./tc [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of TC module. Default slot = %d\n", "-s", tc_slot);
printf("\t%-8s TC start channel. Default channel = %d\n", "-c", tc_channel_start);
printf("\t%-8s TC channel count. Default count = %d\n", "-n", tc_channel_count);
printf("\n");
return 0;
}
}
printf("TC slot = %lu\n", tc_slot);
printf("TC start channel = %u\n", tc_channel_start);
printf("TC channel count = %u\n", tc_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 < tc_channel_count; i++)
{
tc_types[i] = TC_SENSOR_TYPE_K;
}
rc = ioThinx_TC_Config_SetSensorTypes(tc_slot, tc_channel_start, tc_channel_count, tc_types);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_TC_Config_SetSensorTypes() = %d\n", rc);
}
// reload config
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Config_Reload() = %d\n", rc);
}
// get value
do
{
rc = ioThinx_TC_GetValues(tc_slot, tc_channel_start, tc_channel_count, tc_values);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_TC_GetValues() = %d\n", rc);
break;
}
rc = ioThinx_TC_GetStatuss(tc_slot, tc_channel_start, tc_channel_count, tc_statuss);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_TC_GetStatuss() = %d\n", rc);
break;
}
for (i = 0; i < tc_channel_count; i++)
{
printf("[%lu:%u] value = %.2f, status = %d\n", tc_slot, tc_channel_start+i, tc_values[i], tc_statuss[i]);
}
ch = getchar();
}
while (ch != 'q');
return 0;
}

Definition at line 51 of file tc.c.

52 {
53  int32_t rc;
54  uint32_t tc_slot = 6;
55  uint8_t tc_channel_start = 0;
56  uint8_t tc_channel_count = 2;
57  uint8_t tc_types[8];
58  float tc_values[8];
59  uint8_t tc_statuss[8];
60  int32_t i;
61  char ch;
62 
63  while (-1 != (rc = getopt(argc, argv, "c:n:hs:")))
64  {
65  switch (rc)
66  {
67  case 's':
68  tc_slot = atoi(optarg);
69  break;
70  case 'c':
71  tc_channel_start = atoi(optarg);
72  break;
73  case 'n':
74  tc_channel_count = atoi(optarg);
75  break;
76  case 'h':
77  default:
78  printf("TC sample program.\n\n");
79  printf("Usage: ./tc [OPTIONS]\n\n");
80  printf("Options:\n");
81  printf("\t%-8s Slot of TC module. Default slot = %d\n", "-s", tc_slot);
82  printf("\t%-8s TC start channel. Default channel = %d\n", "-c", tc_channel_start);
83  printf("\t%-8s TC channel count. Default count = %d\n", "-n", tc_channel_count);
84  printf("\n");
85  return 0;
86  }
87  }
88 
89  printf("TC slot = %lu\n", tc_slot);
90  printf("TC start channel = %u\n", tc_channel_start);
91  printf("TC channel count = %u\n", tc_channel_count);
92 
93  // initialize ioThinx I/O
95  if (rc != IOTHINX_ERR_OK)
96  {
97  printf("ioThinx_IO_Client_Init() = %d\n", rc);
98  return -1;
99  }
100 
101  // temporarily set config
102  for (i = 0; i < tc_channel_count; i++)
103  {
104  tc_types[i] = TC_SENSOR_TYPE_K;
105  }
106  rc = ioThinx_TC_Config_SetSensorTypes(tc_slot, tc_channel_start, tc_channel_count, tc_types);
107  if (rc != IOTHINX_ERR_OK)
108  {
109  printf("ioThinx_TC_Config_SetSensorTypes() = %d\n", rc);
110  }
111 
112  // reload config
114  if (rc != IOTHINX_ERR_OK)
115  {
116  printf("ioThinx_IO_Config_Reload() = %d\n", rc);
117  }
118 
119  // get value
120  do
121  {
122  rc = ioThinx_TC_GetValues(tc_slot, tc_channel_start, tc_channel_count, tc_values);
123  if (rc != IOTHINX_ERR_OK)
124  {
125  printf("ioThinx_TC_GetValues() = %d\n", rc);
126  break;
127  }
128  rc = ioThinx_TC_GetStatuss(tc_slot, tc_channel_start, tc_channel_count, tc_statuss);
129  if (rc != IOTHINX_ERR_OK)
130  {
131  printf("ioThinx_TC_GetStatuss() = %d\n", rc);
132  break;
133  }
134  for (i = 0; i < tc_channel_count; i++)
135  {
136  printf("[%lu:%u] value = %.2f, status = %d\n", tc_slot, tc_channel_start+i, tc_values[i], tc_statuss[i]);
137  }
138  ch = getchar();
139  }
140  while (ch != 'q');
141 
142  return 0;
143 }
#define TC_SENSOR_TYPE_K
Definition: iothinxio.h:805
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
IOTHINX_ERR ioThinx_TC_GetValues(uint32_t slot, uint8_t start, uint8_t count, float buf[])
IOTHINX_ERR ioThinx_TC_Config_SetSensorTypes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
IOTHINX_ERR ioThinx_TC_GetStatuss(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)