256 lines
6.3 KiB
C++
256 lines
6.3 KiB
C++
#include "IOModulesInterface.h"
|
|
#include "GlobalDefine.h"
|
|
#include <QDebug>
|
|
|
|
|
|
CIOModulesInterface::CIOModulesInterface()
|
|
{
|
|
// mIOModulesQueryTimer = new QTimer();
|
|
// connect(mIOModulesQueryTimer,SIGNAL(timeout()),this,SLOT(IOModulesQueryTimerExpired()));
|
|
|
|
// mIOModulesQueryTimer->setInterval(IO_MODULES_REFRESH_INTERVAL);
|
|
// mIOModulesQueryTimer->setSingleShot(true);
|
|
|
|
hndl=0;
|
|
|
|
|
|
|
|
}
|
|
|
|
CIOModulesInterface::~CIOModulesInterface()
|
|
{
|
|
//delete mIOModulesQueryTimer;
|
|
}
|
|
|
|
|
|
int CIOModulesInterface::OpenIOModules(QString IPAddress)
|
|
{
|
|
|
|
int ret = SM_Open(&hndl,IPAddress.toAscii().data());
|
|
if(ret < 0)
|
|
{
|
|
qDebug("Impossible d'ouvrir le module d'I/O, erreur %d",ret);
|
|
return RET_ERROR;
|
|
}
|
|
|
|
qDebug("Module d'I/O ouvert avec succès");
|
|
|
|
DeviceConfig config;
|
|
SM_SelectDevice(hndl,INPUTS_MODULE_1_SLAVE_ID);
|
|
SM_GetDeviceConfig(hndl,&config);
|
|
qDebug("Module inputs 1: Address %d, Modèle %d",INPUTS_MODULE_1_SLAVE_ID, config.model);
|
|
|
|
SM_SelectDevice(hndl,INPUTS_MODULE_2_SLAVE_ID);
|
|
SM_GetDeviceConfig(hndl,&config);
|
|
qDebug("Module inputs 1: Address %d, Modèle %d",INPUTS_MODULE_2_SLAVE_ID, config.model);
|
|
|
|
SM_SelectDevice(hndl,INPUTS_MODULE_3_SLAVE_ID);
|
|
SM_GetDeviceConfig(hndl,&config);
|
|
qDebug("Module inputs 1: Address %d, Modèle %d",INPUTS_MODULE_3_SLAVE_ID, config.model);
|
|
|
|
SM_SelectDevice(hndl,INPUTS_MODULE_4_SLAVE_ID);
|
|
SM_GetDeviceConfig(hndl,&config);
|
|
qDebug("Module inputs 1: Address %d, Modèle %d",INPUTS_MODULE_4_SLAVE_ID, config.model);
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_1_SLAVE_ID);
|
|
SM_GetDeviceConfig(hndl,&config);
|
|
qDebug("Module inputs 1: Address %d, Modèle %d",OUTPUTS_MODULE_1_SLAVE_ID, config.model);
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_2_SLAVE_ID);
|
|
SM_GetDeviceConfig(hndl,&config);
|
|
qDebug("Module inputs 1: Address %d, Modèle %d",OUTPUTS_MODULE_2_SLAVE_ID, config.model);
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_3_SLAVE_ID);
|
|
SM_GetDeviceConfig(hndl,&config);
|
|
qDebug("Module inputs 1: Address %d, Modèle %d",OUTPUTS_MODULE_3_SLAVE_ID, config.model);
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_4_SLAVE_ID);
|
|
SM_GetDeviceConfig(hndl,&config);
|
|
qDebug("Module inputs 1: Address %d, Modèle %d",OUTPUTS_MODULE_4_SLAVE_ID, config.model);
|
|
|
|
ResetOutputs();
|
|
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
int CIOModulesInterface::CloseIOModules()
|
|
{
|
|
if(hndl == 0)
|
|
return RET_ERROR;
|
|
|
|
// mIOModulesQueryTimer->stop();
|
|
int ret = SM_Close(&hndl);
|
|
|
|
if(ret < 0)
|
|
{
|
|
qDebug("Impossible de fermer le module d'I/O, erreur %d",ret);
|
|
return RET_ERROR;
|
|
}
|
|
|
|
qDebug("Module d'I/O fermé avec succès");
|
|
|
|
|
|
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
void CIOModulesInterface::IOModulesQueryTimerExpired()
|
|
{
|
|
|
|
DoAllIOTransactions();
|
|
// mIOModulesQueryTimer->start();
|
|
qDebug("timerout");
|
|
}
|
|
|
|
int CIOModulesInterface::ReadInputModules()
|
|
{
|
|
if(hndl == 0)
|
|
return RET_ERROR;
|
|
|
|
unsigned char TempBuf[4];
|
|
|
|
SM_SelectDevice(hndl,INPUTS_MODULE_1_SLAVE_ID);
|
|
SM_ReadDigitalInputs(hndl,0,32,&TempBuf[0]);
|
|
memcpy(&mInputsBuffer[0],&TempBuf[0],4);
|
|
|
|
SM_SelectDevice(hndl,INPUTS_MODULE_2_SLAVE_ID);
|
|
SM_ReadDigitalInputs(hndl,0,32,&TempBuf[0]);
|
|
memcpy(&mInputsBuffer[4],&TempBuf[0],4);
|
|
|
|
SM_SelectDevice(hndl,INPUTS_MODULE_3_SLAVE_ID);
|
|
SM_ReadDigitalInputs(hndl,0,32,&TempBuf[0]);
|
|
memcpy(&mInputsBuffer[8],&TempBuf[0],4);
|
|
|
|
SM_SelectDevice(hndl,INPUTS_MODULE_4_SLAVE_ID);
|
|
SM_ReadDigitalInputs(hndl,0,32,&TempBuf[0]);
|
|
memcpy(&mInputsBuffer[12],&TempBuf[0],4);
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
int CIOModulesInterface::WriteOutputModules()
|
|
{
|
|
if(hndl == 0)
|
|
return RET_ERROR;
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_1_SLAVE_ID);
|
|
SM_WriteDigitalOutputs(hndl,0,32,&mOutputsBuffer[0]);
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_2_SLAVE_ID);
|
|
SM_WriteDigitalOutputs(hndl,0,32,&mOutputsBuffer[4]);
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_3_SLAVE_ID);
|
|
SM_WriteDigitalOutputs(hndl,0,32,&mOutputsBuffer[8]);
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_4_SLAVE_ID);
|
|
SM_WriteDigitalOutputs(hndl,0,32,&mOutputsBuffer[12]);
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
int CIOModulesInterface::ReadOutputStates()
|
|
{
|
|
if(hndl == 0)
|
|
return RET_ERROR;
|
|
|
|
unsigned char TempBuf[4];
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_1_SLAVE_ID);
|
|
SM_ReadDigitalOutputs(hndl,0,32,&TempBuf[0]);
|
|
memcpy(&mOutputsStatebuffer[0],&TempBuf[0],4);
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_2_SLAVE_ID);
|
|
SM_ReadDigitalOutputs(hndl,0,32,&TempBuf[0]);
|
|
memcpy(&mOutputsStatebuffer[4],&TempBuf[0],4);
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_3_SLAVE_ID);
|
|
SM_ReadDigitalOutputs(hndl,0,32,&TempBuf[0]);
|
|
memcpy(&mOutputsStatebuffer[8],&TempBuf[0],4);
|
|
|
|
SM_SelectDevice(hndl,OUTPUTS_MODULE_4_SLAVE_ID);
|
|
SM_ReadDigitalOutputs(hndl,0,32,&TempBuf[0]);
|
|
memcpy(&mOutputsStatebuffer[12],&TempBuf[0],4);
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
int CIOModulesInterface::DoAllIOTransactions()
|
|
{
|
|
ReadInputModules();
|
|
WriteOutputModules();
|
|
ReadOutputStates();
|
|
return RET_OK;
|
|
}
|
|
|
|
int CIOModulesInterface::ResetOutputs()
|
|
{
|
|
memset(mOutputsBuffer,0x00,16);
|
|
return WriteOutputModules();
|
|
}
|
|
|
|
int CIOModulesInterface::SetOutputs(QBitArray Outputs)
|
|
{
|
|
if(Outputs.size() != IO_COUNT)
|
|
{
|
|
return RET_ERROR;
|
|
}
|
|
|
|
int IO = 0;
|
|
unsigned char mask = 1;
|
|
for(int byte = 0; byte < 16; byte++)
|
|
{
|
|
mOutputsBuffer[byte] = 0;
|
|
mask = 1;
|
|
for(int bit = 0; bit < 8; bit++)
|
|
{
|
|
if(Outputs.at(IO))
|
|
{
|
|
mOutputsBuffer[byte] |= mask;
|
|
}
|
|
mask <<= 1;
|
|
IO++;
|
|
}
|
|
}
|
|
|
|
WriteOutputModules();
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
QBitArray CIOModulesInterface::GetInputStates()
|
|
{
|
|
ReadInputModules();
|
|
return CharBufferToBitArray(mInputsBuffer, 16);
|
|
}
|
|
|
|
QBitArray CIOModulesInterface::GetOutputStates()
|
|
{
|
|
ReadOutputStates();
|
|
return CharBufferToBitArray(mOutputsStatebuffer, 16);
|
|
}
|
|
|
|
QBitArray CIOModulesInterface::CharBufferToBitArray(unsigned char *buf, int size_in_bytes)
|
|
{
|
|
int Size = size_in_bytes*8;
|
|
QBitArray Result(Size);
|
|
|
|
int IO = 0;
|
|
unsigned char mask = 1;
|
|
for(int byte = 0; byte < size_in_bytes; byte++)
|
|
{
|
|
mask = 1;
|
|
for(int bit = 0; bit < 8; bit++)
|
|
{
|
|
if((buf[byte] & mask) != 0)
|
|
{
|
|
Result.setBit(IO);
|
|
}
|
|
mask <<= 1;
|
|
IO++;
|
|
}
|
|
}
|
|
return Result;
|
|
}
|