diff --git a/CmakeLists.txt b/CmakeLists.txt index ce99ed3..881fe07 100644 --- a/CmakeLists.txt +++ b/CmakeLists.txt @@ -18,6 +18,7 @@ include_directories("source/Logger") include_directories("source/ZZ_SDK") include_directories("source/Settings") include_directories("source/Thread") +include_directories("source/LinearShutter") add_executable( TowerOptoSifAndSpectral main.cpp diff --git a/config/DeviceSettings.ini b/config/DeviceSettings.ini new file mode 100644 index 0000000..7ca5023 --- /dev/null +++ b/config/DeviceSettings.ini @@ -0,0 +1,20 @@ +[DEVICE INFO] +TotalSpectrometer=2 +[FS1] +Model=ISIF +Port=2 +DUID=test1 +[FS2] +Model=IS1 +Port=3 +DUID=test2 +[LINEAR SHUTTER] +Port=4 +Type=0 +DCID=1 +TotalPosition=5 +Position0=0 +Position1=200000 +Position2=300000 +Position3=400000 +Position4=500000 diff --git a/config/DeviceSettings.txt b/config/DeviceSettings.txt deleted file mode 100644 index 4c090cf..0000000 --- a/config/DeviceSettings.txt +++ /dev/null @@ -1,14 +0,0 @@ -[DEVICE INFO] -TotalSpectrometer=1 -[FS1] -Model=ISIF -Port=2 -UID= -[FS2] -Model=IS1 -Port=3 -UID= -[LinearShutter] -TotalPosition=2 -Position0=100000 -Position1=200000 diff --git a/main.cpp b/main.cpp index 8735b14..fd0e5c7 100644 --- a/main.cpp +++ b/main.cpp @@ -16,9 +16,24 @@ int main(int argc, char *argv[]) //////////////////////////////////////////////////////////////////////////configger ZZ_SysConfigger test; + RunTimeGrabberParams testp; test.Initialize(); test.LoadSettingsFromFile_System(); + test.GetGrabberRunParams(testp); //////////////////////////////////////////////////////////////////////////test + using namespace ZZ_MISCDEF::ZZ_RUNPARAMS; + CMainDataGrabber testDG; + QThread* m_pqDataGrabberThreadHolder = new QThread(); + testDG.moveToThread(m_pqDataGrabberThreadHolder); + testDG.SetGrabberParams(testp); + testDG.InitThreadStatus(); + testDG.InitializeWorkers(); + testDG.SetupMsgPipelines(); + testDG.StartWorkers(); + m_pqDataGrabberThreadHolder->start(); + + testDG.StartGrab(); + //QString qstrTest = "This is a test message 2"; //qDebug() << qstrTest; //cout << "This is a test message"; @@ -29,8 +44,7 @@ int main(int argc, char *argv[]) //m_test.GrabOnce(); //QThread* m_pqDataGrabberThreadHolder; - //m_pqDataGrabberThreadHolder = new QThread(); - //m_test.moveToThread(m_pqDataGrabberThreadHolder); + //m_pqDataGrabberThreadHolder->start(); //m_test.TestGrabOnce(); //m_test.StartGrabCall(); diff --git a/source/FS/ZZ_Types.h b/source/FS/ZZ_Types.h index 4040fdd..7ad1c54 100644 --- a/source/FS/ZZ_Types.h +++ b/source/FS/ZZ_Types.h @@ -5,6 +5,7 @@ #include "pch.h" #include #define MAX_DEVICENUMBER_FS 2 +#define MAX_LINEARSHUTTER_POSITION 12 namespace ZZ_MISCDEF { typedef unsigned char ZZ_U8; @@ -166,11 +167,20 @@ namespace ZZ_MISCDEF //运行参数 namespace ZZ_RUNPARAMS { - typedef struct tagDeviceContext + typedef struct tagFiberSpecContext { - ZZ_U8 ucDeviceNumber; - ZZ_U8 ucDeviceModel[MAX_DEVICENUMBER_FS]; - }DeviceContext; + ZZ_U8 ucDeviceNumber; + ZZ_U8 ucDeviceModel[MAX_DEVICENUMBER_FS]; + int iInterface[MAX_DEVICENUMBER_FS]; + std::string strSN[MAX_DEVICENUMBER_FS]; + }FSContext; + + typedef struct tagLinearShutterContext + { + ZZ_U8 ucPort; + ZZ_U8 ucProtocolType; + ZZ_U8 ucCmdID; + }LSContext; typedef struct tagAcquisitionTimeSettings { @@ -179,22 +189,26 @@ namespace ZZ_MISCDEF QTime qtInterval; }AcqTimeSettings; + typedef struct tagAcquisitionPositionSettings + { + int iTotalPosition; + int iPosition[MAX_LINEARSHUTTER_POSITION]; + }AcqPosSettings; + typedef struct tagRunTimeGrabberParams { - DeviceContext dcParams; + LSContext lscParam; + FSContext fscParams; AcqTimeSettings atsParams; + AcqPosSettings apsParams; }RunTimeGrabberParams; - typedef struct tagATPCalibrationSettings { //Up0 Down1,2,3 QString qsISIF_CalibrationFilePath[4]; QString qsIS1_CalibrationFilePath[4]; - }ATPCalibrationSettings; - - } //系统数据文件结构 diff --git a/source/LinearShutter/AbstractPort.h b/source/LinearShutter/AbstractPort.h new file mode 100644 index 0000000..afa4d50 --- /dev/null +++ b/source/LinearShutter/AbstractPort.h @@ -0,0 +1,16 @@ +#pragma once +#include "VSMD12XMiscDefines.h" +#include +class CAbstractPort +{ +public: + virtual void InitPort(PortInfo portinfo) = 0; + virtual bool SetPort() = 0; + virtual bool Open() = 0; + virtual void Close() = 0; + + virtual std::string ReadAll() = 0; + virtual int Write(const std::string strSend) = 0; + +}; + diff --git a/source/LinearShutter/VSMD12XControl.cpp b/source/LinearShutter/VSMD12XControl.cpp new file mode 100644 index 0000000..d8c0d1d --- /dev/null +++ b/source/LinearShutter/VSMD12XControl.cpp @@ -0,0 +1,1323 @@ +#include "pch.h" +#include "VSMD12XControl.h" +#include "ZZ_SeiralPort.h" +#include + +CVSMD12XControl::CVSMD12XControl() +{ + m_iProtocolType = 0; + m_iCID = 1; + m_bIsSyncMove = 0; + m_piPositionInPulses = NULL; +} + +CVSMD12XControl::~CVSMD12XControl() +{ + if (m_piPositionInPulses!=NULL) + { + delete[] m_piPositionInPulses; + } +} + +bool CVSMD12XControl::ILMES_InitializeComm(PortInfo portinfo, int iPortType /*= 1*/, int iCID /*= 1*/, bool bSyncMove /*= true*/) +{ + SetProtocolType(iPortType); + SetCID(iCID); + SetSyncMode(bSyncMove); + + return InitAndOpenPort(portinfo); +} + +bool CVSMD12XControl::ILMES_InitializeParams(ControllerParams cpParams) +{ + bool bRes = false; + + bRes = CVSMD12XControl::SetBaudRate(cpParams.bdr); + if (!bRes) + { + qDebug() << "SetBaudRate Err"; + return false; + } + + bRes = CVSMD12XControl::SetSpeed(cpParams.spd,cpParams.acc,cpParams.dec); + if (!bRes) + { + qDebug() << "SetSpeed Err"; + return false; + } + + bRes = CVSMD12XControl::SetMicroSteps(cpParams.mcs); + if (!bRes) + { + qDebug() << "SetMicroSteps Err"; + return false; + } + + bRes = CVSMD12XControl::SetCurrent(cpParams.cra,cpParams.crn,cpParams.crh); + if (!bRes) + { + qDebug() << "SetCurrent Err"; + return false; + } + + bRes = CVSMD12XControl::SetZeroParams(cpParams.zmd,cpParams.osv, cpParams.snr,cpParams.zsd,cpParams.zsp); + if (!bRes) + { + qDebug() << "SetZeroParams Err"; + return false; + } + return bRes; +} + +void CVSMD12XControl::ILMES_SetPosition(int *piPositionInPulse, size_t szSize) +{ + m_piPositionInPulses = new int[szSize]; + for (size_t i=0;i< szSize;i++) + { + m_piPositionInPulses[i] = piPositionInPulse[i]; + } +} + +bool CVSMD12XControl::ILMES_MoveToPos(int iPositionIndex) +{ + bool bRes = false; + bRes = CVSMD12XControl::StartBackZero(); + if (!bRes) + { + qDebug() << "StartBackZero Err"; + return false; + } + + bRes = CVSMD12XControl::MoveTo(m_piPositionInPulses[iPositionIndex]); + if (!bRes) + { + qDebug() << "MoveTo Err"; + return false; + } + + return bRes; +} + +void CVSMD12XControl::SetProtocolType(int iProtocolType /*= 0*/) +{ + m_iProtocolType = iProtocolType; +} + +void CVSMD12XControl::SetCID(int iID) +{ + m_iCID = iID; + + char cmd[64]; + sprintf_s(cmd, 64, "%d ", iID); + std::string strCID(cmd); + m_strCID = strCID; + return; +} + +void CVSMD12XControl::SetSyncMode(bool bSyncMove /*= false*/) +{ + m_bIsSyncMove = bSyncMove; +} + +bool CVSMD12XControl::InitAndOpenPort(PortInfo portinfo) +{ + bool bRes = false; + if (portinfo.iPortType==0) + { + m_pAbsPort = new CZZ_SeiralPort_QT; + } + + m_pAbsPort->InitPort(portinfo); + bRes = m_pAbsPort->SetPort(); + if (!bRes) + { + return bRes; + } + bRes = m_pAbsPort->Open(); + return bRes; +} + +std::string CVSMD12XControl::ReadReceive() +{ + return m_pAbsPort->ReadAll(); +} + +int CVSMD12XControl::SendCommand(const std::string strSend) +{ + return m_pAbsPort->Write(strSend); +} + +bool CVSMD12XControl::InitController() +{ +// std::string strTest = "this is a test message."; +// +// char *pcTest = new char[strTest.length()]; +// strTest.copy(pcTest, strTest.length(),0); +// +// memcpy(pcTest, strTest.c_str(), 23); +// +// char *pcTest1 = (char*)strTest.c_str(); +// std::string test=m_pAbsPort->ReadAll(); + std::string strCMD = "dev\n"; + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + int iRet=m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + std::string strRecv = m_pAbsPort->ReadAll(); + ParseReturnedString(strRecv, 0); + std::string::size_type szPosition; + szPosition = strRecv.find("VSMD", 0); + if (szPosition != strRecv.npos) + { + return true; + } + return false; +} + +bool CVSMD12XControl::GetStatus(MSInfo &stuMotorParam) +{ + std::string strCMD = "cts\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(200); + + std::string strRecv = m_pAbsPort->ReadAll(); + ParseReturnedParam(strRecv); + + stuMotorParam = m_stuMSInfo; + return true; +} + +bool CVSMD12XControl::Move(int iRelPulse) +{ + using namespace ZZ_MISCDEF; + char cmd[64]; + sprintf_s(cmd, 64, "rmv %d\n", iRelPulse); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + QThread::msleep(200); + m_pAbsPort->ReadAll(); + if (m_bIsSyncMove) + { + if (iRet != 0) + { + return false; + } + + bool bFlagIsStopped=false; + while (!bFlagIsStopped) + { + QThread::msleep(200); + GetStatus(m_stuMSInfo); + ZZ_U8 ucFlag = m_stuMSInfo.uiFlags & 0x000000FF; + ucFlag = ucFlag & 0x10; + if (ucFlag==0x10 && m_stuMSInfo.fVelocity==0) + { + bFlagIsStopped = true; + } + } + return true; + } + else + { + if (iRet == 0) + { + return true; + } + return false; + } + +} + +bool CVSMD12XControl::Enable() +{ + std::string strCMD = "ena\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(200); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +bool CVSMD12XControl::Disable() +{ + std::string strCMD = "off\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(200); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); + +} + +bool CVSMD12XControl::MoveTo(int iAbsPulse) +{ + + char cmd[64]; + sprintf_s(cmd, 64, "pos %d\n", iAbsPulse); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(200); + + m_pAbsPort->ReadAll(); + + if (m_bIsSyncMove) + { + if (iRet != 0) + { + return false; + } + + bool bFlagIsStopped = false; + while (!bFlagIsStopped) + { + QThread::msleep(200); + GetStatus(m_stuMSInfo); + ZZ_U8 ucFlag = m_stuMSInfo.uiFlags & 0x000000FF; + ucFlag = ucFlag & 0x10; + if (ucFlag == 0x10 && m_stuMSInfo.fVelocity == 0) + { + bFlagIsStopped = true; + } + } + return true; + } + else + { + if (iRet == 0) + { + return true; + } + return false; + } +// if (iRet == 0) +// { +// return true; +// } +// return false; +// std::string strTest; +// strTest.resize(7); +// char test[7]; +// //memcpy(test, strCMD.c_str(), 7); +// for (size_t i = 0; i < 7; i++) +// { +// strTest[i] = cmd[i]; +// } +} + +bool CVSMD12XControl::SetLogicZero() +{ + std::string strCMD = "org\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(200); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +bool CVSMD12XControl::StartBackZero() +{ + std::string strCMD = "zero start\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(200); + + std::string strRecv = m_pAbsPort->ReadAll(); + + if (!ParseReturnedString(strRecv, -1)) + { + return false; + } + + bool bFlagIsStopped = false; + while (!bFlagIsStopped) + { + QThread::msleep(200); + GetStatus(m_stuMSInfo); + ZZ_U8 ucFlag = m_stuMSInfo.uiFlags & 0x000000FF; + ucFlag = ucFlag & 0x10; + if (ucFlag == 0x10 && m_stuMSInfo.fVelocity == 0) + { + bFlagIsStopped = true; + } + } + return true; +// return ParseReturnedString(strRecv, -1); + +} + +bool CVSMD12XControl::StopBackZero() +{ + std::string strCMD = "zero stop\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(200); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +bool CVSMD12XControl::SetBaudRate(int iBaudRate) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg bdr=%d\n", iBaudRate); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +int CVSMD12XControl::GetBaudRate() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes= ParseReturnedParam(strRecv); + if (bRes=false) + { + return -1; + } + else + { + return m_stuControllerParams.bdr; + } +} + +bool CVSMD12XControl::SetMicroSteps(int iMicroSteps) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg mcs=%d\n", iMicroSteps); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +int CVSMD12XControl::GetMicroSteps() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.mcs; + } +} + +bool CVSMD12XControl::SetAccSpeed(float fAccSpd) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg acc=%f\n", fAccSpd); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +float CVSMD12XControl::GetAccSpeed() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.acc; + } +} + +bool CVSMD12XControl::SetRunSpeed(float fRunSpeed) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg spd=%f\n", fRunSpeed); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +float CVSMD12XControl::GetRunSpeed() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.spd; + } +} + +bool CVSMD12XControl::SetDecSpeed(float fDecSpeed) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg dec=%f\n", fDecSpeed); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +float CVSMD12XControl::GetDecSpeed() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.dec; + } +} + +bool CVSMD12XControl::SetSpeed(float fRunSpeed, float fAccSpd, float fDecSpeed) +{ + char cmd[128]; + sprintf_s(cmd, 128, "cfg spd=%f acc=%f dec=%f\n", fRunSpeed, fAccSpd, fDecSpeed); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +bool CVSMD12XControl::SetHoldCurrent(float fHoldCurrent) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg crh=%f\n", fHoldCurrent); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +float CVSMD12XControl::GetHoldCurrent() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.crh; + } +} + +bool CVSMD12XControl::SetAccCurrent(float fAccCurrent) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg cra=%f\n", fAccCurrent); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +float CVSMD12XControl::GetAccCurrent() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.cra; + } +} + +bool CVSMD12XControl::SetRunCurrent(float fRunCurrent) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg crn=%f\n", fRunCurrent); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +float CVSMD12XControl::GetRunCurrent() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.crn; + } +} + +bool CVSMD12XControl::SetCurrent(float fAccCurrent, float fRunCurrent, float fHoldCurrent) +{ + char cmd[128]; + sprintf_s(cmd, 128, "cfg cra=%f crn=%f crh=%f\n", fAccCurrent, fRunCurrent, fHoldCurrent); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +bool CVSMD12XControl::SetS1FETE(ZZ_U8 bS1FETE) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg s1f=%d\n", bS1FETE); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +ZZ_U8 CVSMD12XControl::GetS1FETE() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.s1f; + } +} + +bool CVSMD12XControl::SetS1RETE(ZZ_U8 bS1RETE) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg s1r=%d\n", bS1RETE); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +ZZ_U8 CVSMD12XControl::GetS1RETE() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.s1r; + } +} + +bool CVSMD12XControl::SetS2FETE(ZZ_U8 bS2FETE) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg s2f=%d\n", bS2FETE); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +ZZ_U8 CVSMD12XControl::GetS2FETE() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.s2f; + } +} + +bool CVSMD12XControl::SetS2RETE(ZZ_U8 bS2RETE) +{ + char cmd[64]; + sprintf_s(cmd, 64, "cfg s2r=%d\n", bS2RETE); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + return ParseReturnedString(strRecv, -1); +} + +ZZ_U8 CVSMD12XControl::GetS2RETE() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.s2r; + } +} + +bool CVSMD12XControl::SetZeroParams(ZZ_U8 bZeroMode, ZZ_U8 bSwitchOpenVL, ZZ_U8 bZeroSensor, float fZeroVelocity, int iSafePosition) +{ + char cmd[128]; + sprintf_s(cmd, 128, "cfg zmd=%d osv=%d snr=%d zsd=%f zsp=%d\n", + (int)bZeroMode, (int)bSwitchOpenVL, (int)bZeroSensor, fZeroVelocity, iSafePosition); + std::string strCMD(cmd); + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(800); + + std::string strRecv = m_pAbsPort->ReadAll(); + + return ParseReturnedString(strRecv, -1); +} + +ZZ_U8 CVSMD12XControl::GetZeroMode() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.zmd; + } +} + +ZZ_U8 CVSMD12XControl::GetSwitchOpenVoltageLevel() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.osv; + } +} + +ZZ_U8 CVSMD12XControl::GetZeroSensor() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.snr; + } +} + +int CVSMD12XControl::GetSafePosition() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.zsp; + } +} + +float CVSMD12XControl::GetZeroVelocity() +{ + std::string strCMD = "cfg\n"; + + if (m_iProtocolType) + { + strCMD = m_strCID + strCMD; + } + + int iRet = m_pAbsPort->Write(strCMD); + + QThread::msleep(500); + + std::string strRecv = m_pAbsPort->ReadAll(); + bool bRes = ParseReturnedParam(strRecv); + if (bRes = false) + { + return -1; + } + else + { + return m_stuControllerParams.zsp; + } +} + +bool CVSMD12XControl::ParseReturnedString(std::string &strRecv, int iType) +{ + char cHead =(char) 0xFF; + if (iType == -1) + { + if (strRecv[0]== cHead) + { + return true; + } + return false; + } + else if (iType == 0) + { + if (strRecv.size()<3) + { + return false; + } + strRecv.erase(0, 3); + strRecv.erase(strRecv.size() - 3, 3); + return true; + } + else + { + return false; + } +} + +bool CVSMD12XControl::ParseReturnedParam(std::string &strRecv) +{ + if (strRecv[2]==0x2) + { + //strRecv.erase(0, 3); + //strRecv.erase(strRecv.size() - 3, 3); + if (strRecv.size() != 21) + { + return false; + } + ZZ_U8 *pbRecv = (ZZ_U8*)strRecv.c_str(); + + VSMD_BitShift(&pbRecv[3], &m_unValue); + m_stuMSInfo.fVelocity = m_unValue.fValue; + VSMD_BitShift(&pbRecv[8], &m_unValue); + m_stuMSInfo.iPosition = m_unValue.i32Value; + VSMD_BitShift(&pbRecv[13], &m_unValue); + m_stuMSInfo.uiFlags = m_unValue.u32Value; + + //delete pbRecv; + return true; + } + else if (strRecv[2] == 0x3) + { + strRecv[strRecv.size() - 3] = 0; + char* pcParam = ((char*)strRecv.c_str())+3; + + while (pcParam) + { + char* pcNext = VSMD_Split(pcParam, ' '); + char* pcValueStr = VSMD_Split(pcParam, '='); + + // + if (pcValueStr) + { + if (strcmp(pcParam, "bdr") == 0) + { + int iValue = atoi(pcValueStr); + m_stuControllerParams.bdr = iValue; + } + else if (strcmp(pcParam, "mcs") == 0) + { + int iValue = atoi(pcValueStr); + m_stuControllerParams.mcs = iValue; + } + else if (strcmp(pcParam, "spd") == 0) + { + float fValue = (float)atof(pcValueStr); + m_stuControllerParams.spd = fValue; + } + else if (strcmp(pcParam, "acc") == 0) + { + float fValue = (float)atof(pcValueStr); + m_stuControllerParams.acc = fValue; + } + else if (strcmp(pcParam, "dec") == 0) + { + float fValue = (float)atof(pcValueStr); + m_stuControllerParams.dec = fValue; + } + else if (strcmp(pcParam, "cra") == 0) + { + float fValue = (float)atof(pcValueStr); + m_stuControllerParams.cra = fValue; + } + else if (strcmp(pcParam, "crn") == 0) + { + float fValue = (float)atof(pcValueStr); + m_stuControllerParams.crn = fValue; + } + else if (strcmp(pcParam, "crh") == 0) + { + float fValue = (float)atof(pcValueStr); + m_stuControllerParams.crh = fValue; + } + else if (strcmp(pcParam, "s1f") == 0) + { + int iValue = atoi(pcValueStr); + m_stuControllerParams.s1f = iValue; + } + else if (strcmp(pcParam, "s1r") == 0) + { + int iValue = atoi(pcValueStr); + m_stuControllerParams.s1r = iValue; + } + else if (strcmp(pcParam, "s2f") == 0) + { + int iValue = atoi(pcValueStr); + m_stuControllerParams.s2f = iValue; + } + else if (strcmp(pcParam, "s2r") == 0) + { + int iValue = atoi(pcValueStr); + m_stuControllerParams.s2r = iValue; + } + else if (strcmp(pcParam, "zmd") == 0) + { + int iValue = atoi(pcValueStr); + m_stuControllerParams.zmd = iValue; + } + else if (strcmp(pcParam, "osv") == 0) + { + int iValue = atoi(pcValueStr); + m_stuControllerParams.osv = iValue; + } + else if (strcmp(pcParam, "snr") == 0) + { + int iValue = atoi(pcValueStr); + m_stuControllerParams.snr = iValue; + } + else if (strcmp(pcParam, "zsd") == 0) + { + float fValue = (float)atof(pcValueStr); + m_stuControllerParams.zsd = fValue; + } + else if (strcmp(pcParam, "zsp") == 0) + { + int iValue = atoi(pcValueStr); + m_stuControllerParams.zsp = iValue; + } + + } + pcParam = pcNext; + } + return true; + } + + return false; +} + +void CVSMD12XControl::VSMD_BitShift(ZZ_U8* src, unValue* dst) +{ + if (src) + { + dst->u32Value = src[0]; + + dst->u32Value <<= 7; + dst->u32Value |= src[1]; + + dst->u32Value <<= 7; + dst->u32Value |= src[2]; + + dst->u32Value <<= 7; + dst->u32Value |= src[3]; + + dst->u32Value <<= 7; + dst->u32Value |= src[4]; + } +} + +char* CVSMD12XControl::VSMD_Split(char* cStr, char cSplit) +{ + if (cStr) + { + for (char* cPtr = cStr; *cPtr; cPtr++) + { + if (*cPtr == cSplit) + { + *cPtr = '\0'; + cPtr++; + return cPtr; + } + } + } + + return NULL; +} + +// int CVSMD12XControl::add(int a, int b) +// { +// return a + b; +// } diff --git a/source/LinearShutter/VSMD12XControl.h b/source/LinearShutter/VSMD12XControl.h new file mode 100644 index 0000000..f53a58f --- /dev/null +++ b/source/LinearShutter/VSMD12XControl.h @@ -0,0 +1,143 @@ +#pragma once +#include "pch.h" +#include "AbstractPort.h" +using namespace std; +// #ifdef IRIS_API_EXPORTS +// +// #ifdef __cplusplus +// #define IRIS_API_FUNC extern "C" __declspec(dllexport) +// #else +// #define IRIS_API_FUNC __declspec(dllexport) +// #endif +// +// #else +// +// #ifdef __cplusplus +// #define IRIS_API_FUNC extern "C" __declspec(dllimport) +// #else +// #define IRIS_API_FUNC __declspec(dllimport) +// #endif +// +// #endif +// +// +// #ifdef IRIS_API_EXPORTS +// #define IRIS_API_CLASS __declspec(dllexport) +// #else +// #define IRIS_API_CLASS __declspec(dllimport) +// #endif + +class /*IRIS_API_CLASS*/ CVSMD12XControl +{ +public: + CVSMD12XControl(); + ~CVSMD12XControl(); +private: + int *m_piPositionInPulses; + bool m_bIsSyncMove; + CAbstractPort *m_pAbsPort; + unValue m_unValue; + MSInfo m_stuMSInfo; + ControllerParams m_stuControllerParams; + int m_iProtocolType; + int m_iCID; + std::string m_strCID; +public: + ////////////////////////////////////////////////////////////////////////// + //for IRIS Linear Motor - Electric Shutter + + //Initialize communication setup iPortType(0:RS232 1:RS485) iCID(For RS485 device) bSyncMove(move mode) + bool ILMES_InitializeComm(PortInfo portinfo,int iPortType = 1,int iCID = 1, bool bSyncMove = true); + + //Initialize Controller params + bool ILMES_InitializeParams(ControllerParams cpParams); + + //Set the light hole position(pulses) + void ILMES_SetPosition(int *piPositionInPulse,size_t szSize); + + bool ILMES_MoveToPos(int iPositionIndex); + +public: + + ////init port + void SetProtocolType(int iProtocolType = 0);//0:RS232 1:RS485 call before send any command + void SetCID(int iID);//set device id when using RS485 protocol call before send any command + void SetSyncMode(bool bSyncMove = false); + bool InitAndOpenPort(PortInfo portinfo);//Init comport by PortInfo Struct + + //////////////////////////////////////////////////////////////////////////for debug or unsupported function + std::string ReadReceive(); + int SendCommand(const std::string strSend); // return indicates how many char has been sent + //////////////////////////////////////////////////////////////////////////motor control + ////init&misc + bool InitController(); //Init Motor controller hand shake + + bool Enable(); //enbale + bool Disable(); //disable + + bool GetStatus(MSInfo &stuMotorParam); //load motor status see MSInfo Struct + ////motion control + bool Move(int iRelPulse); // relative move mode,pulse (-2147483647~2147483647) + bool MoveTo(int iAbsPulse);//absolute mode,pulse (-2147483647~2147483647) + + bool SetLogicZero(); //set logic zero + + bool StartBackZero();//back zero + bool StopBackZero();// stop back zero & reset register + ////Set&Get + bool SetBaudRate(int iBaudRate);//set controller baud rates,default B9600 (2400 ~ 921600) + int GetBaudRate(); // -1:should retry; + + bool SetMicroSteps(int iMicroSteps);//set micro steps (0~5)->(1/2/4/8/16/32) + int GetMicroSteps(); + + bool SetAccSpeed(float fAccSpd);//set acceleration speed in pulse (0 ~ 192000000) + float GetAccSpeed(); + + bool SetRunSpeed(float fRunSpeed);//set normal running speed in pulse (0 ~ 192000000) + float GetRunSpeed(); + + bool SetDecSpeed(float fDecSpeed);//set deceleration speed in pulse (0 ~ 192000000) + float GetDecSpeed(); + + bool SetSpeed(float fRunSpeed, float fAccSpd, float fDecSpeed);//see above three functions + + bool SetHoldCurrent(float fHoldCurrent);//set hold current ( 0 ~ 8.0 )Amp? pay attention to your motor specification. + float GetHoldCurrent(); + + bool SetAccCurrent(float fAccCurrent);//set acceleration current ( 0 ~ 8.0 )Amp? pay attention to your motor specification. + float GetAccCurrent(); + + bool SetRunCurrent(float fRunCurrent);//set normal running current ( 0 ~ 8.0 )Amp? pay attention to your motor specification. + float GetRunCurrent(); + + bool SetCurrent(float fAccCurrent, float fRunCurrent, float fHoldCurrent);//see above three functions + + bool SetS1FETE(ZZ_U8 bS1FETE);//S1 register falling edge trigger event,see enum + ZZ_U8 GetS1FETE(); + + bool SetS1RETE(ZZ_U8 bS1RETE);//S1 register rising edge trigger event + ZZ_U8 GetS1RETE(); + + bool SetS2FETE(ZZ_U8 bS2FETE);//S2 register falling edge trigger event + ZZ_U8 GetS2FETE(); + + bool SetS2RETE(ZZ_U8 bS2RETE);//S2 register rising edge trigger event + ZZ_U8 GetS2RETE(); + + //(ZeroMode 0:off 1:once 2:once + safe position 3:twice 4:twice + safe position) (OpenVoltageLevel 0:Low Level 1:High Level) + //(ZeroSensor 0:S1 1:S2 2:S3 3:S4 4:S5 5:S6)( ZeroVelocity (-192000) ~(192000)pulse)(SafePosition (-2147483647) ~(2147483647)) + bool SetZeroParams(ZZ_U8 bZeroMode, ZZ_U8 bSwitchOpenVL, ZZ_U8 bZeroSensor, float fZeroVelocity, int iSafePosition); + ZZ_U8 GetZeroMode(); + ZZ_U8 GetSwitchOpenVoltageLevel(); + ZZ_U8 GetZeroSensor(); + int GetSafePosition(); + float GetZeroVelocity(); + +private: + bool ParseReturnedString(std::string &strRecv,int iType); + bool ParseReturnedParam(std::string &strRecv); + void VSMD_BitShift(ZZ_U8* src, unValue* dst); + char* VSMD_Split(char* cStr, char cSplit); +}; + diff --git a/source/LinearShutter/VSMD12XMiscDefines.h b/source/LinearShutter/VSMD12XMiscDefines.h new file mode 100644 index 0000000..93fb35e --- /dev/null +++ b/source/LinearShutter/VSMD12XMiscDefines.h @@ -0,0 +1,79 @@ +#pragma once +#include "ZZ_Types.h" +#define MAXBUFFER 10240 + +using namespace ZZ_MISCDEF; + +typedef struct tagTransportLayerInfomation +{ + //Serial + int iPortType; + int iPortNumber; + int indexBaudRate; + int iBaudRate; + int indexParity, indexBytesize, indexStopBits; //Mode + bool Hw; + bool Sw; + bool Dtr, Rts; + + //Bit + //Stop + //MODEM CONTROL setting + //MODEM LINE STATUS + + //TCP UDP + +}PortInfo; + +typedef struct tagMotorStatusInfo +{ + float fVelocity; + int iPosition; + ZZ_U32 uiFlags; +}MSInfo; + +typedef struct tagControllerParams +{ + int bdr; //baud + int mcs; //microsteps 0:full 1:half 2:4 3:8 4:16 5:32 6:64 7:128 8:256 + float spd; //velocity + float acc; //acceleration + float dec; //deceleration + float cra; //acc state current + float crn; //normal state current + float crh; //hold state current + ZZ_U8 s1f; + ZZ_U8 s1r; + ZZ_U8 s2f; + ZZ_U8 s2r; + ZZ_U8 zmd; //zero mode + ZZ_U8 osv; //open state sensor value + ZZ_U8 snr; //zero sensor + ZZ_U8 zsp; //zero safe position + float zsd; //zero velocity +}ControllerParams; + +typedef union +{ + ZZ_U8 byteValue[4]; + short i16Value[2]; + ZZ_U16 u16Value[2]; + int i32Value; + float fValue; + ZZ_U32 u32Value; +}unValue; + +enum emVSMDTriggerEvent +{ + NoAction =0, //无动作(只有状态位 S1 变化通知) + ResetZeroPosition, //重新设置原点位置 + DecSpdStop, //减速停止 + DecSpdStopRZP, //减速停止,并在停止后重新设置原点位置 + Stop, //立刻停止 + StopRZP, //立刻停止,并在停止后重新设置原点位置 + RunningPosDirect, //正向连续运转(正速度) + RunningNegDirect, //反向连续运转(负速度) + OfflineModeOn, //离线模式启动 + OfflineModeOff //离线模式停止 +}; + diff --git a/source/LinearShutter/ZZ_SeiralPort.cpp b/source/LinearShutter/ZZ_SeiralPort.cpp new file mode 100644 index 0000000..6961107 --- /dev/null +++ b/source/LinearShutter/ZZ_SeiralPort.cpp @@ -0,0 +1,123 @@ +#include "pch.h" +#include "ZZ_SeiralPort.h" + +CZZ_SeiralPort_QT::CZZ_SeiralPort_QT() +{ + m_pSerialPort = new QSerialPort; +} + +CZZ_SeiralPort_QT::~CZZ_SeiralPort_QT() +{ + if (m_pSerialPort != NULL) + { + delete m_pSerialPort; + } +} + + void CZZ_SeiralPort_QT::InitPort(PortInfo portinfo) +{ + + m_piSettings = portinfo; + + +} + + bool CZZ_SeiralPort_QT::SetPort() + { + bool bRes = true; +// int iRes = sio_ioctl(m_piSettings.iPortNumber, m_piSettings.indexBaudRate, +// m_piSettings.indexParity | m_piSettings.indexBytesize | m_piSettings.indexStopBits); +// if (iRes != SIO_OK) +// { +// bRes = false; +// } + QString qstrPortName = QString("COM%1").arg(m_piSettings.iPortNumber); + m_pSerialPort->setPortName(qstrPortName); + m_pSerialPort->setReadBufferSize(512); + bRes = m_pSerialPort->setBaudRate(m_piSettings.iBaudRate); + return bRes; + } + +bool CZZ_SeiralPort_QT::Open() +{ + bool bRes = true; +// int iRes = sio_open(m_piSettings.iPortNumber); +// if (iRes!=SIO_OK) +// { +// bRes = false; +// } + bRes = m_pSerialPort->open(QIODevice::ReadWrite); + if (!bRes) + { + qDebug() << "Err:open Failed.Exit Code:1"; + //std::cout << "Err.open Failed" << std::endl; + return bRes;; + } + return bRes; +} + +void CZZ_SeiralPort_QT::Close() +{ + m_pSerialPort->close(); +} + +std::string CZZ_SeiralPort_QT::ReadAll() +{ + m_pSerialPort->waitForReadyRead(); + qbaRecv.clear(); + qbaRecv = m_pSerialPort->readAll(); + std::string strRet(qbaRecv.constData(), qbaRecv.length()); + return strRet; + +// std::string strRet; +// char cBuf[MAXBUFFER]; +// int iRet = sio_read(m_piSettings.iPortNumber,cBuf, MAXBUFFER); +// if (iRet==0) +// { +// strRet = ""; +// +// } +// else +// { +// char *pRecv = new char[iRet]; +// memcpy(pRecv, cBuf, iRet); +// //std::string strRet(pRecv); +// //delete pRecv; +// //return strRet; +// +// strRet.resize(iRet); +// for (int i=0;iwrite(qbaTest); + m_pSerialPort->waitForBytesWritten(); + if (qi64Write != qbaTest.size()) + { + qDebug() << "Err:write Failed.Exit Code:1" << qi64Write; + return qi64Write; + } + + return 0; +// char* pcSend = (char*)strSend.c_str(); +// int iRet = sio_write(m_piSettings.iPortNumber, pcSend, (int)strSend.length()); +// +// //delete pcSend; +// if (iRet!= (int)strSend.length()) +// { +// return iRet; +// } +// else +// { +// return 0; +// } +// //sio_write(); +} diff --git a/source/LinearShutter/ZZ_SeiralPort.h b/source/LinearShutter/ZZ_SeiralPort.h new file mode 100644 index 0000000..57a6dfb --- /dev/null +++ b/source/LinearShutter/ZZ_SeiralPort.h @@ -0,0 +1,25 @@ +#pragma once +#include "VSMD12XMiscDefines.h" +#include "AbstractPort.h" + +class CZZ_SeiralPort_QT:public CAbstractPort +{ + +private: + PortInfo m_piSettings; + QByteArray qbaSend, qbaRecv; + QSerialPort *m_pSerialPort; +public: + CZZ_SeiralPort_QT(); + virtual ~CZZ_SeiralPort_QT(); +public: + virtual void InitPort(PortInfo portinfo); + virtual bool SetPort(); + virtual bool Open(); + virtual void Close(); + + virtual std::string ReadAll(); + virtual int Write(const std::string strSend); + +}; + diff --git a/source/Settings/SystemConfigger.cpp b/source/Settings/SystemConfigger.cpp index d60f0b4..46e8800 100644 --- a/source/Settings/SystemConfigger.cpp +++ b/source/Settings/SystemConfigger.cpp @@ -42,8 +42,29 @@ { QString qstrTemp= m_qsDeviceConfig->value(QString("FS%1/Model").arg(i+1), "Null").toString(); m_struDeviceContext.ucDeviceModel[i]= enumDeviceModel.keysToValue(qstrTemp.toLatin1().data()); + + int iInterface= m_qsDeviceConfig->value(QString("FS%1/Port").arg(i + 1), -2).toInt(); + m_struDeviceContext.iInterface[i] = iInterface; + + qstrTemp = m_qsDeviceConfig->value(QString("FS%1/UID").arg(i + 1), "Null").toString(); + m_struDeviceContext.strSN[i] = qstrTemp.toStdString(); } + m_struAcqPosition.iTotalPosition = m_qsDeviceConfig->value(QString("LINEAR SHUTTER/TotalPosition"), -1).toInt(); + if (m_struAcqPosition.iTotalPosition == -1) + { + qDebug() << "LINEAR SHUTTER TotalPosition Value Err."; + return 4; + } + + for (ZZ_U8 i = 0; i < m_struAcqPosition.iTotalPosition; i++) + { + int iPosition = m_qsDeviceConfig->value(QString("LINEAR SHUTTER/Position%1").arg(i), -2).toInt(); + m_struAcqPosition.iPosition[i] = iPosition; + } + + + //////////////////////////////////////////////////////////////////////////load json settings qfJConfig.setFileName(m_qstrJSONConfigFilePath); bool bRes = qfJConfig.open(QIODevice::ReadOnly); @@ -67,7 +88,7 @@ int ZZ_SysConfigger::GetGrabberRunParams(RunTimeGrabberParams &struGrabberRTParams) { struGrabberRTParams.atsParams = m_struAcqTime; - struGrabberRTParams.dcParams = m_struDeviceContext; + struGrabberRTParams.fscParams = m_struDeviceContext; return 0; } diff --git a/source/Settings/SystemConfigger.h b/source/Settings/SystemConfigger.h index 5388b0c..6f237a2 100644 --- a/source/Settings/SystemConfigger.h +++ b/source/Settings/SystemConfigger.h @@ -33,7 +33,8 @@ public: Q_ENUM(ConfiggerDeviceModel); //////////////////////////////////////////////////////////////////////////run-time settings AcqTimeSettings m_struAcqTime; - DeviceContext m_struDeviceContext; + FSContext m_struDeviceContext; + AcqPosSettings m_struAcqPosition; ////////////////////////////////////////////////////////////////////////// EContext m_struEC; MEContext m_struMEC; diff --git a/source/Thread/AbstractFSController.cpp b/source/Thread/AbstractFSController.cpp index f020e70..8aebeed 100644 --- a/source/Thread/AbstractFSController.cpp +++ b/source/Thread/AbstractFSController.cpp @@ -1,19 +1,22 @@ #include "AbstractFSController.h" CAbstractFSController::CAbstractFSController(QObject* parent /*= nullptr*/) { + m_pFSCtrl = NULL; m_iThreadID = -1; - m_iDeviceType = -1; } CAbstractFSController::~CAbstractFSController() { - + if (m_pFSCtrl!= 0 ) + { + delete m_pFSCtrl; + } } -int CAbstractFSController::SetRunParas(int iThreadID, int iDeviceType) +int CAbstractFSController::SetRunParas(int iThreadID, FSInfo fsInfo) { m_iThreadID = iThreadID; - m_iDeviceType = iDeviceType; + m_fsInfo = fsInfo; return 0; } @@ -21,12 +24,13 @@ int CAbstractFSController::SetRunParas(int iThreadID, int iDeviceType) int CAbstractFSController::InitializeFSControl() { - if (m_iThreadID == -1|| m_iDeviceTyp == -1) + using namespace ZZ_MISCDEF::IRIS; + if (m_iThreadID == -1/*|| m_iDeviceType == -1*/) { qDebug() << "Params Err. Call SetRunParas first"; return 1; } - switch (m_iDeviceType) + switch (m_fsInfo.ucDeviceModel) { case DeviceModel::OSIFAlpha: @@ -35,12 +39,15 @@ int CAbstractFSController::InitializeFSControl() break; case DeviceModel::ISIF: m_pFSCtrl = new ZZ_ATPControl_Serial_Qt; + m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, NULL); break; case DeviceModel::IS1: m_pFSCtrl = new ZZ_ATPControl_Serial_Qt; + m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, NULL); break; case DeviceModel::IS2: m_pFSCtrl = new ZZ_ATPControl_Serial_Qt; + m_pFSCtrl->Initialize(false, m_fsInfo.iInterface, NULL); break; default: break; @@ -50,11 +57,18 @@ int CAbstractFSController::InitializeFSControl() int CAbstractFSController::StartAcquisition() { + // + qDebug() << "Starting acq" << " Thread ID:" << m_iThreadID; + QThread::msleep(5000); + qDebug() << "Stop acq" << " Thread ID:" << m_iThreadID; + emit SignalAcqFinished(m_iThreadID, 1); return 0; } int CAbstractFSController::StopAcquisition() { + return 0; } + diff --git a/source/Thread/AbstractFSController.h b/source/Thread/AbstractFSController.h index f75dcbe..0788356 100644 --- a/source/Thread/AbstractFSController.h +++ b/source/Thread/AbstractFSController.h @@ -3,6 +3,13 @@ #include "ATPControl_Serial_QT.h" #include +typedef struct tagFSInfo +{ + ZZ_U8 ucDeviceModel; + int iInterface; + std::string strSN; +}FSInfo; + class CAbstractFSController :public QObject { Q_OBJECT @@ -10,21 +17,19 @@ public: CAbstractFSController(QObject* parent = nullptr); virtual ~CAbstractFSController(); public: -private: - CIrisFSBase *m_pFSCtrl; - - int m_iThreadID; - int m_iDeviceType; -public slots: - //call first - virtual int SetRunParas(int iThreadID, int iDeviceType); + virtual int SetRunParas(int iThreadID, FSInfo fsInfo); //create derived class from base class virtual int InitializeFSControl(); - virtual int StartAcquisition(); +private: + CIrisFSBase *m_pFSCtrl; + int m_iThreadID; + FSInfo m_fsInfo; +public slots: + virtual int StartAcquisition(); virtual int StopAcquisition(); signals: diff --git a/source/Thread/MainDataGrabber.cpp b/source/Thread/MainDataGrabber.cpp index 758a6cc..21e77ee 100644 --- a/source/Thread/MainDataGrabber.cpp +++ b/source/Thread/MainDataGrabber.cpp @@ -1,8 +1,46 @@ +#include "MainDataGrabber.h" + +CMainDataGrabber::CMainDataGrabber(QObject* parent /*= nullptr*/) +{ + m_piFlagCaptureThreadStatus = NULL; +} + +CMainDataGrabber::~CMainDataGrabber() +{ + if (m_pControlThread.size() != 0) + { + for (int i=0;i< m_pControlThread.size();i++) + { + delete m_pControlThread[i]; + } + } + m_pControlThread.clear(); + + if (m_piFlagCaptureThreadStatus!=NULL) + { + delete m_piFlagCaptureThreadStatus; + } +} int CMainDataGrabber::SetGrabberParams(RunTimeGrabberParams struGrabberRTParams) { m_struAcqTime = struGrabberRTParams.atsParams; - m_struDeviceContext = struGrabberRTParams.dcParams; + m_struDeviceContext = struGrabberRTParams.fscParams; + m_iTotalThreads = m_struDeviceContext.ucDeviceNumber; + return 0; +} + +int CMainDataGrabber::InitThreadStatus() +{ + if (m_piFlagCaptureThreadStatus!=NULL) + { + delete m_piFlagCaptureThreadStatus; + } + m_piFlagCaptureThreadStatus = new int[m_iTotalThreads]; + for (int i = 0; i < m_iTotalThreads; i++) + { + m_piFlagCaptureThreadStatus[i] = 0; + } return 0; } @@ -10,8 +48,93 @@ int CMainDataGrabber::InitializeWorkers() { for (ZZ_U8 i = 0; i < m_struDeviceContext.ucDeviceNumber; i++) { + m_piFlagCaptureThreadStatus[i] = 0; + FSInfo fsInfo; + fsInfo.iInterface = m_struDeviceContext.iInterface[i]; + fsInfo.ucDeviceModel = m_struDeviceContext.ucDeviceModel[i]; + fsInfo.strSN = m_struDeviceContext.strSN[i]; + + CAbstractFSController* m_pWorkThread = new CAbstractFSController; + m_pWorkThread->SetRunParas(i, fsInfo); + //m_pWorkThread->InitializeFSControl(); + m_pControlThread.push_back(m_pWorkThread); } return 0; } +int CMainDataGrabber::StartWorkers() +{ + + for (ZZ_U8 i = 0; i < m_struDeviceContext.ucDeviceNumber; i++) + { + QThread *pWorkThreadHolder = new QThread(); + m_pControlThread[i]->moveToThread(pWorkThreadHolder); + pWorkThreadHolder->start(); + } + + return 0; +} + +int CMainDataGrabber::SetupMsgPipelines() +{ + //connect(this, &CMainDataGrabber::SignalStartGrabOnce, this, &CMainDataGrabber::StartGrab); + + for (ZZ_U8 i = 0; i < m_struDeviceContext.ucDeviceNumber; i++) + { + connect(this, &CMainDataGrabber::SignalStartGrabOnce, m_pControlThread[i], &CAbstractFSController::StartAcquisition); + connect(m_pControlThread[i], &CAbstractFSController::SignalAcqFinished, this, &CMainDataGrabber::HandleThreadEvent); + } + + connect(this, &CMainDataGrabber::SignalGrabOnceFinished, this, &CMainDataGrabber::GrabFinished); + + return 0; +} + +int CMainDataGrabber::HandleThreadEvent(int iThreadID, int iFlagStatus) +{ + m_piFlagCaptureThreadStatus[iThreadID] = iFlagStatus; + + bool bFinished = true; + for (int i = 0; i < m_iTotalThreads; i++) + { + if (m_piFlagCaptureThreadStatus[i] != 1) + { + bFinished = false; + break; + } + } + + if (bFinished) + { + emit SignalGrabOnceFinished(); + } + return 0; +} + +int CMainDataGrabber::StartGrab() +{ + m_iFlagIsCapturing = true; + emit SignalStartGrabOnce(); + while (m_iFlagIsCapturing) + { + QThread::msleep(1000); + } + + m_iFlagIsCapturing = true; + InitThreadStatus(); + emit SignalStartGrabOnce(); + while (m_iFlagIsCapturing) + { + QThread::msleep(1000); + } + qDebug() << "Allgrab stopped" << " Thread ID:" <<2; + return 0; +} + +int CMainDataGrabber::GrabFinished() +{ + m_iFlagIsCapturing = false; + return 0; +} + diff --git a/source/Thread/MainDataGrabber.h b/source/Thread/MainDataGrabber.h index b50dfba..3d3abb2 100644 --- a/source/Thread/MainDataGrabber.h +++ b/source/Thread/MainDataGrabber.h @@ -17,19 +17,28 @@ private: vector m_pControlThread; vector m_pqThreadHolder; + int *m_piFlagCaptureThreadStatus; int m_iTotalThreads; + int m_iFlagIsCapturing; + AcqTimeSettings m_struAcqTime; - DeviceContext m_struDeviceContext; + FSContext m_struDeviceContext; public: //Call this first int SetGrabberParams(RunTimeGrabberParams struGrabberRTParams); + int InitThreadStatus(); int InitializeWorkers(); + int StartWorkers(); + + int SetupMsgPipelines(); + public slots: int HandleThreadEvent(int iThreadID, int iFlagStatus); int StartGrab(); + int GrabFinished(); signals: void SignalStartGrabOnce();