tc_calibration.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  * TC Calibration Sample Application
6  *
7  * Date Author Comment
8  * 2019-01-04 Eddy Chiu Created.
9  ******************************************************************************/
10 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <iothinx/iothinxio.h>
52 
54 int main(int argc, char **const argv)
55 {
56  int32_t rc;
57  uint32_t tc_slot = 6;
58  uint8_t tc_channel = 0;
59  uint8_t tc_type = TC_SENSOR_TYPE_K;
60  float tc_value;
61  float tc_calibration_temperature = 0.0f;
62  char ch;
63 
64  while (-1 != (rc = getopt(argc, argv, "c:e:hs:t:")))
65  {
66  switch (rc)
67  {
68  case 'c':
69  tc_channel = atoi(optarg);
70  break;
71  case 'e':
72  tc_calibration_temperature = atof(optarg);
73  break;
74  case 's':
75  tc_slot = atoi(optarg);
76  break;
77  case 't':
78  tc_type = atoi(optarg);
79  break;
80  case 'h':
81  default:
82  printf("TC calibration sample program.\n\n");
83  printf("Usage: ./tc_calibration [OPTIONS]\n\n");
84  printf("Options:\n");
85  printf("\t%-8s Slot of TC module. Default slot = %d\n", "-s", tc_slot);
86  printf("\t%-8s TC channel. Default channel = %d\n", "-c", tc_channel);
87  printf("\t%-8s TC type. Default type = %d\n", "-t", tc_type);
88  printf("\t%-8s Setting temperature. Default temperature = %.3f\n", "-e", tc_calibration_temperature);
89  printf("\n");
90  return 0;
91  }
92  }
93 
94  printf("TC slot = %lu\n", tc_slot);
95  printf("TC channel = %u\n", tc_channel);
96  printf("TC type = %d\n", tc_type);
97  printf("calibration temperature = %.3f\n", tc_calibration_temperature);
98 
99  // initialize ioThinx I/O
100  rc = ioThinx_IO_Client_Init();
101  if (rc != IOTHINX_ERR_OK)
102  {
103  printf("ioThinx_IO_Client_Init() = %d\n", rc);
104  return -1;
105  }
106 
107  // temporarily set config
108  rc = ioThinx_TC_Config_SetSensorTypes(tc_slot, tc_channel, 1, &tc_type);
109  if (rc != IOTHINX_ERR_OK)
110  {
111  printf("ioThinx_TC_Config_SetSensorTypes() = %d\n", rc);
112  return -1;
113  }
114 
115  // reload config
117  if (rc != IOTHINX_ERR_OK)
118  {
119  printf("ioThinx_IO_Config_Reload() = %d\n", rc);
120  }
121 
122  printf("\nAfter executing this program, a non-volatile offset will be set for the selected channel.\n");
123  printf("1. Ensure the sensor is connected.\n");
124  printf("2. Ensure the channel and its sensor type is correctly selected.\n");
125  printf("3. Put the sensor into a glass that contains a mixture of ice and water.\n");
126  printf("4. Do not remove the sensor from the ice water during calibration...\n");
127  printf("Continue ? (y/n): ");
128  ch = getchar();
129  if (ch == 'y')
130  {
131  // get TC value
132  rc = ioThinx_TC_GetValues(tc_slot, tc_channel, 1, &tc_value);
133  if (rc != IOTHINX_ERR_OK)
134  {
135  printf("ioThinx_TC_GetValues() = %d\n", rc);
136  }
137  else
138  {
139  printf("TC value before calibration: %.3f\n", tc_value);
140  }
141 
142  // execute the API with zero degrees Celsius as input parameter
143  rc = ioThinx_TC_SetCalibrations(tc_slot, tc_channel, 1, &tc_calibration_temperature);
144  if (rc != IOTHINX_ERR_OK)
145  {
146  printf("ioThinx_TC_SetCalibrations() = %d\n", rc);
147  }
148 
149  // wait for calibration ok
150  printf("Calibrating...\n");
151  sleep(3);
152 
153  // get TC value
154  rc = ioThinx_TC_GetValues(tc_slot, tc_channel, 1, &tc_value);
155  if (rc != IOTHINX_ERR_OK)
156  {
157  printf("ioThinx_TC_GetValues() = %d\n", rc);
158  }
159  else
160  {
161  printf("TC value after calibration: %.3f\n", tc_value);
162  }
163  printf("Finish.\n");
164  }
165  else
166  {
167  printf("Abort.\n");
168  }
169 
170  return 0;
171 }
#define TC_SENSOR_TYPE_K
Definition: iothinxio.h:805
int main(int argc, char **const argv)
#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_SetCalibrations(uint32_t slot, uint8_t start, uint8_t count, float buf[])
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
IOTHINX_ERR ioThinx_IO_Client_Init(void)