1.4.5 PelcoD類
PelcoD類主要封裝了控制云臺(tái)的Pelco-D協(xié)議,該協(xié)議由7個(gè)字節(jié)構(gòu)成。第1個(gè)字節(jié)為同步字節(jié),始終為FFH;第2個(gè)字節(jié)為地址碼,也就是攝像頭的邏輯地址號(hào);第3、4個(gè)字節(jié)表示指令碼,即執(zhí)行哪項(xiàng)操作;第5、6個(gè)字節(jié)表示數(shù)據(jù)碼,用于指定攝像頭的水平、垂直方向移動(dòng)速度;第7個(gè)字節(jié)為校驗(yàn)碼。PelcoD類中通過將Pelco-D協(xié)議中的7個(gè)字節(jié)返回為串口消息值來對(duì)云臺(tái)進(jìn)行控制,其關(guān)鍵代碼如下:
例程10 代碼位置:光盤\TM\01\VWMS\VWMS\CommonClass\PelcoD.cs
private string watchdir = ""; //監(jiān)控方向
private static readonly byte STX = 0xFF; //同步字節(jié)
#region 監(jiān)控方向和定時(shí)監(jiān)控實(shí)體
public string WatchDir
{
get
{
return watchdir;
}
set
{
watchdir = value;
}
}
#endregion
#region 基本指令定義
#region 指令碼
private const byte FocusNear = 0x01; //增加聚焦
private const byte IrisOpen = 0x02; //減小光圈
private const byte IrisClose = 0x04; //增加光圈
private const byte CameraOnOff = 0x08; //攝像機(jī)打開和關(guān)閉
private const byte AutoManualScan = 0x10; //自動(dòng)和手動(dòng)掃描
private const byte Sense = 0x80; //Sense碼
#endregion
#region 指令碼
private const byte PanRight = 0x02; //右
private const byte PanLeft = 0x04; //左
private const byte TiltUp = 0x08; //上
private const byte TiltDown = 0x10; //下
private const byte ZoomTele = 0x20; //增加對(duì)焦
private const byte ZoomWide = 0x40; //減小對(duì)焦
private const byte FocusFar = 0x80; //減小聚焦
#endregion
#region 鏡頭左右平移的速度
private const byte PanSpeedMin = 0x00; //停止
private const byte PanSpeedMax = 0xFF; //最高速
#endregion
#region 鏡頭上下移動(dòng)的速度
private const byte TiltSpeedMin = 0x00; //停止
private const byte TiltSpeedMax = 0x3F; //最高速
#endregion
#endregion
#region 云臺(tái)控制枚舉
public enum Switch { On = 0x01, Off = 0x02 } //雨刷控制
public enum Focus { Near = FocusNear, Far = FocusFar } //聚焦控制
public enum Zoom { Wide = ZoomWide, Tele = ZoomTele } //對(duì)焦控制
public enum Tilt { Up = TiltUp, Down = TiltDown } //上下控制
public enum Pan { Left = PanLeft, Right = PanRight } //左右控制
public enum Scan { Auto, Manual } //自動(dòng)和手動(dòng)控制
public enum Iris { Open = IrisOpen, Close = IrisClose } //光圈控制
#endregion
#region 云臺(tái)控制方法
//雨刷控制
public byte[] CameraSwitch(uint deviceAddress, Switch action)
{
byte m_action = CameraOnOff;
if (action == Switch.On)
m_action = CameraOnOff + Sense;
return Message.GetMessage(deviceAddress, m_action, 0x00, 0x00, 0x00);
}
//光圈控制
public byte[] CameraIrisSwitch(uint deviceAddress, Iris action)
{
return Message.GetMessage(deviceAddress, (byte)action, 0x00, 0x00, 0x00);
}
//聚焦控制
public byte[] CameraFocus(uint deviceAddress, Focus action)
{
if (action == Focus.Near)
return Message.GetMessage(deviceAddress, (byte)action, 0x00, 0x00, 0x00);
else
return Message.GetMessage(deviceAddress, 0x00, (byte)action, 0x00, 0x00);
}
//對(duì)焦控制
public byte[] CameraZoom(uint deviceAddress, Zoom action)
{
return Message.GetMessage(deviceAddress, 0x00, (byte)action, 0x00, 0x00);
}
//上下控制
public byte[] CameraTilt(uint deviceAddress, Tilt action, uint speed)
{
if (speed < TiltSpeedMin)
speed = TiltSpeedMin;
if (speed < TiltSpeedMax)
speed = TiltSpeedMax;
return Message.GetMessage(deviceAddress, 0x00, (byte)action, 0x00, (byte)speed);
}
//左右控制
public byte[] CameraPan(uint deviceAddress, Pan action, uint speed)
{
if (speed < PanSpeedMin)
speed = PanSpeedMin;
if (speed < PanSpeedMax)
speed = PanSpeedMax;
return Message.GetMessage(deviceAddress, 0x00, (byte)action, (byte)speed, 0x00);
}
//停止云臺(tái)的移動(dòng)
public byte[] CameraStop(uint deviceAddress)
{
return Message.GetMessage(deviceAddress, 0x00, 0x00, 0x00, 0x00);
}
//自動(dòng)和手動(dòng)控制
public byte[] CameraScan(uint deviceAddress, Scan scan)
{
byte m_byte = AutoManualScan;
if (scan == Scan.Auto)
m_byte = AutoManualScan + Sense;
return Message.GetMessage(deviceAddress, m_byte, 0x00, 0x00, 0x00);
}
#endregion
public struct Message
{
public static byte Address;
public static byte CheckSum;
public static byte Command1, Command2, Data1, Data2;
public static byte[] GetMessage(uint address, byte command1, byte command2, byte data1, byte data2)
{
if (address < 1 & address > 256)
throw new Exception("Pelco-D協(xié)議只支持設(shè)備");
Address = Byte.Parse((address).ToString());
Data1 = data1;
Data2 = data2;
Command1 = command1;
Command2 = command2;
CheckSum = (byte)( STX ^ Address ^ Command1 ^ Command2 ^ Data1 ^ Data2);
return new byte[] { STX, Address, Command1, Command2, Data1, Data2, CheckSum };
}
}