CollimatorDevice.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "stdafx.h"
  2. #include "CommonFun.h"
  3. #include "CollimatorDevice.h"
  4. using namespace DIOS::Dev::Detail::Collimator;
  5. namespace nsSYN = DIOS::Dev::Detail::SYNBOX;
  6. //-----------------------------------------------------------------------------
  7. // DynBoxDevice
  8. //-----------------------------------------------------------------------------
  9. nsSYN::CollimatorDevice::CollimatorDevice(std::shared_ptr <IOEventCenter> center, nsSCF::SCF SCF)
  10. {
  11. m_SCF = SCF;
  12. EventCenter = center;
  13. m_CollimatorUnit.m_XSize.reset(new XSIZEMould(0, 0, 4300, 1));
  14. m_CollimatorUnit.m_YSize.reset(new YSIZEMould(0, 0, 4300, 1));
  15. m_CollimatorUnit.m_Filter.reset(new FILTERMould(0, 0, 5, 1));
  16. m_CollimatorUnit.m_SID.reset(new SIDMould(0, 0, 18000, 1));
  17. m_CollimatorUnit.m_Angle.reset(new ANGLEMould(0, 0, 360.0, 0.1));
  18. m_CollimatorUnit.m_Mode.reset(new MODEMould(0, 0, 6, 1));
  19. m_CollimatorUnit.m_Light.reset(new LIGHTMould(0, 0, 1, 1));
  20. }
  21. nsSYN::CollimatorDevice::~CollimatorDevice()
  22. {
  23. }
  24. void nsSYN::CollimatorDevice::Register(Dispatch* Dispatch)
  25. {
  26. super::Register(Dispatch);
  27. }
  28. void nsSYN::CollimatorDevice::OnCallback()
  29. {
  30. }
  31. RET_STATUS nsSYN::CollimatorDevice::SetCollimatorSize(float xproportion, float yproportion)
  32. {
  33. xproportion = xproportion * 100;
  34. yproportion = yproportion * 100;
  35. int cmdID = 0x641;
  36. unsigned char data[8];
  37. data[0] = 0x13;
  38. int nHX, nLX, nHY, nLY;
  39. nHX = ((int)xproportion >> 8) & 0xff;
  40. nLX = (int)yproportion & 0xff;
  41. nHY = ((int)xproportion >> 8) & 0xff;
  42. nLY = ((int)yproportion) & 0xff;
  43. data[1] = 0x03;
  44. data[2] = nLX; //X Y exchaned 20100420
  45. data[3] = nHX;
  46. data[4] = nLY;
  47. data[5] = nHY;
  48. //data[2] = 0xf1; //X Y exchaned 20100420
  49. //data[3] = 0x01;
  50. //data[4] = 0xf1;
  51. //data[5] = 0x01;
  52. data[6] = 0;//new public member
  53. data[7] = 0x0;
  54. SendCANBySCF(cmdID, &data[0], 8);
  55. Sleep(500);
  56. SendCANBySCF(cmdID, &data[0], 8);
  57. SetCollimatorLight(1);
  58. return RET_STATUS::RET_SUCCEED;
  59. }
  60. RET_STATUS nsSYN::CollimatorDevice::SetCollimatorSID(unsigned short sid)
  61. {
  62. int nSID = sid * 100;
  63. int cmdID = 0x641;
  64. unsigned char data[3];
  65. data[0] = 0x11;
  66. int nHSID, nLSID;
  67. nHSID = ((int)nSID >> 8) & 0xff;
  68. nLSID = (int)nSID & 0xff;
  69. data[1] = nLSID; //X Y exchaned 20100420
  70. data[2] = nHSID;
  71. SendCANBySCF(cmdID, &data[0], 3);
  72. Sleep(500);
  73. SendCANBySCF(cmdID, &data[0], 3);
  74. return RET_STATUS::RET_SUCCEED;
  75. }
  76. RET_STATUS nsSYN::CollimatorDevice::SetCollimatorFilter(unsigned short pParams)
  77. {
  78. int cmdID = 0x641;
  79. unsigned char data[8];
  80. data[0] = 0x13;
  81. int nHX, nLX, nHY, nLY, nFilter;
  82. nHX = (m_CollimatorUnit.m_XSize->Get() >> 8) & 0xff;
  83. nLX = m_CollimatorUnit.m_XSize->Get() & 0xff;
  84. nHY = (m_CollimatorUnit.m_YSize->Get() >> 8) & 0xff;
  85. nLY = (m_CollimatorUnit.m_YSize->Get()) & 0xff;
  86. nFilter = pParams;
  87. data[1] = 0x40;
  88. data[2] = nLX; //X Y exchaned 20100420
  89. data[3] = nHX;
  90. data[4] = nLY;
  91. data[5] = nHY;
  92. data[6] = nFilter;//new public member
  93. data[7] = 0x0;
  94. SendCANBySCF(cmdID, &data[0], 8);
  95. Sleep(500);
  96. SendCANBySCF(cmdID, &data[0], 8);
  97. return RET_STATUS::RET_SUCCEED;
  98. }
  99. RET_STATUS nsSYN::CollimatorDevice::SetCollimatorAngle(float pParams)
  100. {
  101. return RET_STATUS::RET_SUCCEED;
  102. }
  103. RET_STATUS nsSYN::CollimatorDevice::SetCollimatorMode(unsigned short pParams)
  104. {
  105. int cmdID = 0x41;
  106. unsigned char data[8];
  107. data[0] = 0x1;
  108. data[1] = 0x0;
  109. data[2] = 0x6; //X Y exchaned 20100420
  110. SendCANBySCF(cmdID, &data[0], 3);
  111. return RET_STATUS::RET_SUCCEED;
  112. }
  113. RET_STATUS nsSYN::CollimatorDevice::SetCollimatorLight(unsigned short pParams)
  114. {
  115. int cmdID = 0x641;
  116. unsigned char data[3];
  117. data[0] = 0x18;
  118. data[1] = 2; //X Y exchaned 20100420
  119. data[2] = 0;
  120. SendCANBySCF(cmdID, &data[0], 3);
  121. return RET_STATUS::RET_SUCCEED;
  122. }
  123. int nsSYN::CollimatorDevice::DealReceiveData(string sCmdID, const char* pData, int Datalen)
  124. {
  125. if (strtol(("0x" + sCmdID).c_str(), NULL, 16) == 0x626)
  126. {
  127. string strCmd = pData;
  128. if (strtol(("0x" + strCmd.substr(0, 2)).c_str(), NULL, 16) == 0x14)
  129. {
  130. bool bUpdateSize = false, bUpdateFilter = false;
  131. if (strtol(("0x" + strCmd.substr(3, 1)).c_str(), NULL, 16) & 0x01)
  132. {
  133. int nXSize = strtol(("0x" + strCmd.substr(4, 2)).c_str(), NULL, 16) + strtol(("0x" + strCmd.substr(6, 2)).c_str(), NULL, 16) * 256;
  134. bUpdateSize = true;
  135. nXSize = nXSize / 100;
  136. if (m_CollimatorUnit.m_XSize->Update(nXSize))
  137. {
  138. mLog::Info("m_nXSize = {$}", nXSize);
  139. FireNotify(m_CollimatorUnit.m_XSize->GetKey(), m_CollimatorUnit.m_XSize->JSGet());
  140. }
  141. }
  142. if (strtol(("0x" + strCmd.substr(3, 1)).c_str(), NULL, 16) & 0x02)
  143. {
  144. int nYSize = strtol(("0x" + strCmd.substr(8, 2)).c_str(), NULL, 16) + strtol(("0x" + strCmd.substr(10, 2)).c_str(), NULL, 16) * 256;
  145. nYSize = nYSize / 100;
  146. bUpdateSize = true;
  147. if (m_CollimatorUnit.m_YSize->Update(nYSize))
  148. {
  149. mLog::Info("m_nYSize = {$}", nYSize);
  150. FireNotify(m_CollimatorUnit.m_YSize->GetKey(), m_CollimatorUnit.m_YSize->JSGet());
  151. }
  152. }
  153. if (strtol(("0x" + strCmd.substr(2, 1)).c_str(), NULL, 16) & 0x04)
  154. {
  155. int nFilter = strtol(("0x" + strCmd.substr(12, 2)).c_str(), NULL, 16) + strtol(("0x" + strCmd.substr(14, 2)).c_str(), NULL, 16) * 256;
  156. bUpdateFilter = true;
  157. //printf("m_nFilter = %d\n", nFilter);
  158. if (m_CollimatorUnit.m_Filter->Update(nFilter))
  159. {
  160. mLog::Info("m_nFilter = {$}", nFilter);
  161. FireNotify(m_CollimatorUnit.m_Filter->GetKey(), m_CollimatorUnit.m_Filter->JSGet());
  162. }
  163. }
  164. }
  165. }
  166. return 1;
  167. }
  168. bool nsSYN::CollimatorDevice::SendCANBySCF(int cmdID, unsigned char* cmd, int cmdlen)
  169. {
  170. int nTimeout = 100;
  171. char cCmd[MAX_COMMAND_LEN] = "STLB";
  172. char str[20];
  173. sprintf_s(str, 3, "%02x", 10 + cmdlen * 2);
  174. cCmd[4] = str[0];
  175. cCmd[5] = str[1];
  176. sprintf_s(str, 10, "%08x", cmdID);
  177. for (int i = 0; i < 8; i++)
  178. {
  179. cCmd[6 + i] = str[i];
  180. }
  181. string CANProtocol = (string)ResDYNConfig["CANPortocol"];
  182. cCmd[14] = CANProtocol[0];
  183. sprintf_s(str, 4, "%x", cmdlen);
  184. cCmd[15] = str[0];
  185. for (int j = 0; j < cmdlen; j++)
  186. {
  187. sprintf_s(str, 4, "%02x", cmd[j]);
  188. cCmd[16 + (2 * j)] = str[0];
  189. cCmd[16 + (2 * j + 1)] = str[1];
  190. }
  191. int nCmdSize = 16 + 2 * cmdlen;
  192. FormatCmd(cCmd, nCmdSize);
  193. string strLog;
  194. CmdtoString(cCmd, nCmdSize, strLog);
  195. //PRINTA_INFO(m_pLog, "[Send:%s]", strLog.c_str());
  196. mLog::Info("==OUT==: {$}", cCmd);
  197. int ret = 0;
  198. m_SCF.Lock(msTimeOut_Lock)
  199. .SendPacket(cCmd, nCmdSize, nTimeout, ret);
  200. Sleep(nTimeout);
  201. return true;
  202. }
  203. void nsSYN::CollimatorDevice::FireNotify(std::string key, std::string content)
  204. {
  205. EventCenter->OnNotify(1, key, content);
  206. }