tf_comm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "tf.h"
  2. #include "plt.h"
  3. static void tongfei_comm_0x0000(int idx)
  4. {
  5. unsigned short tab_us[128] = {0};
  6. struct tongfei_t *dev = &tf[idx];
  7. struct comm_t *comm = &dev->comm;
  8. int chanidx = comm->chanidx;
  9. int addr = comm->adr;
  10. int start, nb, rc, i;
  11. int ret = 0;
  12. if (comm_get_state(comm) != COMMST_NORMAL)
  13. {
  14. return;
  15. }
  16. nb = 33;
  17. start = 0x0000;
  18. chan_lock(chanidx);
  19. usleep(100000); // 100ms
  20. rc = chan_read_holdingregisters_with_retry(chanidx, addr, start, nb, tab_us);
  21. if (rc < 0)
  22. {
  23. comm_set_state(comm, COMMST_ERR);
  24. }
  25. chan_unlock(chanidx);
  26. if (rc == 0)
  27. { /* read ok */
  28. //0x0000
  29. dev->run_statue = tab_us[0]; //运行状态,0空闲 1准备启动 2运行 3 延时停机 4 故障
  30. if (dev->run_statue == 0)
  31. {
  32. sprintf(dev->run_state_str, "%s", "stop");
  33. }
  34. else if (dev->run_statue == 1)
  35. {
  36. sprintf(dev->run_state_str, "%s", "ready");
  37. }
  38. else if (dev->run_statue == 2)
  39. {
  40. sprintf(dev->run_state_str, "%s", "run");
  41. }
  42. else if (dev->run_statue == 3)
  43. {
  44. sprintf(dev->run_state_str, "%s", "delay stop");
  45. }
  46. else
  47. {
  48. sprintf(dev->run_state_str, "%s", "error");
  49. }
  50. dev->io_in= tab_us[1];
  51. dev->io_out= tab_us[2];
  52. dev->out_water_temp= tab_us[3] / 100.0; //进水口温度
  53. dev->in_water_temp= tab_us[4]/ 10.0; //出水口温度
  54. dev->in_evaporation_temp= tab_us[5]/ 10.0;//蒸发入口温度
  55. dev->out_evaporation_temp= tab_us[7]/ 10.0;//蒸发入口温度
  56. dev->ev_temp= tab_us[8] / 10.0; //环境温度
  57. dev->expansion_valve_degree= tab_us[11]; //膨胀阀当前开度
  58. dev->in_water_press= tab_us[18]/10.0; //进水口压力
  59. dev->out_water_press= tab_us[19]/10.0; //出水口压力
  60. dev->Err_1= tab_us[25];
  61. dev->Err_2= tab_us[26];
  62. dev->Err_3= tab_us[27];
  63. dev->Err_4= tab_us[28];
  64. dev->FrequencyChangerErr = tab_us[29];
  65. }
  66. else
  67. {
  68. return;
  69. }
  70. nb = 3;
  71. start = 0x1000;
  72. chan_lock(chanidx);
  73. usleep(100000); // 100ms
  74. rc = chan_read_holdingregisters_with_retry(chanidx, addr, start, nb, tab_us);
  75. if (rc < 0)
  76. {
  77. comm_set_state(comm, COMMST_ERR);
  78. }
  79. chan_unlock(chanidx);
  80. if (rc == 0)
  81. { /* read ok */
  82. //0x1000
  83. dev->runMode = tab_us[0x1001 - start];
  84. if (dev->runMode == 1)
  85. {
  86. sprintf(dev->runMode_str, "%s", "cool");
  87. }
  88. else if (dev->runMode == 2)
  89. {
  90. sprintf(dev->runMode_str, "%s", "heat");
  91. }
  92. else if (dev->runMode == 3)
  93. {
  94. sprintf(dev->runMode_str, "%s", "loop");
  95. }
  96. else if (dev->runMode == 4)
  97. {
  98. sprintf(dev->runMode_str, "%s", "auto");
  99. }
  100. dev->ctlMode = tab_us[0x1000 - start];
  101. if (dev->ctlMode == 0)
  102. {
  103. sprintf(dev->ctrMode_str, "%s", "manual ");
  104. }
  105. else if (dev->ctlMode == 1)
  106. {
  107. sprintf(dev->ctrMode_str, "%s", "auto");
  108. }
  109. else if (dev->ctlMode == 2)
  110. {
  111. sprintf(dev->ctrMode_str, "%s", "romote");
  112. }
  113. }
  114. else
  115. {
  116. return;
  117. }
  118. }
  119. int tongfei_set_dev_start(int idx)
  120. {
  121. int ret = 0;
  122. struct tongfei_t *dev = &tf[idx];
  123. struct comm_t *comm = &dev->comm;
  124. chan_lock(comm->chanidx);
  125. ret = chan_write_single_register_with_retry(comm->chanidx, comm->adr, 0x0300, 1);
  126. chan_unlock(comm->chanidx);
  127. log_dbg("%s, idx:%d", __func__, idx);
  128. log_info("----------------------------------%s, ret:%d,tongfei start", __func__, ret);
  129. return ret;
  130. }
  131. int tongfei_set_dev_stop(int idx)
  132. {
  133. int ret = 0;
  134. struct tongfei_t *dev = &tf[idx];
  135. struct comm_t *comm = &dev->comm;
  136. chan_lock(comm->chanidx);
  137. ret = chan_write_single_register_with_retry(comm->chanidx, comm->adr, 0x0300, 2);
  138. chan_unlock(comm->chanidx);
  139. log_dbg("%s, idx:%d", __func__, idx);
  140. return ret;
  141. }
  142. /*
  143. 范围[0,5]
  144. 0 停止,
  145. 1 制冷,
  146. 2 加热,
  147. 3 内循环
  148. 4 全 自 动 ( 根 据 水温)
  149. */
  150. int tongfei_set_dev_runmode(int idx, int mode)
  151. {
  152. int ret = 0;
  153. struct tongfei_t *dev = &tf[idx];
  154. struct comm_t *comm = &dev->comm;
  155. chan_lock(comm->chanidx);
  156. ret = chan_write_single_register_with_retry(comm->chanidx, comm->adr, 0x1001, mode);
  157. chan_unlock(comm->chanidx);
  158. log_dbg("%s, idx:%d", __func__, idx);
  159. return ret;
  160. }
  161. /*
  162. 范围[0,2]
  163. 0 命令方式启停,
  164. 1 制自动延时开启,
  165. 2 远程开启
  166. */
  167. int tongfei_set_dev_ctlmode(int idx, int mode)
  168. {
  169. int ret = 0;
  170. struct tongfei_t *dev = &tf[idx];
  171. struct comm_t *comm = &dev->comm;
  172. chan_lock(comm->chanidx);
  173. ret = chan_write_single_register_with_retry(comm->chanidx, comm->adr, 0x1000, mode);
  174. chan_unlock(comm->chanidx);
  175. log_dbg("%s, idx:%d", __func__, idx);
  176. return ret;
  177. }
  178. int tongfei_set_dev_mbadr(int idx, int val)
  179. {
  180. int ret = 0;
  181. struct tongfei_t *dev = &tf[idx];
  182. struct comm_t *comm = &dev->comm;
  183. chan_lock(comm->chanidx);
  184. ret = chan_write_single_register_with_retry(comm->chanidx, comm->adr, 0x100a, val);
  185. chan_unlock(comm->chanidx);
  186. log_dbg("%s, idx:%d, val:%d, ret:%d", __func__, idx, val, ret);
  187. return ret;
  188. }
  189. int tongfei_comm_init(int idx)
  190. {
  191. struct tongfei_t *dev = &tf[idx];
  192. struct comm_t *comm = &dev->comm;
  193. comm_set_state(comm, COMMST_ERR);
  194. }
  195. int tongfei_comm_reset(int idx)
  196. {
  197. struct tongfei_t *dev = &tf[idx];
  198. struct comm_t *comm = &dev->comm;
  199. comm_reset(comm);
  200. // comm_set_state( comm, COMMST_NORMAL );
  201. }
  202. void tongfei_comm_dac(int idx)
  203. {
  204. struct tongfei_t *dev = &tf[idx];
  205. struct comm_t *comm = &dev->comm;
  206. int ret = 0;
  207. if (comm_get_state(comm) != COMMST_NORMAL)
  208. {
  209. return;
  210. }
  211. comm_start_cal_dac_timing(comm);
  212. tongfei_comm_0x0000(idx);
  213. }