fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5.  
  6. char sendbuf[129];
  7.  
  8. float temp_lm73s = 5.5555, temp_lm73f = 7.7777, f_cold_junction_temperature = 22.2222, f_linearized_tc_temperature = 33.3333, rtd_temp = 44.4444;
  9. bool bot_heat_state = false, top_heat_state = true;
  10. uint8_t fans_rpm = 0xFF;
  11.  
  12. uint8_t var_tosend[] = {3, 4, 5, 6, 7}; // выход с парсера
  13.  
  14. int main(void) {
  15.  
  16. char temp_buf[129];
  17. char tmp_str[10];
  18.  
  19. memset(temp_buf, 0, 129);
  20.  
  21. snprintf(temp_buf, sizeof(temp_buf), "<S:0,%3.3f;1,%3.3f;2,%u;", temp_lm73s, temp_lm73f, fans_rpm);
  22.  
  23. for(uint8_t k = 0; k < (sizeof(var_tosend) / sizeof(var_tosend[0])); k++)
  24. {
  25. //if((!var_tosend[k]) && (!var_tosend[k+1])){break;}; // if current element of array and element that going after it == 0 -> exit
  26.  
  27. switch(var_tosend[k]){
  28. case 3:
  29. snprintf(tmp_str, sizeof(tmp_str), "3,%3.3f;", f_cold_junction_temperature);
  30. strcat(temp_buf, tmp_str);
  31. break;
  32.  
  33. case 4:
  34. snprintf(tmp_str, sizeof(tmp_str), "4,%3.3f;", f_linearized_tc_temperature);
  35. strcat(temp_buf, tmp_str);
  36. break;
  37.  
  38. case 5:
  39. snprintf(tmp_str, sizeof(tmp_str), "5,%3.3f;", rtd_temp);
  40. strcat(temp_buf, tmp_str);
  41. break;
  42.  
  43. case 6:
  44. snprintf(tmp_str, sizeof(tmp_str), "6,%d;", bot_heat_state ? 1 : 0);
  45. strcat(temp_buf, tmp_str);
  46. break;
  47.  
  48. case 7:
  49. snprintf(tmp_str, sizeof(tmp_str), "7,%d;", top_heat_state ? 1 : 0);
  50. strcat(temp_buf, tmp_str);
  51. break;
  52. }
  53. memset(tmp_str, 0, 10);
  54. }
  55.  
  56. strcat(temp_buf, ">");
  57.  
  58. int res = strlen(temp_buf);
  59. if (res >= sizeof(sendbuf)) {
  60. printf("Data truncated.\n");
  61. }
  62.  
  63. printf("%s\n", temp_buf);
  64.  
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0s 4160KB
stdin
Standard input is empty
stdout
<S:0,5.556;1,7.778;2,255;3,22.222;4,33.333;5,44.444;6,0;7,1;>