Functions
rtd_calibration.c File Reference

RTD Calibration 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

RTD Calibration Sample

Date
2019-01-04
Author
Eddy Chiu
Version
V1.0
Introduction:
This is RTD calibration sample code.
Example:
1. Using default: ./rtd_calibration
2. Setting RTD slot, channel, type and temperature: ./rtd_calibration -s6 -c2 -t2 -e36.5
Default:
Slot of RTD module = 5
RTD channel = 0
RTD type = PT100
Calibration temperature = 0 degrees Celsius
Help:
moxa@Moxa:~$ sudo ./rtd_calibration -h
RTD Calibration sample program.

Usage: ./rtd_calibration [OPTIONS]

Options:
    -s      Slot of RTD module. Default slot = 5
    -c      Channel on RTD module. Default channel = 0
    -t      RTD type. Default type = 1
    -e      Temperature in degrees Celsius. Default value = 0.000

Library:
RTD APIs

Definition in file rtd_calibration.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
*
* RTD Calibration Sample Application
*
* Date Author Comment
* 2019-01-04 Eddy Chiu Created.
******************************************************************************/
#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 rtd_slot = 5;
uint8_t rtd_channel = 0;
uint8_t rtd_type = RTD_SENSOR_TYPE_PT100;
float rtd_value;
float rtd_calibration_temperature = 0.0f;
char ch;
while (-1 != (rc = getopt(argc, argv, "c:e:hs:t:")))
{
switch (rc)
{
case 'c':
rtd_channel = atoi(optarg);
break;
case 'e':
rtd_calibration_temperature = atof(optarg);
break;
case 's':
rtd_slot = atoi(optarg);
break;
case 't':
rtd_type = atoi(optarg);
break;
case 'h':
default:
printf("RTD calibration sample program.\n\n");
printf("Usage: ./rtd_calibration [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of RTD module. Default slot = %d\n", "-s", rtd_slot);
printf("\t%-8s RTD channel. Default channel = %d\n", "-c", rtd_channel);
printf("\t%-8s RTD type. Default type = %d\n", "-t", rtd_type);
printf("\t%-8s Setting temperature. Default temperature = %.3f\n", "-e", rtd_calibration_temperature);
printf("\n");
return 0;
}
}
printf("RTD slot = %lu\n", rtd_slot);
printf("RTD channel = %u\n", rtd_channel);
printf("RTD type = %d\n", rtd_type);
printf("calibration temperature = %.3f\n", rtd_calibration_temperature);
// initialize ioThinx I/O
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Client_Init() = %d\n", rc);
return -1;
}
// temporarily set config
rc = ioThinx_RTD_Config_SetSensorTypes(rtd_slot, rtd_channel, 1, &rtd_type);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_RTD_Config_SetSensorTypes() = %d\n", rc);
return -1;
}
// reload config
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Config_Reload() = %d\n", rc);
}
printf("\nAfter executing this program, a non-volatile offset will be set for the selected channel.\n");
printf("1. Ensure the sensor is connected.\n");
printf("2. Ensure the channel and its sensor type is correctly selected.\n");
printf("3. Put the sensor into a glass that contains a mixture of ice and water.\n");
printf("4. Do not remove the sensor from the ice water during calibration...\n");
printf("Continue ? (y/n): ");
ch = getchar();
if (ch == 'y')
{
// get RTD value
rc = ioThinx_RTD_GetValues(rtd_slot, rtd_channel, 1, &rtd_value);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_RTD_GetValues() = %d\n", rc);
}
else
{
printf("RTD value before calibration: %.3f\n", rtd_value);
}
// execute the API with zero degrees Celsius as input parameter
rc = ioThinx_RTD_SetCalibrations(rtd_slot, rtd_channel, 1, &rtd_calibration_temperature);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_RTD_SetCalibrations() = %d\n", rc);
}
// wait for calibration ok
printf("Calibrating...\n");
sleep(3);
// get RTD value
rc = ioThinx_RTD_GetValues(rtd_slot, rtd_channel, 1, &rtd_value);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_RTD_GetValues() = %d\n", rc);
}
else
{
printf("RTD value after calibration: %.3f\n", rtd_value);
}
printf("Finish.\n");
}
else
{
printf("Abort.\n");
}
return 0;
}

Definition at line 54 of file rtd_calibration.c.

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
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 }
IOTHINX_ERR ioThinx_RTD_SetCalibrations(uint32_t slot, uint8_t start, uint8_t count, float buf[])
IOTHINX_ERR ioThinx_RTD_Config_SetSensorTypes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
IOTHINX_ERR ioThinx_RTD_GetValues(uint32_t slot, uint8_t start, uint8_t count, float buf[])
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
#define RTD_SENSOR_TYPE_PT100
Definition: iothinxio.h:942
IOTHINX_ERR ioThinx_IO_Client_Init(void)