You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

241 lines
6.1 KiB

  1. #include "IOModulesInterface.h"
  2. #include "GlobalDefine.h"
  3. #include <QDebug>
  4. CIOModulesInterface::CIOModulesInterface()
  5. {
  6. // mIOModulesQueryTimer = new QTimer();
  7. // connect(mIOModulesQueryTimer,SIGNAL(timeout()),this,SLOT(IOModulesQueryTimerExpired()));
  8. // mIOModulesQueryTimer->setInterval(IO_MODULES_REFRESH_INTERVAL);
  9. // mIOModulesQueryTimer->setSingleShot(true);
  10. }
  11. CIOModulesInterface::~CIOModulesInterface()
  12. {
  13. //delete mIOModulesQueryTimer;
  14. }
  15. int CIOModulesInterface::OpenIOModules()
  16. {
  17. int ret = SM_Open(&hndl,(char*)"192.168.0.52");
  18. if(ret < 0)
  19. {
  20. qDebug("Impossible d'ouvrir le module d'I/O, erreur %d",ret);
  21. return RET_ERROR;
  22. }
  23. qDebug("Module d'I/O ouvert avec succès");
  24. DeviceConfig config;
  25. SM_SelectDevice(hndl,INPUTS_MODULE_1_SLAVE_ID);
  26. SM_GetDeviceConfig(hndl,&config);
  27. qDebug("Module inputs 1: Address %d, Modèle %d",INPUTS_MODULE_1_SLAVE_ID, config.model);
  28. SM_SelectDevice(hndl,INPUTS_MODULE_2_SLAVE_ID);
  29. SM_GetDeviceConfig(hndl,&config);
  30. qDebug("Module inputs 1: Address %d, Modèle %d",INPUTS_MODULE_2_SLAVE_ID, config.model);
  31. SM_SelectDevice(hndl,INPUTS_MODULE_3_SLAVE_ID);
  32. SM_GetDeviceConfig(hndl,&config);
  33. qDebug("Module inputs 1: Address %d, Modèle %d",INPUTS_MODULE_3_SLAVE_ID, config.model);
  34. SM_SelectDevice(hndl,INPUTS_MODULE_4_SLAVE_ID);
  35. SM_GetDeviceConfig(hndl,&config);
  36. qDebug("Module inputs 1: Address %d, Modèle %d",INPUTS_MODULE_4_SLAVE_ID, config.model);
  37. SM_SelectDevice(hndl,OUTPUTS_MODULE_1_SLAVE_ID);
  38. SM_GetDeviceConfig(hndl,&config);
  39. qDebug("Module inputs 1: Address %d, Modèle %d",OUTPUTS_MODULE_1_SLAVE_ID, config.model);
  40. SM_SelectDevice(hndl,OUTPUTS_MODULE_2_SLAVE_ID);
  41. SM_GetDeviceConfig(hndl,&config);
  42. qDebug("Module inputs 1: Address %d, Modèle %d",OUTPUTS_MODULE_2_SLAVE_ID, config.model);
  43. SM_SelectDevice(hndl,OUTPUTS_MODULE_3_SLAVE_ID);
  44. SM_GetDeviceConfig(hndl,&config);
  45. qDebug("Module inputs 1: Address %d, Modèle %d",OUTPUTS_MODULE_3_SLAVE_ID, config.model);
  46. SM_SelectDevice(hndl,OUTPUTS_MODULE_4_SLAVE_ID);
  47. SM_GetDeviceConfig(hndl,&config);
  48. qDebug("Module inputs 1: Address %d, Modèle %d",OUTPUTS_MODULE_4_SLAVE_ID, config.model);
  49. ResetOutputs();
  50. return RET_OK;
  51. }
  52. int CIOModulesInterface::CloseIOModules()
  53. {
  54. // mIOModulesQueryTimer->stop();
  55. int ret = SM_Close(&hndl);
  56. if(ret < 0)
  57. {
  58. qDebug("Impossible de fermer le module d'I/O, erreur %d",ret);
  59. return RET_ERROR;
  60. }
  61. qDebug("Module d'I/O fermé avec succès");
  62. return RET_OK;
  63. }
  64. void CIOModulesInterface::IOModulesQueryTimerExpired()
  65. {
  66. DoAllIOTransactions();
  67. // mIOModulesQueryTimer->start();
  68. qDebug("timerout");
  69. }
  70. int CIOModulesInterface::ReadInputModules()
  71. {
  72. unsigned char TempBuf[4];
  73. SM_SelectDevice(hndl,INPUTS_MODULE_1_SLAVE_ID);
  74. SM_ReadDigitalInputs(hndl,0,32,&TempBuf[0]);
  75. memcpy(&mInputsBuffer[0],&TempBuf[0],4);
  76. SM_SelectDevice(hndl,INPUTS_MODULE_2_SLAVE_ID);
  77. SM_ReadDigitalInputs(hndl,0,32,&TempBuf[0]);
  78. memcpy(&mInputsBuffer[4],&TempBuf[0],4);
  79. SM_SelectDevice(hndl,INPUTS_MODULE_3_SLAVE_ID);
  80. SM_ReadDigitalInputs(hndl,0,32,&TempBuf[0]);
  81. memcpy(&mInputsBuffer[8],&TempBuf[0],4);
  82. SM_SelectDevice(hndl,INPUTS_MODULE_4_SLAVE_ID);
  83. SM_ReadDigitalInputs(hndl,0,32,&TempBuf[0]);
  84. memcpy(&mInputsBuffer[12],&TempBuf[0],4);
  85. return RET_OK;
  86. }
  87. int CIOModulesInterface::WriteOutputModules()
  88. {
  89. SM_SelectDevice(hndl,OUTPUTS_MODULE_1_SLAVE_ID);
  90. SM_WriteDigitalOutputs(hndl,0,32,&mOutputsBuffer[0]);
  91. SM_SelectDevice(hndl,OUTPUTS_MODULE_2_SLAVE_ID);
  92. SM_WriteDigitalOutputs(hndl,0,32,&mOutputsBuffer[4]);
  93. SM_SelectDevice(hndl,OUTPUTS_MODULE_3_SLAVE_ID);
  94. SM_WriteDigitalOutputs(hndl,0,32,&mOutputsBuffer[8]);
  95. SM_SelectDevice(hndl,OUTPUTS_MODULE_4_SLAVE_ID);
  96. SM_WriteDigitalOutputs(hndl,0,32,&mOutputsBuffer[12]);
  97. return RET_OK;
  98. }
  99. int CIOModulesInterface::ReadOutputStates()
  100. {
  101. unsigned char TempBuf[4];
  102. SM_SelectDevice(hndl,OUTPUTS_MODULE_1_SLAVE_ID);
  103. SM_ReadDigitalOutputs(hndl,0,32,&TempBuf[0]);
  104. memcpy(&mOutputsStatebuffer[0],&TempBuf[0],4);
  105. SM_SelectDevice(hndl,OUTPUTS_MODULE_2_SLAVE_ID);
  106. SM_ReadDigitalOutputs(hndl,0,32,&TempBuf[0]);
  107. memcpy(&mOutputsStatebuffer[4],&TempBuf[0],4);
  108. SM_SelectDevice(hndl,OUTPUTS_MODULE_3_SLAVE_ID);
  109. SM_ReadDigitalOutputs(hndl,0,32,&TempBuf[0]);
  110. memcpy(&mOutputsStatebuffer[8],&TempBuf[0],4);
  111. SM_SelectDevice(hndl,OUTPUTS_MODULE_4_SLAVE_ID);
  112. SM_ReadDigitalOutputs(hndl,0,32,&TempBuf[0]);
  113. memcpy(&mOutputsStatebuffer[12],&TempBuf[0],4);
  114. return RET_OK;
  115. }
  116. int CIOModulesInterface::DoAllIOTransactions()
  117. {
  118. ReadInputModules();
  119. WriteOutputModules();
  120. ReadOutputStates();
  121. return RET_OK;
  122. }
  123. int CIOModulesInterface::ResetOutputs()
  124. {
  125. memset(mOutputsBuffer,0x00,16);
  126. return WriteOutputModules();
  127. }
  128. int CIOModulesInterface::SetOutputs(QBitArray Outputs)
  129. {
  130. if(Outputs.size() != IO_COUNT)
  131. {
  132. return RET_ERROR;
  133. }
  134. int IO = 0;
  135. unsigned char mask = 1;
  136. for(int byte = 0; byte < 16; byte++)
  137. {
  138. mOutputsBuffer[byte] = 0;
  139. mask = 1;
  140. for(int bit = 0; bit < 8; bit++)
  141. {
  142. if(Outputs.at(IO))
  143. {
  144. mOutputsBuffer[byte] |= mask;
  145. }
  146. mask <<= 1;
  147. IO++;
  148. }
  149. }
  150. WriteOutputModules();
  151. return RET_OK;
  152. }
  153. QBitArray CIOModulesInterface::GetInputStates()
  154. {
  155. ReadInputModules();
  156. return CharBufferToBitArray(mInputsBuffer, 16);
  157. }
  158. QBitArray CIOModulesInterface::GetOutputStates()
  159. {
  160. ReadOutputStates();
  161. return CharBufferToBitArray(mOutputsStatebuffer, 16);
  162. }
  163. QBitArray CIOModulesInterface::CharBufferToBitArray(unsigned char *buf, int size_in_bytes)
  164. {
  165. int Size = size_in_bytes*8;
  166. QBitArray Result(Size);
  167. int IO = 0;
  168. unsigned char mask = 1;
  169. for(int byte = 0; byte < size_in_bytes; byte++)
  170. {
  171. mask = 1;
  172. for(int bit = 0; bit < 8; bit++)
  173. {
  174. if((buf[byte] & mask) != 0)
  175. {
  176. Result.setBit(IO);
  177. }
  178. mask <<= 1;
  179. IO++;
  180. }
  181. }
  182. return Result;
  183. }