#pragma once
#include "afxwin.h"
#include <afxsock.h>
#include <afxinet.h>
class CRobotControl
{
public:
CRobotControl(void);
~CRobotControl(void);
public:
BOOL RobotSocketConnect(CString m_RobotIP, int strport);
BOOL RobotFTPConnect(CString m_RobotIP, CString strusr, int strport);
BOOL RobotSendFile(CString str, CString strname);
void RobotSend(CString strOrdel);
char* CStringToChar(CString strCLIPath);
void RobotSocketClose();
public:
CInternetSession *pRobotSession;
CFtpConnection *pRobotConnection;
SOCKET m_hSocket;
struct sockaddr_in local;
};
#include "StdAfx.h"
#include "RobotControl.h"
CRobotControl::CRobotControl(void)
{
pRobotConnection = NULL;
pRobotSession = NULL;
m_hSocket = NULL;
}
CRobotControl::~CRobotControl(void)
{
RobotSocketClose();
if (pRobotSession)
{
pRobotSession->Close();
delete pRobotSession;
}
}
void CRobotControl::RobotSocketClose()
{
closesocket(m_hSocket);
if (m_hSocket != NULL)
{
m_hSocket = NULL;
}
pRobotConnection = NULL;
}
BOOL CRobotControl::RobotSocketConnect(CString m_RobotIP, int strport)
{
AfxSocketInit();
m_hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_hSocket == SOCKET_ERROR)
{
return FALSE;
}
unsigned long ul = 1;
int ret = ioctlsocket(m_hSocket, FIONBIO, (unsigned long*)&ul);
if(ret==SOCKET_ERROR)
return FALSE;
char cip[30];
wcstombs(cip, m_RobotIP, m_RobotIP.GetLength() * 2);
local.sin_family = AF_INET;
local.sin_addr.S_un.S_addr = inet_addr(cip);
local.sin_port = htons(strport);
connect(m_hSocket, (sockaddr *)&local, sizeof(local));
ul = 0;
ret = ioctlsocket(m_hSocket, FIONBIO, (unsigned long*)&ul);
if(ret==SOCKET_ERROR)
return FALSE;
struct timeval timeout;
fd_set set;
FD_ZERO(&set);
FD_SET(m_hSocket, &set);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
if( select(0, NULL, &set, NULL, &timeout) <= 0)
{
closesocket (m_hSocket);
m_hSocket = INVALID_SOCKET;
AfxMessageBox(_T("连接失败,请核对IP和端口!"));
return FALSE;
}
return TRUE;
}
BOOL CRobotControl::RobotFTPConnect(CString m_RobotIP, CString strusr, int strport)
{
pRobotSession = new CInternetSession(_T(""), 1, INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY);
pRobotSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1, 0);
try
{
pRobotConnection = pRobotSession->GetFtpConnection(m_RobotIP, _T("maintenance"), _T("spec_cal"), strport, FALSE);
if(pRobotConnection)
{
return TRUE;
}
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr,1024);
pEx->Delete();
pRobotConnection = NULL;
return FALSE;
}
return FALSE;
}
void CRobotControl::RobotSend(CString strOrdel)
{
char *nSocketOrdel = CStringToChar(strOrdel);
if (m_hSocket)
{
sendto(m_hSocket, nSocketOrdel, (strlen(nSocketOrdel)), 0, (LPSOCKADDR)&local, sizeof(local));
}
else
{
AfxMessageBox(_T("请连接网络!"));
}
}
BOOL CRobotControl::RobotSendFile(CString str, CString strname)
{
if (pRobotConnection == NULL)
{
AfxMessageBox(_T("FTP未连接成功!"));
return FALSE;
}
CString strdir = _T("/usr/usrapp/saveCAM/");
pRobotConnection->SetCurrentDirectory(strdir);
BOOL bput=pRobotConnection->PutFile((LPCTSTR)str,(LPCTSTR)strname);
if (bput)
{
pRobotConnection->SetCurrentDirectory(strdir);
return TRUE;
}
else
{
return FALSE;
}
}
char* CRobotControl::CStringToChar(CString strCLIPath)
{
DWORD dCliPath = WideCharToMultiByte(CP_OEMCP, NULL, strCLIPath, -1, NULL, NULL, 0, NULL);
char *nCliPaths = new char[dCliPath];
WideCharToMultiByte(CP_OEMCP, NULL, strCLIPath, -1, nCliPaths, dCliPath, 0, NULL);
return nCliPaths;
}
void CPrinterControl::PrinterOrder(CString strOrder)
{
if (pFtpConnection)
{
try
{
pFtpConnection->Command(strOrder, CFtpConnection::CmdRespNone, FTP_TRANSFER_TYPE_BINARY, 1);
}
catch (CInternetException* pEx)
{
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr,1024);
pEx->Delete();
}
}
}
int CPrinterControl::PrinterBackInfo()
{
DWORD dwError, dwBufferLength;
LPTSTR strError;
int ntime = -1;
if( !InternetGetLastResponseInfo(&dwError, NULL, &dwBufferLength) )
{
strError = new TCHAR[dwBufferLength + 1];
InternetGetLastResponseInfo(&dwError, strError, &dwBufferLength);
CString strRemain(_T("NULL"));
CString strE(_T(""));
strE.Format( _T("%s"), strError );
strRemain.Format(_T("%s"), (CStringW)(strstr(CStringToChar(strE), "Remain")));
if (strRemain.GetLength() > 5)
{
ntime = _ttoi(strRemain.Mid(8, 4));
}
else
{
strE.Format(_T("%s"), (CStringW)(strstr(CStringToChar(strE), "Finish")));
if (strE.GetLength() > 5)
{
ntime = -1;
}
}
delete[] strError;
}
return ntime;
}