不同云台的协议可能不一样,我这里有一个,不知道合不合你用。
//类型定义
typedef struct _CAMERAINFO
{
CHAR Name[32];
BOOL bIsQuickBall; //快球标志
BOOL bHasPanTielt; //云台标志
BOOL bHasMicrophone; //麦克风标志
CHAR szComment[30];
int nQuickBallSpeed;
}CAMERAINFO,CAMERAINFOMATION;
基类:
头文件
// DecoderBase.h: interface for the CDecoderBase class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_DECODERBASE_H__9761EEC3_545E_4FC0_94F4_BAA0DC21AF3C__INCLUDED_)
#define AFX_DECODERBASE_H__9761EEC3_545E_4FC0_94F4_BAA0DC21AF3C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMSComm;
class CDecoderBase
{
public:
CDecoderBase();
~CDecoderBase();
static CMSComm * m_spComm;
int * m_pDecoderID;
CAMERAINFO * m_pDecoderInfo;
public:
virtual VOID Initialize(int nBoardNo);
virtual VOID SetPanTielt(int nAction=0)=0; //设置云台,nAction:0表示自动,1向上,2向下,3向左,4向右
virtual VOID GetPreset(int nIndex,LPSTR szName=NULL)=0; //预置调用,nIndex表示第几个预置位
virtual VOID SetPreset(int nIndex=0,LPSTR szName=NULL)=0; //预置设置,nIndex表示第几个预置位
virtual VOID SetZoom(BOOL bIn=TRUE)=0; //设置镜头倍数,bIn=TRUE表示放大
virtual VOID SetAperture(BOOL bOpen=TRUE)=0; //设置光圈,bOpen表示打开
virtual VOID SetFocus(BOOL bFar=TRUE)=0; //设置焦距,bFar=TRUE表示将焦点调远
BYTE SumArr(LPBYTE pArr,int nCount=7);
};
#endif // !defined(AFX_DECODERBASE_H__9761EEC3_545E_4FC0_94F4_BAA0DC21AF3C__INCLUDED_)
//cpp文件
// DecoderBase.cpp: implementation of the CDecoderBase class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "client2.h"
#include "mscomm.h"
#include "DecoderBase.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMSComm * CDecoderBase::m_spComm=NULL;
CDecoderBase::CDecoderBase()
{
}
CDecoderBase::~CDecoderBase()
{
}
VOID CDecoderBase::Initialize(int nBoardNo)
{
}
BYTE CDecoderBase::SumArr(LPBYTE pArr,int nCount)
{
BYTE sum=0;
for(int i=0;i<nCount;i++)
{
sum=sum+pArr[i];
}
return sum;
}
//具体的解码器(云台),此处为Alex快球
//头文件
// Alex.h: interface for the CAlex class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_ALEX_H__7DCEEB8B_3C82_4ED7_9BB7_409E63120296__INCLUDED_)
#define AFX_ALEX_H__7DCEEB8B_3C82_4ED7_9BB7_409E63120296__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "DecoderBase.h"
#include "mscomm.h"
class CAlex : public CDecoderBase
{
public:
VOID SetFocus(BOOL bFar=TRUE);
VOID SetAperture(BOOL bOpen=TRUE);
VOID SetZoom(BOOL bIn=TRUE);
VOID SetPanTielt(int nAction=0);
VOID GetPreset(int nIndex, LPSTR szName);
VOID SetPreset(int nIndex = -1, LPSTR szName=NULL);
VOID Initialize(int nBoardNo);
VOID ClearPreset();
CAlex();
CAlex(CMSComm* pComm,CDecoder* pParentDecoder);
virtual ~CAlex();
int m_nPresetCount;
BYTE m_byCmd[7];
CByteArray m_byArrCmd;
};
#endif // !defined(AFX_ALEX_H__7DCEEB8B_3C82_4ED7_9BB7_409E63120296__INCLUDED_)
//cpp文件
// Alex.cpp: implementation of the CAlex class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "client2.h"
#include "Alex.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//建构函数,参数CMSComm* pComm用于向解码器发送数据,
//CDecoder* pParentDecoder用于取得已经保存好的设置信息,
//例如将快球的速度保存等等,具体保存内容见CAMERAINFO,对于一般的应用可以不要
CAlex::CAlex(CMSComm* pComm,CDecoder* pParentDecoder)
{
if(!m_spComm)
m_spComm=pComm;
if(!m_spComm->GetPortOpen())
{
m_spComm->SetCommPort(1);
m_spComm->SetSettings("4800,N,8,1");
m_spComm->SetPortOpen(TRUE);
}
m_pDecoderInfo=&pParentDecoder->m_DecoderInfo;
Initialize(pParentDecoder->m_nDecoderID );
ZeroMemory(m_byCmd,sizeof(m_byCmd));
m_nPresetCount=1;
}
CAlex::CAlex()
{
}
CAlex::~CAlex()
{
SetPanTielt(5); //先将云台停止动作再退出
}
VOID CAlex::Initialize(int nBoardNo) //初始化解码器,同时设置云台速度
{
//请对照协议填充命令位
m_byArrCmd.SetSize(6);
m_byCmd[0]=0x02;
m_byCmd[1]=0x01;
m_byCmd[2]=14;
m_byCmd[3]=m_pDecoderInfo->nQuickBallSpeed;
m_byCmd[4]=m_byCmd[3];
m_byCmd[5]=0;
m_byCmd[5]=SumArr(m_byCmd,6);
for(int j=0;j<m_byArrCmd.GetSize();j++)
{
m_byArrCmd.SetAt(j,m_byCmd[j]);
}
//利用串口向解码器发送数据
m_spComm->SetOutput(COleVariant(m_byArrCmd));
}
VOID CAlex::SetPreset(int nIndex, LPSTR szName) //设置预置位
{
m_byCmd[0]=0x02;
m_byCmd[1]=0x01;
m_byCmd[2]=0x05;
if(nIndex==-1)
m_byCmd[3]=m_nPresetCount;
else
m_byCmd[3]=nIndex;
m_byCmd[4]=0;
m_byCmd[4]=SumArr(m_byCmd,5);
m_byArrCmd.SetSize(5);
for(int j=0;j<m_byArrCmd.GetSize();j++)
{
m_byArrCmd.SetAt(j,m_byCmd[j]);
}
m_spComm->SetOutput(COleVariant(m_byArrCmd));
m_nPresetCount++;
}
VOID CAlex::GetPreset(int nIndex, LPSTR szName) //调用预置位
{
m_byCmd[0]=0x02;
m_byCmd[1]=0x01;
m_byCmd[2]=0x02;
m_byCmd[3]=nIndex;
m_byCmd[4]=0;
m_byCmd[4]=SumArr(m_byCmd,5);
if(m_byArrCmd.GetSize())
m_byArrCmd.RemoveAll();
m_byArrCmd.SetSize(5);
for(int j=0;j<m_byArrCmd.GetSize();j++)
{
m_byArrCmd.SetAt(j,m_byCmd[j]);
}
m_spComm->SetOutput(COleVariant(m_byArrCmd));
}
VOID CAlex::SetPanTielt(int nAction)
{
//设置云台,nAction:1向上,2向下,3向左,4向右,5停止动作
m_byCmd[0]=0x02;
m_byCmd[1]=0x01;
m_byCmd[2]=0x01;
m_byCmd[4]=0x00;
m_byCmd[5]=0;
switch(nAction)
{
case 1:
m_byCmd[3]=0X04;
m_byCmd[5]=SumArr(m_byCmd);
break;
case 2:
m_byCmd[3]=0X08;
m_byCmd[5]=SumArr(m_byCmd);
break;
case 3:
m_byCmd[3]=0X02;
m_byCmd[5]=SumArr(m_byCmd);
break;
case 4:
m_byCmd[3]=0X01;
m_byCmd[5]=SumArr(m_byCmd);
break;
case 5:
m_byCmd[3]=0X00;
m_byCmd[5]=SumArr(m_byCmd);
break;
}
if(m_byArrC