- Main
- Tutorials
- Library
- Samples

- File List
- Globals

rtd_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 * RTD 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 rtd_slot = 5;
58 uint8_t rtd_channel = 0;
59 uint8_t rtd_type = RTD_SENSOR_TYPE_PT100;
60 float rtd_value;
61 float rtd_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 rtd_channel = atoi(optarg);
70 break;
71 case 'e':
72 rtd_calibration_temperature = atof(optarg);
73 break;
74 case 's':
75 rtd_slot = atoi(optarg);
76 break;
77 case 't':
78 rtd_type = atoi(optarg);
79 break;
80 case 'h':
81 default:
82 printf("RTD calibration sample program.\n\n");
83 printf("Usage: ./rtd_calibration [OPTIONS]\n\n");
84 printf("Options:\n");
85 printf("\t%-8s Slot of RTD module. Default slot = %d\n", "-s", rtd_slot);
86 printf("\t%-8s RTD channel. Default channel = %d\n", "-c", rtd_channel);
87 printf("\t%-8s RTD type. Default type = %d\n", "-t", rtd_type);
88 printf("\t%-8s Setting temperature. Default temperature = %.3f\n", "-e", rtd_calibration_temperature);
89 printf("\n");
90 return 0;
91 }
92 }
93
94 printf("RTD slot = %lu\n", rtd_slot);
95 printf("RTD channel = %u\n", rtd_channel);
96 printf("RTD type = %d\n", rtd_type);
97 printf("calibration temperature = %.3f\n", rtd_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_RTD_Config_SetSensorTypes(rtd_slot, rtd_channel, 1, &rtd_type);
109 if (rc != IOTHINX_ERR_OK)
110 {
111 printf("ioThinx_RTD_Config_SetSensorTypes() = %d\n", rc);
112 return -1;
113 }
114
115 // reload config
116 rc = ioThinx_IO_Config_Reload();
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 RTD value
132 rc = ioThinx_RTD_GetValues(rtd_slot, rtd_channel, 1, &rtd_value);
133 if (rc != IOTHINX_ERR_OK)
134 {
135 printf("ioThinx_RTD_GetValues() = %d\n", rc);
136 }
137 else
138 {
139 printf("RTD value before calibration: %.3f\n", rtd_value);
140 }
141
142 // execute the API with zero degrees Celsius as input parameter
143 rc = ioThinx_RTD_SetCalibrations(rtd_slot, rtd_channel, 1, &rtd_calibration_temperature);
144 if (rc != IOTHINX_ERR_OK)
145 {
146 printf("ioThinx_RTD_SetCalibrations() = %d\n", rc);
147 }
148
149 // wait for calibration ok
150 printf("Calibrating...\n");
151 sleep(3);
152
153 // get RTD value
154 rc = ioThinx_RTD_GetValues(rtd_slot, rtd_channel, 1, &rtd_value);
155 if (rc != IOTHINX_ERR_OK)
156 {
157 printf("ioThinx_RTD_GetValues() = %d\n", rc);
158 }
159 else
160 {
161 printf("RTD value after calibration: %.3f\n", rtd_value);
162 }
163 printf("Finish.\n");
164 }
165 else
166 {
167 printf("Abort.\n");
168 }
169
170 return 0;
171 }
172
ioThinx_RTD_SetCalibrations
IOTHINX_ERR ioThinx_RTD_SetCalibrations(uint32_t slot, uint8_t start, uint8_t count, float buf[])
main
int main(int argc, char **const argv)
Definition: rtd_calibration.c:54
ioThinx_RTD_Config_SetSensorTypes
IOTHINX_ERR ioThinx_RTD_Config_SetSensorTypes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
IOTHINX_ERR_OK
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
ioThinx_RTD_GetValues
IOTHINX_ERR ioThinx_RTD_GetValues(uint32_t slot, uint8_t start, uint8_t count, float buf[])
ioThinx_IO_Config_Reload
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
RTD_SENSOR_TYPE_PT100
#define RTD_SENSOR_TYPE_PT100
Definition: iothinxio.h:843
ioThinx_IO_Client_Init
IOTHINX_ERR ioThinx_IO_Client_Init(void)