Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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