123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- // *****************************************************************************
- // 版权所有(C)2023~2099 上海骄成超声波技术有限公司
- // 保留所有权利
- // *****************************************************************************
- // 作者 : 杨坚
- // 版本 : 1.0
- // 功能说明:
- //
- // *****************************************************************************
- #include "CUGWatch.h"
- #include "../JLogAllOutput.h"
- CUGWatch::CUGWatch()
- {
- ReadConfig();
- }
- CUGWatch::~CUGWatch()
- {
- }
- void CUGWatch::ReadConfig()
- {
- ComParameter com = {};
- com.nSerialPortNumber = 1;
- com.nBaudRate = 65535;
- m_comPar.push_back(com);
- m_cUGSerial.reset(new CRS232());
- }
- bool CUGWatch::GetUserParaUT100(int& iMode, int& iVolFullScale, int& iCurFullScale, int& iPowFullScale, int& iSignSns, int& iTestLev, int iOverTime/* = 2000*/)
- {
- bool bRtn = false;
- iMode = 0;
- iVolFullScale = 0;
- iCurFullScale = 0;
- iPowFullScale = 0;
- iSignSns = 0;
- iTestLev = 0;
- if (COM_OK == JOpenPort())
- {
- m_cUGSerial->ClearBuffer();
- Sleep(10);
- // 读取当前编码器
- unsigned char uiDataBuf[100] = { 0 };
- int iDataLen = 0;
- uiDataBuf[iDataLen++] = 0xFF; //起始标志
- uiDataBuf[iDataLen++] = 0x90; //命令
- //发送
- if (iDataLen != m_cUGSerial->SerialPortWrite(iDataLen, (char*)uiDataBuf))
- {
- JLogAllOutput::cmd_debug("Write Error");
- }
- else
- {
- Sleep(200);
- //读串口
- unsigned char bufRev[100] = { 0 };
- int len = sizeof(bufRev);
- int bytes = m_cUGSerial->SerialPortRead(len, (char*)bufRev, 1000);
- if (bytes >= 1)
- {
- if (9 == bytes)
- {
- iSignSns = (bufRev[2] & 0xF0) >> 4;
- iVolFullScale = (bufRev[3] & 0xF0) >> 4;
- iCurFullScale = (bufRev[4] & 0xF0) >> 4;
- iPowFullScale = (bufRev[5] & 0xF0) >> 4;
- iMode = (bufRev[6] & 0xF0) >> 4;
- iMode += 1; //为了和UT60统一
- iTestLev = (bufRev[8] & 0xF0) >> 4;
- bRtn = TRUE;
- }
- }
- }
- }
- JClosePort();
- return bRtn;
- }
- bool CUGWatch::SetUserParaUT100(int iMode, int iVolFullScale, int iCurFullScale, int iPowFullScale, int iSignSns, int iTestLev, int iOverTime/* = 2000*/)
- {
- bool bRtn = false;
- if (COM_OK == JOpenPort())
- {
- // 写入
- unsigned char uiDataBuf[100] = { 0 };
- int iDataLen = 0;
- uiDataBuf[iDataLen++] = 0xFF; //起始标志
- uiDataBuf[iDataLen++] = 0x50; //命令
- uiDataBuf[iDataLen++] = iSignSns << 4; //信号极性
- uiDataBuf[iDataLen++] = iVolFullScale << 4; //电压量程
- uiDataBuf[iDataLen++] = iCurFullScale << 4; //电流量程
- uiDataBuf[iDataLen++] = iPowFullScale << 4; //功率量程
- uiDataBuf[iDataLen++] = (iMode - 1) << 4; //工作模式,为了和UT60统一
- uiDataBuf[iDataLen++] = 0x00; //00
- uiDataBuf[iDataLen++] = iTestLev << 4; //测试输出等级
- //发送
- if (iDataLen != m_cUGSerial->SerialPortWrite(iDataLen, (char*)uiDataBuf))
- {
- JLogAllOutput::cmd_debug("Write Error");
- }
- else
- {
- Sleep(200);
- while (1)
- {
- //读串口
- unsigned char bufRev[100] = { 0 };
- int len = sizeof(bufRev);
- int bytes = m_cUGSerial->SerialPortRead(len, (char*)bufRev, 1000);
- if (bytes >= 1)
- {
- if (2 == bytes && 0xFF == bufRev[0] && 0x50 == bufRev[1])
- {
- bRtn = TRUE;
- }
- goto _label_exit;
- }
- Sleep(5);
- }
- }
- }
-
- _label_exit:
- JClosePort();
- return bRtn;
- }
- bool CUGWatch::SetCaliCenterFrqUT100(int iCenterFrq, int iOverTime /*= 2000*/)
- {
- bool bRtn = false;
- if (COM_OK == JOpenPort())
- {
- unsigned char uiDataBuf[100] = { 0 };
- int iDataLen = 0;
- uiDataBuf[iDataLen++] = 0xFF; //起始标志
- uiDataBuf[iDataLen++] = 0x60; //命令
- uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF000000) >> 24; //A通道中心频率:2位最高位,5为最低位
- uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF0000) >> 16; //
- uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF00) >> 8; //
- uiDataBuf[iDataLen++] = iCenterFrq & 0XFF; //
- uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF000000) >> 24; //B通道中心频率:6位最高位,9为最低位
- uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF0000) >> 16; //
- uiDataBuf[iDataLen++] = (iCenterFrq & 0XFF00) >> 8; //
- uiDataBuf[iDataLen++] = iCenterFrq & 0XFF; //
- //发送
- if (iDataLen != m_cUGSerial->SerialPortWrite(iDataLen, (char*)uiDataBuf))
- {
- JLogAllOutput::cmd_debug("Write Error");
- }
- else
- {
- Sleep(200);
- while (1)
- {
- //读串口
- unsigned char bufRev[100] = { 0 };
- int len = sizeof(bufRev);
- int bytes = m_cUGSerial->SerialPortRead(len, (char*)bufRev, 1000);
- if (bytes >= 1)
- {
- if (2 == bytes && 0xFF == bufRev[0] && 0x60 == bufRev[1])
- {
- bRtn = TRUE;
- }
- goto _label_exit;
- }
- Sleep(5);
- }
- }
- }
-
- _label_exit:
- JClosePort();
- return bRtn;
- }
- bool CUGWatch::CalibrationUG_UT100()
- {
- bool bRtn = false;
- if (COM_OK == JOpenPort())
- {
- //Write 发送校准
- unsigned char uiDataBuf[10] = { 0 };
- int iDataLen = 0;
- uiDataBuf[iDataLen++] = 0xFF; //起始标志
- uiDataBuf[iDataLen++] = 0x61; //命令
- uiDataBuf[iDataLen++] = 0x01;
- //发送
- if (iDataLen != m_cUGSerial->SerialPortWrite(iDataLen, (char*)uiDataBuf))
- {
- JLogAllOutput::cmd_debug("Write Error");
- }
- else
- {
- //Read
- unsigned char ucDataBuf[256] = { 0 };
- long lCount = 0;
- bool bReading = FALSE;
- while (1)
- {
- //读串口
- unsigned char bufRev;
- int len = sizeof(bufRev);
- int bytes = m_cUGSerial->SerialPortRead(1, (char*)bufRev, 1000);
- if (bytes >= 1)
- {
- bReading = TRUE;
- ucDataBuf[lCount] = bufRev;
- lCount++;
- }
- else if (bReading)
- {
- break;
- }
- }
- //校验下
- {
- if (ucDataBuf[0] != 0xFF || ucDataBuf[1] != 0x61)
- {
- goto _label_exit;
- }
- else
- {
- bRtn = TRUE;
- }
- }
- }
- }
- _label_exit:
- JClosePort();
- return bRtn;
- }
- bool CUGWatch::CheckCaliFinish()
- {
- bool bRtn = false;
- if (COM_OK == JOpenPort())
- {
- unsigned char uiDataBuf[10] = { 0 };
- int iDataLen = 0;
- uiDataBuf[iDataLen++] = 0xFF; //起始标志
- uiDataBuf[iDataLen++] = 0x62; //命令
- //Write 发送校准
- if (iDataLen != m_cUGSerial->SerialPortWrite(iDataLen, (char*)uiDataBuf))
- {
- JLogAllOutput::cmd_debug("Write Error");
- goto _label_exit;
- }
- //Read
- unsigned char ucDataBuf[256] = { 0 };
- long lCount = 0;
- bool bReading = FALSE;
- while (1)
- {
- //读串口
- unsigned char bufRev;
- int len = sizeof(bufRev);
- int bytes = m_cUGSerial->SerialPortRead(1, (char*)bufRev, 1000);
- if (bytes >= 1)
- {
- bReading = TRUE;
- ucDataBuf[lCount] = bufRev;
- lCount++;
- }
- else if (bReading)
- {
- break;
- }
- }
- //校验下
- {
- if (ucDataBuf[0] != 0xFF || ucDataBuf[1] != 0x62)
- {
- goto _label_exit;
- }
- else
- {
- bRtn = true;
- }
- }
- }
- _label_exit:
- JClosePort();
- return bRtn;
- }
- int CUGWatch::JOpenPort()
- {
- JClosePort();
- ComParameter comOne = m_comPar[EN_COM_NUMBER::ONE];
- return m_cUGSerial->OpenPort(comOne.nSerialPortNumber,
- comOne.nBaudRate,
- comOne.nDataBits,//xDataSize?
- comOne.nParity,
- comOne.nStopBits,
- MAX_COM_BUFFER_LEN, MAX_COM_BUFFER_LEN, 1000
- );
- }
- void CUGWatch::JClosePort()
- {
- m_cUGSerial->ClearBuffer();
- m_cUGSerial->ClosePort();
- }
|