sdcs_comm.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "plt.h"
  2. #include "sdcs_comm.h"
  3. #include "sdcs_sm.h"
  4. #include "sdcs.h"
  5. int sdcs_comm_init(int idx)
  6. {
  7. struct sdcs_t* dev = &sdcs[idx];
  8. struct comm_t* comm = &dev->comm;
  9. comm_set_state( comm, COMMST_ERR );
  10. }
  11. int sdcs_comm_reset(int idx)
  12. {
  13. struct sdcs_t* dev = &sdcs[idx];
  14. struct comm_t* comm = &dev->comm;
  15. comm_set_state( comm, COMMST_NORMAL );
  16. }
  17. void sdcs_comm_dac( int idx )
  18. {
  19. unsigned short tab_us[256]={0};
  20. struct sdcs_t* dev = &sdcs[idx];
  21. struct comm_t* comm = &dev->comm;
  22. int chanidx = comm->chanidx;
  23. int addr = comm->adr;
  24. int start, nb, rc, i;
  25. int ret = 0;
  26. if(comm_get_state(comm) != COMMST_NORMAL){
  27. return;
  28. }
  29. comm_start_cal_dac_timing(comm);
  30. nb = 0x0E;
  31. start = 0;
  32. chan_lock(comm->chanidx);
  33. if( chan_read_holdingregisters_with_retry( chanidx, addr, start, nb, tab_us) < 0){
  34. chan_unlock(comm->chanidx);
  35. comm_set_state(comm, COMMST_ERR);
  36. return;
  37. }else{ /* read ok */
  38. chan_unlock(comm->chanidx);
  39. dev->humi = tab_us[0x0001 - start]/10.0;
  40. dev->temp = *((SHORT*)&tab_us[0x0000 - start])/10.0;
  41. dev->start_humi = tab_us[0x0002 - start] / 10.0;
  42. dev->stop_humi = tab_us[0x0003 - start] / 10.0;
  43. if(tab_us[0x0004 - start] & 0x8000){
  44. dev->heat_temp = -(tab_us[0x0004 - start] - 0x8000) / 10.0;
  45. }else{
  46. dev->heat_temp = tab_us[0x0004 - start] / 10.0;
  47. }
  48. if(tab_us[0x0005 - start] & 0x8000){
  49. dev->alarm_temp = -(tab_us[0x0005 - start] - 0x8000) / 10.0;
  50. }else{
  51. dev->alarm_temp = tab_us[0x0005 - start] / 10.0;
  52. }
  53. dev->alarm_humi = tab_us[0x0006 - start] / 10.0;
  54. dev->heat_state = tab_us[0x0007 - start];
  55. dev->dehumi_state = tab_us[0x0008 - start];
  56. dev->dehumi_auto = tab_us[0x0009 - start];
  57. dev->dehumi_by_hand = tab_us[0x000A - start];
  58. dev->heat_by_hand = tab_us[0x000B - start];
  59. dev->over_temperature_alarm = tab_us[0x000C - start];
  60. dev->over_humi_alarm = tab_us[0x000D - start];
  61. comm_stop_cal_dac_timing(comm);
  62. }
  63. }
  64. int sdcs_set_dev_start_humi( int idx,double humi)
  65. {
  66. int ret = 0;
  67. struct sdcs_t* dev = &sdcs[idx];
  68. struct comm_t* comm = &dev->comm;
  69. unsigned short value = 0;
  70. value = humi * 10.0;
  71. chan_lock(comm->chanidx);
  72. ret = chan_write_single_register_with_retry( comm->chanidx, comm->adr, 0x0002, value);
  73. chan_unlock(comm->chanidx);
  74. log_dbg("%s, idx:%d", __func__, idx);
  75. return ret;
  76. }
  77. int sdcs_set_dev_start_temperature( int idx,double temperature)
  78. {
  79. int ret = 0;
  80. struct sdcs_t* dev = &sdcs[idx];
  81. struct comm_t* comm = &dev->comm;
  82. unsigned short value = 0;
  83. if(temperature < 0.0){
  84. value = (- temperature * 10.0) + 0x8000;
  85. }else{
  86. value = temperature * 10.0;
  87. }
  88. chan_lock(comm->chanidx);
  89. ret = chan_write_single_register_with_retry( comm->chanidx, comm->adr, 0x0003, value);
  90. chan_unlock(comm->chanidx);
  91. log_dbg("%s, idx:%d", __func__, idx);
  92. return ret;
  93. }
  94. int sdcs_set_dev_stop_temperature( int idx,double temperature)
  95. {
  96. int ret = 0;
  97. struct sdcs_t* dev = &sdcs[idx];
  98. struct comm_t* comm = &dev->comm;
  99. unsigned short value = 0;
  100. if(temperature < 0.0){
  101. value = (- temperature * 10.0) + 0x8000;
  102. }else{
  103. value = temperature * 10.0;
  104. }
  105. chan_lock(comm->chanidx);
  106. ret = chan_write_single_register_with_retry( comm->chanidx, comm->adr, 0x0004, value);
  107. chan_unlock(comm->chanidx);
  108. log_dbg("%s, idx:%d", __func__, idx);
  109. return ret;
  110. }
  111. int sdcs_set_dev_stop_auto( int idx,int val)
  112. {
  113. int ret = 0;
  114. struct sdcs_t* dev = &sdcs[idx];
  115. struct comm_t* comm = &dev->comm;
  116. chan_lock(comm->chanidx);
  117. ret = chan_write_single_register_with_retry( comm->chanidx, comm->adr, 0x0009, val);
  118. chan_unlock(comm->chanidx);
  119. log_dbg("%s, idx:%d", __func__, idx);
  120. return ret;
  121. }