Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <chrono>
  4.  
  5. #include "CSerial.h"
  6.  
  7. // ofset 11
  8. enum eThermoType {
  9.     T1 = 0 ,
  10.     T2 = 64,
  11.     T1_T2 = 192 /// T1-T2
  12. };
  13.  
  14. enum eThermoMode {
  15.     MAX = 193,
  16.     MIN = 194,
  17.     AVERGAGE = 195,
  18.     MEMORY = 0
  19. };
  20.  
  21. // offset 10, low nybble?
  22. enum eThermoSystem {
  23.     CELCIUS = 1,
  24.     FARENHEIT = 2,
  25.     KELVIN = 3
  26. };
  27.  
  28. // is meter in hold or free running state
  29. // offset 10, high nybble
  30. enum eThermoState {
  31.     HOLD = 192,
  32.     FREE = 128
  33. };
  34.  
  35. typedef struct thermo_tag {
  36.     // time
  37.     uint8_t hour;
  38.     uint8_t minute;
  39.     uint8_t seconds;
  40.     // modes etc
  41.     eThermoState state;
  42.     eThermoType type;
  43.     eThermoMode mode;
  44.     eThermoSystem system;
  45.  
  46.     // mine has two inputs
  47.     float temperatureT1;
  48.     float temperatureT2;
  49.  
  50. } thermoData ;
  51.  
  52. char getSystem(eThermoSystem s)
  53. {
  54.     switch (s) {
  55.         case KELVIN: return 'K';
  56.         case CELCIUS: return 'C';
  57.         case FARENHEIT: return 'F';
  58.     }
  59.  
  60.     return 'U';
  61. }
  62.  
  63. void dump(thermoData t)
  64. {
  65.     printf("HMS %02d:%02d:%02d,  Temp %f(T1) %f(T2) (%c)\n",
  66.         t.hour, t.minute, t.seconds,
  67.         t.temperatureT1, t.temperatureT2, getSystem(t.system)
  68.     );
  69. }
  70.  
  71. int main()
  72. {
  73.     CSerial com;
  74.     uint8_t buffer[18];
  75.     thermoData thermo;
  76.  
  77.     if (ERROR_SUCCESS == com.Open("\\\\.\\COM14")) {
  78.  
  79.         com.Setup(CSerial::EBaud9600, CSerial::EData8, CSerial::EParNone, CSerial::EStop1);
  80.        
  81.         DWORD dataRead = 0;
  82.  
  83.         int count = 10;
  84.        
  85.         com.Purge();
  86.  
  87.         while (count--) {
  88.  
  89.             if (ERROR_SUCCESS == com.Read(buffer, sizeof(buffer), &dataRead, 0, 0)) {
  90.  
  91.                 if (dataRead == sizeof(buffer)) {
  92.  
  93.                     thermo.system = (eThermoSystem)(buffer[10] & 0x0f);
  94.                     thermo.state = (eThermoState)(buffer[10] >> 4);
  95.                     thermo.type = (eThermoType)(buffer[11] & 0x0f);
  96.                     thermo.mode = (eThermoMode)buffer[12];
  97.  
  98.                     thermo.hour = (uint8_t)buffer[13];
  99.                     thermo.minute = (uint8_t)buffer[14];
  100.                     thermo.seconds = (uint8_t)buffer[15];
  101.  
  102.                     // construct temperature
  103.                     thermo.temperatureT1 = (float)(((uint16_t)(buffer[5]) << 8) + (uint16_t)(buffer[6])) / 10 ;
  104.                     thermo.temperatureT2 = (float)(((uint16_t)(buffer[7]) << 8) + (uint16_t)(buffer[8])) / 10 ;
  105.  
  106.                     dump(thermo);
  107.                 }
  108.                 else {
  109.                     std::this_thread::sleep_for(std::chrono::milliseconds(500));
  110.                 }
  111.             }
  112.         }
  113.  
  114.         com.Close();
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement