C++Builder XE6开发海康设备控制软件,获取报警主机的信息,研究了3、4天,主要是回调函数问题,最后通过仔细研究海康的帮助文档,终于搞定,直接上代码了。
Main.cpp代码:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
void CALLBACK MsgCallback(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char* pAlarmInfo, DWORD dwBufLen, void* pUser);
void ProcessCommAlarm_CIDAlarm(NET_DVR_ALARMER *pAlarmer, char* pAlarmInfo, DWORD dwBufLen, void* pUser);
void UpdateClientList(AnsiString strAlarmTime, AnsiString strDevIP, AnsiString strAlarmMsg);
void *UpdateListBoxCallback(AnsiString strAlarmTime, AnsiString strDevIP,AnsiString strAlarmMsg);
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
Dir = ExtractFilePath(Application->ExeName) ;
m_lUserID = -1 ;
iListenHandle = -1 ;
iDeviceNumber = 0 ; //添加设备个数
iLastErr = 0 ;
CreateDirectory(String(Dir + "SdkLog").c_str(),NULL);
//CreateDirectory(String(Dir + "CapPic").c_str(),NULL);
//CreateDirectory(String(Dir + "Record").c_str(),NULL);
m_bInitSDK = NET_DVR_Init() ;
if (m_bInitSDK == false)
{
ShowMessage("NET_DVR_Init初始化失败!") ;
return;
}
else
{
char strIP[16][16];
DWORD dwValidNum = 0 ;
BOOL bEnableBind = false ;
//void* pUser = 0 ;
//获取本地PC网卡IP信息
if(NET_DVR_GetLocalIP(strIP, &dwValidNum, &bEnableBind))
{
if (dwValidNum > 0)
{
String StrIP = "";
//取第一张网卡的IP地址为默认监听端口
for(int j=0;j<16;j++)
StrIP += String(strIP[0][j]) ;
edtListenIP->Text = StrIP ;
}
}
//保存SDK日志
NET_DVR_SetLogToFile(3, AnsiString(Dir+ "SdkLog\\").c_str(), true);
for (int i = 0; i < 200; i++)
{
m_lAlarmHandle[i] = -1 ;
}
//设置报警回调函数
NET_DVR_SetDVRMessageCallBack_V30(MsgCallback, NULL);
}
}
//---------------------------------------------------------------------------
//****************************相关函数***************************************
//---------------------------------------------------------------------------
void CALLBACK MsgCallback(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char* pAlarmInfo, DWORD dwBufLen, void* pUser)
{
//通过lCommand来判断接收到的报警信息类型,不同的lCommand对应不同的pAlarmInfo内容
switch (lCommand)
{
case COMM_ALARMHOST_CID_ALARM://报警主机CID报警上传
ProcessCommAlarm_CIDAlarm(pAlarmer, pAlarmInfo, dwBufLen, pUser);
break;
default:
break;
}
}
//---------------------------------------------------------------------------
void ProcessCommAlarm_CIDAlarm(NET_DVR_ALARMER *pAlarmer, char* pAlarmInfo, DWORD dwBufLen, void* pUser)
{
NET_DVR_CID_ALARM struCIDAlarm ;
//unsigned int dwSize = (unsigned int)sizeof(struCIDAlarm) ;
memcpy(&struCIDAlarm, pAlarmInfo, sizeof(NET_DVR_CID_ALARM)) ;
//报警设备IP地址
AnsiString strIP = pAlarmer->sDeviceIP ;
//报警时间:年月日时分秒
AnsiString strTimeYear = AnsiString(struCIDAlarm.struTriggerTime.wYear) ;
AnsiString strTimeMonth = AnsiString(struCIDAlarm.struTriggerTime.byMonth) ;
AnsiString strTimeDay = AnsiString(struCIDAlarm.struTriggerTime.byDay) ;
AnsiString strTimeHour = AnsiString(struCIDAlarm.struTriggerTime.byHour) ;
AnsiString strTimeMinute = AnsiString(struCIDAlarm.struTriggerTime.byMinute) ;
AnsiString strTimeSecond = AnsiString(struCIDAlarm.struTriggerTime.bySecond) ;
AnsiString strTime = strTimeYear + "-" + strTimeMonth + "-" + strTimeDay + " "
+ strTimeHour + ":" + strTimeMinute + ":" + strTimeSecond ;
AnsiString strAlarm = "报警主机CID报告,sCIDCode:" + AnsiString(int(struCIDAlarm.sCIDCode))
+ ",sCIDDescribe:" + AnsiString(int(struCIDAlarm.sCIDDescribe))
+ ",报告类型:" + AnsiString(int(struCIDAlarm.byReportType))
+ ",防区号:" + AnsiString(int(struCIDAlarm.wDefenceNo))
+ ",报警触发时间:" + strTime ;
//创建该控件的主线程直接更新信息列表
UpdateClientList(Now().FormatString("yyyy年mm月dd日 hh时nn分ss秒"), strIP,strAlarm);
}
//---------------------------------------------------------------------------
void UpdateClientList(AnsiString strAlarmTime, AnsiString strDevIP, AnsiString strAlarmMsg)
{
//列表新增报警信息
//lvAlarmInfo.Items.Add(new ListViewItem(new AnsiString[] { strAlarmTime, strDevIP, strAlarmMsg }));
TListItem *liAlarmInfo;
liAlarmInfo = frmMain->lvAlarmInfo->Items->Add() ;
liAlarmInfo->Caption = strAlarmTime ;
liAlarmInfo->SubItems->Add(strDevIP) ;
liAlarmInfo->SubItems->Add(strAlarmMsg) ;
}
//---------------------------------------------------------------------------
//添加设备
void __fastcall TfrmMain::btnLoginClick(TObject *Sender)
{
if (edtIPAddress->Text==""||edtPortNumber->Text==""
||edtUserName->Text==""||edtPassword->Text=="")
{
ShowMessage("请输入设备地址、端口、用户名和密码!") ;
return ;
}
if (iDeviceNumber >= 20)
{
ShowMessage("本程序限制最多添加20台设备!");
return;
}
DVRIPAddress = edtIPAddress->Text ; //设备IP地址或者域名
DVRPortNumber = edtPortNumber->Text.ToInt() ; //设备服务端口号
DVRUserName = edtUserName->Text ; //设备登录用户名
DVRPassword = edtPassword->Text ; //设备登录密码
NET_DVR_DEVICEINFO_V30 DeviceInfo ;
//登录设备
m_lUserID = NET_DVR_Login_V30(DVRIPAddress.c_str(), DVRPortNumber, DVRUserName.c_str(), DVRPassword.c_str(), &DeviceInfo) ;
if (m_lUserID < 0)
{
iLastErr = NET_DVR_GetLastError() ;
//登录失败,输出错误号
ShowMessage("NET_DVR_Login_V30登录失败,错误代码" + String(iLastErr)) ;
}
else
{
//登录成功
ShowMessage(DVRIPAddress+"登录成功") ;
iDeviceNumber++;
TListItem *liDevice;
liDevice = lvDevice->Items->Add() ; //将已注册设备添加进列表
liDevice->Caption = AnsiString(m_lUserID) ;
liDevice->SubItems->Add(DVRIPAddress) ;
liDevice->SubItems->Add("未布防") ;
}
}
//---------------------------------------------------------------------------
//全部布防
void __fastcall TfrmMain::btnSetAlarmClick(TObject *Sender)
{
NET_DVR_SETUPALARM_PARAM struAlarmParam ;
struAlarmParam.dwSize = (unsigned int)sizeof(struAlarmParam);
struAlarmParam.byLevel = 1 ; //0- 一级布防,1- 二级布防
struAlarmParam.byAlarmInfoType = 1 ; //智能交通设备有效,新报警信息类型
struAlarmParam.byFaceAlarmDetection = 1 ; //1-人脸侦测
for (int i = 0; i < iDeviceNumber; i++)
{
m_lUserID = lvDevice->Items->Item[i]->Caption.ToInt() ;
m_lAlarmHandle[m_lUserID] = NET_DVR_SetupAlarmChan_V41(m_lUserID, &struAlarmParam) ;
if (m_lAlarmHandle[m_lUserID] < 0)
{
iLastErr = NET_DVR_GetLastError() ;
strErr = "布防失败,错误号:" + AnsiString(iLastErr) ; //布防失败,输出错误号
lvDevice->Items->Item[i]->SubItems->Strings[1] = strErr ;
}
else
{
lvDevice->Items->Item[i]->SubItems->Strings[1] = "布防成功" ;
}
btnSetAlarm->Enabled = false ;
}
}
//---------------------------------------------------------------------------
//全部撤防
void __fastcall TfrmMain::btnCloseAlarmClick(TObject *Sender)
{
for (int i = 0; i < iDeviceNumber; i++)
{
m_lUserID = lvDevice->Items->Item[i]->Caption.ToInt() ;
if (m_lAlarmHandle[m_lUserID] >= 0)
{
if(!NET_DVR_CloseAlarmChan_V30(m_lAlarmHandle[m_lUserID]))
{
iLastErr = NET_DVR_GetLastError() ;
strErr = "撤防失败,错误号:" + AnsiString(iLastErr) ; //撤防失败,输出错误号
lvDevice->Items->Item[i]->SubItems->Strings[1] = strErr ;
}
else
{
lvDevice->Items->Item[i]->SubItems->Strings[1] = "未布防" ;
m_lAlarmHandle[i] = -1;
}
}
else
{
lvDevice->Items->Item[i]->SubItems->Strings[1] = "未布防" ;
}
}
btnSetAlarm->Enabled = true ;
}
//---------------------------------------------------------------------------
//启动监听
void __fastcall TfrmMain::btnStartListenClick(TObject *Sender)
{
AnsiString sLocalIP = edtListenIP->Text ;
unsigned int wLocalPort = edtListenPort->Text.ToInt() ;
void* pUser = 0 ;
iListenHandle = NET_DVR_StartListen_V30(sLocalIP.c_str(), wLocalPort, m_falarmData, pUser) ;
if (iListenHandle < 0)
{
iLastErr = NET_DVR_GetLastError() ;
strErr = "启动监听失败,错误号:" + AnsiString(iLastErr) ; //撤防失败,输出错误号
ShowMessage(strErr) ;
}
else
{
ShowMessage("成功启动监听!") ;
btnStopListen->Enabled = true ;
btnStartListen->Enabled = false ;
}
}
//---------------------------------------------------------------------------
//停止监听
void __fastcall TfrmMain::btnStopListenClick(TObject *Sender)
{
if(NET_DVR_StopListen_V30(iListenHandle))
{
iLastErr = NET_DVR_GetLastError() ;
strErr = "停止监听失败,错误号:" + AnsiString(iLastErr) ; //撤防失败,输出错误号
ShowMessage(strErr) ;
}
else
{
ShowMessage("停止监听!") ;
btnStopListen->Enabled = false ;
btnStartListen->Enabled = true ;
}
}
//---------------------------------------------------------------------------
Main.h代码:
//---------------------------------------------------------------------------
#ifndef MainH
#define MainH
//---------------------------------------------------------------------------
#include "HCNetSDK.h"
#include "AlarmDemoPCH.h"
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
template
dst_type pointer_cast(src_type src)
{
return *static_cast(static_cast(&src));
}
class TfrmMain : public TForm
{
__published: // IDE-managed Components
TButton *btnLogin;
TEdit *edtPortNumber;
TEdit *edtPassword;
TLabel *lblPsw;
TLabel *lblPort;
TEdit *edtUserName;
TLabel *lblUser;
TEdit *edtIPAddress;
TLabel *lblIP;
TListView *lvAlarmInfo;
TListView *lvDevice;
TPanel *pnlListen;
TButton *btnStartListen;
TEdit *edtListenPort;
TLabel *lbllListenPort;
TEdit *edtListenIP;
TLabel *lblListenIP;
TButton *btnStopListen;
TButton *btnCloseAlarm;
TButton *btnSetAlarm;
TLabel *Label1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall btnLoginClick(TObject *Sender);
void __fastcall btnSetAlarmClick(TObject *Sender);
void __fastcall btnCloseAlarmClick(TObject *Sender);
void __fastcall btnStartListenClick(TObject *Sender);
void __fastcall btnStopListenClick(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TfrmMain(TComponent* Owner);
unsigned int iLastErr;
int m_lUserID,m_lAlarmHandle[200],iListenHandle,iDeviceNumber,DVRPortNumber ;
bool m_bInitSDK;
AnsiString Dir,DVRIPAddress,DVRUserName,DVRPassword,strErr;
NET_VCA_TRAVERSE_PLANE m_struTraversePlane;
MSGCallBack m_falarmData;
NET_VCA_INTRUSION m_struIntrusion;
NET_DVR_PDC_ALRAM_INFO m_struStatFrame;
NET_DVR_PDC_ALRAM_INFO m_struStatTime;
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmMain *frmMain;
//---------------------------------------------------------------------------
#endif