1. 程式人生 > >.net實現PTZ雲臺控制

.net實現PTZ雲臺控制

VB.NET程式碼:

Public Class Form1
    Dim x As PtzProtocol.PtzProtocol

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        x.Close()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.comboBox1.Items.Add("Hikvision")
        Me.comboBox1.Items.Add("PelcoD")
        Me.comboBox1.Items.Add("PelcoP")
        Me.comboBox1.Items.Add("Vicon")
        Me.comboBox1.Items.Add("YAAN")

        Me.comboBox1.SelectedIndex = 0

        Dim sp As New IO.Ports.SerialPort("COM2")
        With sp
            .BaudRate = 9600
            .DataBits = 8
            .StopBits = IO.Ports.StopBits.One
            .Parity = IO.Ports.Parity.None
        End With
        x = New PtzProtocol.Hikvision(sp, 2)
        x.Open()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        x.Up()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        x.Down()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        x.Left()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        x.Right()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        x.Focus(PtzProtocol.PtzProtocol.DoAction.Additive)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        x.Focus(PtzProtocol.PtzProtocol.DoAction.Subtraction)
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        x.Zoom(PtzProtocol.PtzProtocol.DoAction.Additive)
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        x.Zoom(PtzProtocol.PtzProtocol.DoAction.Subtraction)
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        x.IRIS(PtzProtocol.PtzProtocol.DoAction.Additive)
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        x.IRIS(PtzProtocol.PtzProtocol.DoAction.Subtraction)
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        x.SetPresetPoint(2)
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        x.GotoPresetPoint(2)
    End Sub

    Private Sub comboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBox1.SelectedIndexChanged
        Try
            Dim sp As System.IO.Ports.SerialPort = New System.IO.Ports.SerialPort("COM2", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One)
            x.Close()
            Select (Me.comboBox1.SelectedIndex)

                Case 0

                    x = New PtzProtocol.Hikvision(sp, 1, 40, 40)
                    x.Open()
                Case 1

                    x = New PtzProtocol.PelcoD(sp, 1, 40, 40)
                    x.Open()
                Case 2
                    x = New PtzProtocol.PelcoP(sp, 1, 40, 40)
                    x.Open()
                Case 3
                    x = New PtzProtocol.Vicon(sp, 1, 40, 40)
                    x.Open()
                Case 4
                    x = New PtzProtocol.YAAN(sp, 1, 40, 40)
                    x.Open()
            End Select

        Catch ex As Exception

        End Try
    End Sub
End Class


C#程式碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private PtzProtocol.PtzProtocol  x;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.comboBox1.Items.Add("Hikvision");
            this.comboBox1.Items.Add("PelcoD");
            this.comboBox1.Items.Add("PelcoP");
            this.comboBox1.Items.Add("Vicon");
            this.comboBox1.Items.Add("YAAN");

            this.comboBox1.SelectedIndex = 0;

            System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort("COM2", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);

            x = new PtzProtocol.Hikvision(sp, 1, 40, 40);
            x.Open();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            x.Close();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            x.Up(40);
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            x.Down(40);
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            x.Left(40);
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            x.Right(40);
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            x.Focus(PtzProtocol.PtzProtocol.DoAction.Additive );
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            x.Focus(PtzProtocol.PtzProtocol.DoAction.Subtraction);
        }

        private void Button7_Click(object sender, EventArgs e)
        {
            x.Zoom(PtzProtocol.PtzProtocol.DoAction.Additive);
        }

        private void Button8_Click(object sender, EventArgs e)
        {
            x.Zoom(PtzProtocol.PtzProtocol.DoAction.Subtraction);
        }

        private void Button9_Click(object sender, EventArgs e)
        {
            x.IRIS(PtzProtocol.PtzProtocol.DoAction.Additive);
        }

        private void Button10_Click(object sender, EventArgs e)
        {
            x.IRIS(PtzProtocol.PtzProtocol.DoAction.Subtraction);
        }

        private void Button11_Click(object sender, EventArgs e)
        {
            x.SetPresetPoint(1);
        }

        private void Button12_Click(object sender, EventArgs e)
        {
            x.GotoPresetPoint(1);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort("COM2", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                x.Close();
                switch (this.comboBox1.SelectedIndex)
                {
                    case 0:
                        {
                            x = new PtzProtocol.Hikvision(sp, 1, 40, 40);
                            x.Open();
                            break;
                        }
                    case 1:
                        {
                            x = new PtzProtocol.PelcoD(sp, 1, 40, 40);
                            x.Open();
                            break;
                        }
                    case 2:
                        {
                            x = new PtzProtocol.PelcoP(sp, 1, 40, 40);
                            x.Open();
                            break;
                        }
                    case 3:
                        {
                            x = new PtzProtocol.Vicon (sp, 1, 40, 40);
                            x.Open();
                            break;
                        }
                    case 4:
                        {
                            x = new PtzProtocol.YAAN (sp, 1, 40, 40);
                            x.Open();
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}


 事實證明,任何時候對某一類具有相同型別業務的合理抽象將會使後期程式碼的工作量大大減少,同樣程式碼的可重用性將會增加。