?? ofxplcdev.cpp.bak
字號(hào):
// OFxPLCDev.cpp: implementation of the COFxPLCDev class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "OFxPLCDev.h"
#include <cmath>
#include <string>
using namespace std;
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
COFxPLCDev::COFxPLCDev()
{
}
COFxPLCDev::~COFxPLCDev()
{
}
int COFxPLCDev::check_sum(BYTE *buf, int &buflen, BYTE &chSum)
{
/*三菱的伺服放大器的檢驗(yàn)和是將準(zhǔn)備好的命令字符串從第一個(gè)開(kāi)始,依次作加法運(yùn)算。最后得到的字節(jié)轉(zhuǎn)換成兩位的字符。
*比如: "00502303203"的檢驗(yàn)和是FC。計(jì)算是這樣進(jìn)行的: 0x30+0x30+0x35+0x30+0x32+0x33+0x32+0x30+0x33 = 0xFC
*最后的字符串為:"00502303203FC"
*/
//輸入 buf[], buflen
//輸出 chSum
//
char newBuf[128];
memset(newBuf, 0x00, sizeof (newBuf));
//將字符串拷貝到臨時(shí)的緩沖區(qū)中。
memcpy(newBuf, buf, buflen);
int l_get = 0;
for (int i = 0; i<buflen; i++)
{
l_get += newBuf[i];
}
chSum = l_get;
return SUCCESS;
}
int COFxPLCDev::SendTo(_retstruct *_ret,BYTE *addr, int adlen, BYTE *buf, int buflen)
{
//向LCA寄存器發(fā)送數(shù)據(jù)
/************************************************************************/
/*相關(guān)函數(shù):check_sum,為數(shù)據(jù)添加檢驗(yàn)和位
* get_response, 取得LCA的響應(yīng)數(shù)據(jù)
*/
/************************************************************************/
/*
輸入: addr 寄存器地址
adlen 寄存器地址長(zhǎng)度
buf 將要發(fā)送的數(shù)據(jù)
buflen 發(fā)送數(shù)據(jù)的長(zhǎng)度
輸出:
返回: 成功返回success, 否則返回錯(cuò)誤代碼。
*/
return SUCCESS;
}
int COFxPLCDev::ReadFrom(_retstruct *_ret,BYTE *addr, int adlen, BYTE *recbuf, int &reclen)
{
//讀取LCA寄存器數(shù)據(jù)
/************************************************************************/
/*相關(guān)函數(shù):check_sum,為數(shù)據(jù)添加檢驗(yàn)和位
* get_response, 取得LCA的響應(yīng)數(shù)據(jù),并返回錯(cuò)誤代碼
*/
/************************************************************************/
/*
輸入: addr 寄存器地址
adlen 寄存器地址長(zhǎng)度
輸出: recbuf 將要讀取的數(shù)據(jù)緩沖區(qū)
reclen 讀取數(shù)據(jù)的長(zhǎng)度
返回: 成功返回success, 否則返回錯(cuò)誤代碼。
*/
return SUCCESS;
}
int COFxPLCDev::get_response(BYTE *ibuf, int ilen, BYTE *rbuf, int &rlen, _retstruct *_ret)
{
//讀取返回值
/*
輸入: 無(wú)
輸出: resbuf 接受的數(shù)據(jù)緩沖區(qū)
reslen 接受數(shù)據(jù)的長(zhǎng)度
返回: 成功返回success, 否則返回錯(cuò)誤代碼。
*/
char inputbuf[128];
memset(inputbuf, 0x00, sizeof(inputbuf));
for (int i = 0; i<ilen ; i++)
{
inputbuf[i] = ibuf[i];
}
if (inputbuf[2] == 'A' || inputbuf[2] == 'a')
{
memcpy(rbuf, &inputbuf[3], ilen - 6);
rlen = ilen - 6;
_ret->code = SUCCESS;
}
else
{
_ret->code =FAIL;
_ret->discription = "響應(yīng)錯(cuò)誤";
_ret->time = CTime::GetCurrentTime();
}
return _ret->code;
}
int COFxPLCDev::InitDev(CString strReg, _retstruct *_ret)
{
/*
執(zhí)行步驟:
1、讀取注冊(cè)表strReg中的串口參數(shù)
2、這些參數(shù)賦值給它的屬性數(shù)據(jù)
3、調(diào)用InitComm
*/
LONG hr;
HKEY hKey = NULL;
DWORD cbData;
DWORD dwType = REG_DWORD;
DWORD dwDisposition;
hr = RegCreateKeyEx(HKEY_LOCAL_MACHINE, strReg, 0, NULL, REG_OPTION_NON_VOLATILE, \
KEY_WRITE|KEY_READ, NULL, &hKey, &dwDisposition);
if (ERROR_SUCCESS != hr)
return FAIL;
if(ERROR_SUCCESS != RegQueryValueEx(hKey, "Port", \
NULL, &dwType, (BYTE*)&m_iPortID, &cbData) )
{
return FAIL;
}
if(ERROR_SUCCESS != RegQueryValueEx(hKey, "BaudRate", NULL, &dwType, (BYTE*)&m_iBaudRate, &cbData))
{
return FAIL;
}
if (ERROR_SUCCESS != RegQueryValueEx(hKey, "ByteSize", NULL, &dwType, (BYTE*)&m_iBytesize, &cbData))
{
return FAIL;
}
if (ERROR_SUCCESS != RegQueryValueEx(hKey, "Parity", NULL, &dwType, (BYTE*)&m_iParity, &cbData))
{
return FAIL;
}
if (ERROR_SUCCESS != RegQueryValueEx(hKey, "StopBits", NULL, &dwType, (BYTE*)&m_iStopBits, &cbData))
{
return FAIL;
}
if (SUCCESS!= InitComm())
{
_ret->code = FAIL;
_ret->discription = _T("注冊(cè)表?yè)p壞,請(qǐng)重新添加注冊(cè)表文件!");
_ret->time = CTime::GetCurrentTime();
}
return _ret->code = SUCCESS;
}
int COFxPLCDev::SendENQ(_retstruct *_ret)
{
char chStrRET[2];
memset(chStrRET, 0x00, sizeof(chStrRET));
char chStrENQ[2];
memset(chStrENQ, 0x00, sizeof(chStrENQ));
chStrENQ[0] = ENQ;
int iLen = 0;
if (SUCCESS != WritePort((BYTE*)chStrENQ, strlen(chStrENQ), 300))
return FAIL;
if(SUCCESS == ReadPort((BYTE*)chStrENQ, iLen, 300))
{
if(chStrENQ[0] == ACK)
{
_ret->code = ACK;
_ret->time = CTime::GetCurrentTime();
_ret->discription = "LCA連接成功。";
}
else if(chStrENQ[0] == NAK)
{
_ret->code = NAK;
_ret->time = CTime::GetCurrentTime();
_ret->discription = "LCA連接失敗。返回NAK。";
}
else
{
_ret->code = FAIL;
_ret->time = CTime::GetCurrentTime();
_ret->discription = "LCA連接失敗。其他未知錯(cuò)誤。";
}
return _ret->code;
}
else
{
_ret->code = FAIL;
_ret->time = CTime::GetCurrentTime();
_ret->discription = "LCA連接失敗。請(qǐng)檢查通信線路是否正常!";
}
return _ret->code;
}
int COFxPLCDev::GetSpeed(_retstruct *_ret, int istation, int &Speed)
{
/*
執(zhí)行步驟:
1、組合命令;
2、將數(shù)據(jù)添加檢驗(yàn)和,并添加命令首部
3、WritePort
4、ReadPort
5、解析返回?cái)?shù)據(jù),并將數(shù)據(jù)送給Speed.
*/
if (0>istation || istation>32)
{
_ret->code = FAIL;
_ret->discription = _T(" 無(wú)效的站點(diǎn)");
_ret->time = CTime::GetCurrentTime();
}
//添加站號(hào) istation
CString station;
station.Format("%c", istation+0x30);
//添加指令[0][5]
CString cmdstr;
cmdstr = "\x30\x35";
//添加參數(shù)[0][9]
CString cmdpra;
cmdpra = "\x30\x39";
//組成命令
char chCmd[128];
memset(chCmd, 0x00, sizeof(chCmd));
CString strcmd;
strcmd = station +cmdstr + "\x02"+ cmdpra +"\x03";
int strcmdlen = 0;
strcmdlen = strlen(strcmd);
memcpy(chCmd, strcmd, strcmdlen);
BYTE chSum = 0;
check_sum((BYTE*)chCmd, strcmdlen , chSum);
CString schSum;
schSum.Format("%02X", chSum);
CString strfin;
strfin = "\x01" + strcmd +schSum;
int strfinlen = 0;
strfinlen = strlen(strfin);
memset(chCmd, 0x00, sizeof(chCmd));
memcpy(chCmd, strfin, strfinlen);
//發(fā)送命令
if (SUCCESS != WritePort((BYTE*)chCmd, strfinlen, 10))
{
_ret->code = FAIL;
_ret->discription = "寫入數(shù)據(jù)錯(cuò)誤!";
_ret->time = CTime::GetCurrentTime();
}
else
{
//取得響應(yīng)并解析值
char recbuf[128];
memset(recbuf, 0x00, sizeof(recbuf));
int ilen = 128;
if (SUCCESS != ReadPort((BYTE*)recbuf, ilen,90))
{
_ret->code = FAIL;
_ret->discription = "讀取數(shù)據(jù)錯(cuò)誤!";
_ret->time = CTime::GetCurrentTime();
}
else
{
char rbuf[128];
memset(rbuf, 0x00, sizeof(rbuf));
int rlen = 128;
if( SUCCESS == get_response((BYTE *)recbuf, ilen, (BYTE *)rbuf, rlen, _ret))
{
Speed = 0;
for (int i = 1; i<8; i++)
{
if (0x40<rbuf[i]&&rbuf[i]<0x47)
rbuf[i] = rbuf[i] - 7;
Speed += (rbuf[i]-0x30)*pow(16, 7-i);
}
_ret->code = SUCCESS;
}
}
}
return _ret->code;
}
int COFxPLCDev::SetSpeed(_retstruct *_ret, int istation, int Speed)
{
/*
執(zhí)行步驟:
1、組合命令;
2、將數(shù)據(jù)添加檢驗(yàn)和,并添加命令首部
3、WritePort
4、ReadPort
5、解析返回?cái)?shù)據(jù),是否設(shè)置成功.
*/
if (0>istation || istation>32)
{
_ret->code = FAIL;
_ret->discription = _T(" 無(wú)效的站點(diǎn)");
_ret->time = CTime::GetCurrentTime();
}
//添加站號(hào) istation
CString station;
station.Format("%c", istation+0x30);
//添加指令[0][5]
CString cmdstr;
cmdstr = "\x38\x34";
//添加參數(shù)[0][9]
CString cmdpra;
cmdpra = "\x30\x39";
//將Speed轉(zhuǎn)換成8個(gè)16進(jìn)制字符
CString cmdSpeed;
cmdSpeed.Format("%08X", Speed);
//組成命令
char chCmd[128];
memset(chCmd, 0x00, sizeof(chCmd));
CString strcmd;
strcmd = station +cmdstr + "\x02"+ cmdpra + cmdSpeed +"\x03";
int strcmdlen = 0;
strcmdlen = strlen(strcmd);
memcpy(chCmd, strcmd, strcmdlen);
BYTE chSum = 0;
check_sum((BYTE*)chCmd, strcmdlen , chSum);
CString schSum;
schSum.Format("%02X", chSum);
CString strfin;
strfin = "\x01" + strcmd +schSum;
int strfinlen = 0;
strfinlen = strlen(strfin);
memset(chCmd, 0x00, sizeof(chCmd));
memcpy(chCmd, strfin, strfinlen);
//發(fā)送命令
if (SUCCESS != WritePort((BYTE*)chCmd, strfinlen, 10))
{
_ret->code = FAIL;
_ret->discription = "寫入數(shù)據(jù)錯(cuò)誤!";
_ret->time = CTime::GetCurrentTime();
}
else
{
//取得響應(yīng)并解析值
char recbuf[128];
memset(recbuf, 0x00, sizeof(recbuf));
int ilen = 128;
if (SUCCESS != ReadPort((BYTE*)recbuf, ilen,200))
{
_ret->code = FAIL;
_ret->discription = "讀取數(shù)據(jù)錯(cuò)誤!";
_ret->time = CTime::GetCurrentTime();
}
else
{
char rbuf[128];
memset(rbuf, 0x00, sizeof(rbuf));
int rlen = 128;
if( SUCCESS == get_response((BYTE *)recbuf, ilen, (BYTE *)rbuf, rlen, _ret))
{
Speed = 0;
for (int i = 1; i<8; i++)
{
if (0x40<rbuf[i]&&rbuf[i]<0x47)
rbuf[i] = rbuf[i] - 7;
Speed += (rbuf[i]-0x30)*pow(16, 7-i);
}
_ret->code = SUCCESS;
}
}
}
return _ret->code;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -