class PelcoD { private static readonly byte STX = 0xFF; //同步字节 private const byte TiltUp = 0x08; //上 private const byte TiltDown = 0x10; //下 #region 镜头上下移动的速度 private const byte TiltSpeedMin = 0x00; //停止 private const byte TiltSpeedMax = 0x3F; //最高速 #endregion public enum Tilt { Up = TiltUp, Down = TiltDown } //上下控制 //上下控制 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 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协议只支持设备"); 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 }; } } public partial class frmMain : Form { PelcoD pelcod = new PelcoD(); SerialPort serialPort = new SerialPort("COM1", 2400, Parity.None, 8); byte addressin = Byte.Parse(Convert.ToString(0x01)); byte speedin = Byte.Parse(Convert.ToString(0xff)); byte[] messagesend; //方向控制——下 private void btnDown_MouseDown(object sender, MouseEventArgs e) { messagesend = pelcod.CameraTilt(addressin, PelcoD.Tilt.Down, speedin); serialPort.Open(); serialPort.Write(messagesend, 0, 7); serialPort.Close(); } } |