C# 云台控制 Pelco_D-P协议

这是一个C#实现Pelco_D和Pelco_P云台控制协议的代码示例,包括了多个命令,如预设位置、翻转、零位设置、辅助设备操作等。通过枚举类型简化了命令和参数的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pelco_D:

 

using System;
using System.Collections;

namespace Pelco
{
 /// <summary>
 /// dot.NET Implementation of Pelco D Protocol
 /// </summary>
    public class Pelco_D
 {
        private static readonly byte STX = 0xFF;//同步字节

  #region Pan and Tilt Commands
  #region Command1
  private const byte FocusNear = 0x01;
  private const byte IrisOpen = 0x02;
  private const byte IrisClose = 0x04;
  private const byte CameraOnOff = 0x08;
  private const byte AutoManualScan = 0x10;
  private const byte Sense = 0x80;
  #endregion

  #region Command2
  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;
  private const byte ZoomWide = 0x40;
  private const byte FocusFar = 0x80;
  #endregion

  #region Data1
  private const byte PanSpeedMin = 0x00;
  private const byte PanSpeedMax = 0xFF;
  #endregion

  #region Data2
  private const byte TiltSpeedMin = 0x00;
  private const byte TiltSpeedMax = 0x3F;
  #endregion
  #endregion

  #region Enums
  public enum PresetAction {Set,Clear,Goto}
  public enum AuxAction {Set=0x09,Clear=0x0B}
  public enum Action {Start,Stop}
  public enum LensSpeed {Low=0x00,Medium=0x01,High=0x02,Turbo=0x03}
  public enum PatternAction {Start,Stop,Run}
  public enum SwitchAction {Auto=0x00,On=0x01,Off=0x02}
  public enum Switch {On=0x01,Off=0x02}
  public enum Focus {Near = FocusNear,Far = FocusFar}
  public enum Zoom {Wide = ZoomWide,Tele = ZoomTele}
  public enum Tilt {Up = TiltUp,Down = TiltDown}
  public enum Pan {Left = PanLeft,Right = PanRight}
  public enum Scan {Auto, Manual}
  public enum Iris {Open = IrisOpen,Close = IrisClose}
  #endregion

  #region Extended Command Set
  public byte[] Preset(uint deviceAddress, byte preset, PresetAction action)
  {
   byte m_action;
   switch (action)
   {
    case PresetAction.Set:
     m_action = 0x03;
     break;
    case PresetAction.Clear:
     m_action = 0x05;
     break;
    case PresetAction.Goto:
     m_action = 0x07;
     break;
    default:
     m_action = 0x03;
     break;
   }
   return Message.GetMessage(deviceAddress,0x00,m_action,0x00,preset);
  }

  public byte[] Flip(uint deviceAddress)
  {
   return Message.GetMessage(deviceAddress,0x00,0x07,0x00,0x21);
  }

  public byte[] ZeroPanPosition(uint deviceAddress)
  {
   return Message.GetMessage(deviceAddress,0x00,0x07,0x00,0x22);
  }

  public byte[] SetAuxiliary(uint deviceAddress,byte auxiliaryID, AuxAction action)
  {
   if(auxiliaryID<0x00)
    auxiliaryID = 0x00;
   else if(auxiliaryID>0x08)
    auxiliaryID = 0x08;
   return Message.GetMessage(deviceAddress,0x00,(byte)action,0x00,auxiliaryID);
  }

  public byte[] RemoteReset(uint deviceAddress)
  {
   return Message.GetMessage(deviceAddress,0x00,0x0F,0x00,0x00);
  }
  public byte[] Zone(uint deviceAddress,byte zone, Action action)
  {
   if(zone<0x01 & zone>0x08)
    throw new Exception("Zone value should be between 0x01 and 0x08 include");
   byte m_action;
   if(action == Action.Start)
    m_action = 0x11;
   else
    m_action = 0x13;

   return Message.GetMessage(deviceAddress,0x00,m_action,0x00,zone);
  }

  public byte[] WriteToScreen(uint deviceAddress,string text)
  {
   if(text.Length > 40)
    text = text.Remove(40,text.Length-40);
   System.Text.Encoding encoding = System.Text.Encoding.ASCII;
   byte[] m_bytes = new byte[encoding.GetByteCount(text)*7];
   int i = 0;
   byte m_scrPosition;
   byte m_ASCIIchr;
   
   foreach(char ch in text)
   {
    m_scrPosition = Convert.ToByte(i/7);
    m_ASCIIchr = Convert.ToByte(ch);
    Array.Copy(Message.GetMessage(deviceAddress,0x00,0x15,m_scrPosition,m_ASCIIchr),

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值