Module_DcMeter.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Module_DcMeter.c
  3. *
  4. * Created on: 2021/5/31
  5. * Author: foluswen
  6. */
  7. #include "Module_DcMeter.h"
  8. #include "meterComm.h"
  9. /**
  10. * Initial share memory
  11. * @return
  12. */
  13. int InitShareMemory()
  14. {
  15. int result = PASS;
  16. int MeterSMId;
  17. //Initialize ShmSysConfigAndInfo
  18. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  19. {
  20. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  21. result = FAIL;
  22. }
  23. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  24. {
  25. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  26. result = FAIL;
  27. }
  28. else
  29. {}
  30. //Initialize ShmStatusCodeData
  31. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  32. {
  33. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  34. result = FAIL;
  35. }
  36. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  37. {
  38. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  39. result = FAIL;
  40. }
  41. else
  42. {}
  43. return result;
  44. }
  45. //==========================================
  46. // Main loop
  47. //==========================================
  48. int main(void)
  49. {
  50. Meter_Info meter_info;
  51. uint8_t pollingIndex = 0;
  52. uint8_t failCount = 0;
  53. #ifndef DEBUG_STANDALONG
  54. // Initialize share memory
  55. if(InitShareMemory() == FAIL)
  56. {
  57. DEBUG_ERROR("InitShareMemory NG\n");
  58. if(ShmStatusCodeData!=NULL)
  59. {
  60. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
  61. }
  62. sleep(5);
  63. return -1;
  64. }
  65. #endif//DEBUG_STANDALONG
  66. // Initialize DC meter model
  67. /*
  68. * TODO:
  69. * 1. Maybe need to parse configuration by model name
  70. */
  71. meterInitialize(METER_MODEL_LEM_L18005A);
  72. // Main loop
  73. for(;;)
  74. {
  75. switch(pollingIndex)
  76. {
  77. case 0:
  78. if(readCurrent(&meter_info))
  79. {
  80. #ifndef DEBUG_STANDALONG
  81. /*
  82. * TODO:
  83. * 1. Synchronize data to share memory
  84. */
  85. #else
  86. DEBUG_INFO("Output current: %.3f A\n", meter_info.presentCurrent);
  87. #endif//DEBUG_STANDALONG
  88. pollingIndex++;
  89. failCount = 0;
  90. }
  91. else
  92. {
  93. if(failCount < 10)
  94. failCount++;
  95. }
  96. break;
  97. case 1:
  98. if(readVoltage(&meter_info))
  99. {
  100. #ifndef DEBUG_STANDALONG
  101. /*
  102. * TODO:
  103. * 1. Synchronize data to share memory
  104. */
  105. #else
  106. DEBUG_INFO("Output voltage: %.3f V\n", meter_info.presetVoltage);
  107. #endif//DEBUG_STANDALONG
  108. pollingIndex++;
  109. failCount = 0;
  110. }
  111. else
  112. {
  113. if(failCount < 10)
  114. failCount++;
  115. }
  116. break;
  117. case 2:
  118. if(readPower(&meter_info))
  119. {
  120. #ifndef DEBUG_STANDALONG
  121. /*
  122. * TODO:
  123. * 1. Synchronize data to share memory
  124. */
  125. #else
  126. DEBUG_INFO("Output power: %.3f kw\n", meter_info.presentPower);
  127. #endif//DEBUG_STANDALONG
  128. pollingIndex++;
  129. failCount = 0;
  130. }
  131. else
  132. {
  133. if(failCount < 10)
  134. failCount++;
  135. }
  136. break;
  137. case 3:
  138. if(readEnergy(&meter_info))
  139. {
  140. #ifndef DEBUG_STANDALONG
  141. /*
  142. * TODO:
  143. * 1. Synchronize data to share memory
  144. */
  145. #else
  146. DEBUG_INFO("Totalize import energy: %.3f kwh\n", meter_info.totlizeImportEnergy);
  147. DEBUG_INFO("Totalize export energy: %.3f kwh\n", meter_info.totlizeExportEnergy);
  148. #endif//DEBUG_STANDALONG
  149. pollingIndex++;
  150. failCount = 0;
  151. }
  152. else
  153. {
  154. if(failCount < 10)
  155. failCount++;
  156. }
  157. break;
  158. default:
  159. pollingIndex = 0;
  160. break;
  161. }
  162. if(failCount >= 10)
  163. {
  164. #ifndef DEBUG_STANDALONG
  165. if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MeterCommTimeout)
  166. {
  167. DEBUG_ERROR("Meter communication timeout");
  168. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MeterCommTimeout = ON;
  169. }
  170. #endif//DEBUG_STANDALONG
  171. }
  172. else
  173. {
  174. #ifndef DEBUG_STANDALONG
  175. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MeterCommTimeout = OFF;
  176. #endif//DEBUG_STANDALONG
  177. }
  178. usleep(500000);
  179. }
  180. return -1;
  181. }