Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

Pelco P and D protocol implementation in C#

Rate me:
Please Sign up or sign in to vote.
4.56/5 (25 votes)
18 Aug 200410 min read 350.8K   6.1K   53   64
This is a full C# classes to control a PELCO PTZ cameras, matrix switching systems, reciever devices and more via RS422/485 'P' and 'D' protocol. It supports all of the commands including UP, DOWN, IN, OUT, LEFT,

Introduction

This is GPL software. Do with it as you want, but feed us back any improvements.

This is a full C# classes to control a PELCO PTZ cameras, matrix switching systems,
reciever devices and more via RS422/485 'P' and 'D' protocol.
It supports all of the commands including UP, DOWN, IN, OUT, LEFT, RIGHT, NEAR, FAR, as well as other extended commands.

To use this, you need to put a RS232->RS422 adapter on the output of your desired serial port.

The Pelco doesn't return ANY usefull info back, so you only really need 2-wire support (one way) communications out. However, I advice to read it in order to know if the command was recieved by the device.

This section describes the protocol used when sending commands to an Intercept Dome in the “P” version protocol and Coaxitron series equipment and with Pelco’s “D” version receivers. Those protocols use no parity, one start bit, eight data bits, and one stop bit. The recommended baud rate is 4800 (4800, 8, N, 1, 1).

Theory

In those protocols the messages structure are different. However both of protocols using RS-485 port to send and recieve messages.

All values below are shown in hexadecimal (base 16).

Pelco P message structure

Byte

Value

Function

1

$A0

STX (start transmission)

2

$00 to $1F

Address

3

Data byte 1

(see below)

4

Data byte 2

(see below)

5

Data byte 3

(see below)

6

Data byte 4

(see below)

7

$AF

ETX (end transmission)

8

$00-$FF

Check Sum

Byte 1 is always $A0
Byte 2 is the receiver address, set by DIP switch in the receiver
Byte 3-6, see below
Byte 7 is always $AF
Byte 8 is an XOR sum of Bytes 1-7

The protocol is “zero indexed” so that the hexadecimal address sent in the protocol for the first receiver is $00 which corresponds to address 1.

Pelco D message structure

The “D” protocol has some added overhead to improve the reliability of transmissions. The format for a message is:

Word 1

Word 2

Word 3

Word 4

Word 5

Word 6

Word 7

Synch Byte

Address

Command 1

Command 2

Data 1

Data 2

Check Sum

The synchronization byte is always $FF.

The address is the logical address of the receiver/driver being controlled.

Following structs that bulds those message and prepare them to be sent to the device

C#
public struct Message // Pelco P Message
  {
   public static byte Address;
   public static byte CheckSum;
   public static byte Data1,Data2,Data3,Data4;
C#
   public static byte[] GetMessage(uint address, byte data1, byte data2, byte data3, byte data4)
   {
    if (address<0 & address>32)
     throw new Exception("Protocol Pelco P support 32 devices only");

 

    Address = Byte.Parse((address-1).ToString());
    Data1 = data1;
    Data2 = data2;
    Data3 = data3;
    Data4 = data4;

 

    CheckSum = (byte)(STX ^ Address ^ Data1 ^ Data2 ^ Data3 ^ Data4 ^ ETX);

    return new byte[]{STX,Address,Data1,Data2,Data3,Data4,ETX,CheckSum};
   }
   
  }

 

public struct Message //Pelco D 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("Protocol Pelco D support 256 devices only");
    
    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};
   }
   
  }

Base Message Format

In both Pelco protocols the bits within the “Data bytes” are broken up into two main groups. For Pan and Tilt commands in Pelco P protocol, the functions are determined as follows:

Bit number

7

6

5

4

3

2

1

0

Data 1

0

Camera On

Autoscan On

Camera on / off

Iris Close

Iris Open

Focus Near

Focus Far

Data 2

0

Zoom Wide

Zoom Tele

Tilt Down

Tilt Up

Pan Left

Pan Right

0 (for pan / tilt)

Data 3

Pan Speed $00 to $3F and $40 for Turbo

Data 4

Tilt Speed $00 to $3F

In order to produce this in C# we should use binary calculation as following:

public enum PresetAction {Set,Clear,Goto}

public enum PatternAction {Start,Stop,Run}

public enum Action {Start,Stop}

public enum LensSpeed {Low=0x00,Medium=0x01,High=0x02,Turbo=0x03}

public enum Pan {Left = PanLeft,Right = PanRight}

public enum Tilt {Up = TiltUp,Down = TiltDown}

public enum Iris {Open = IrisOpen,Close = IrisClose}

public enum Zoom {Wide = ZoomWide,Tele = ZoomTele}

public enum Switch {On,Off}

public enum Focus {Near = FocusNear,Far = FocusFar}

and impelement it like this:

public byte[] CameraSwitch(uint deviceAddress,Switch action)

{

byte m_action = CameraOnOff;

if(action == Switch.On)

m_action += CameraOnOff; //Maybe wrong !!!

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



}

But in Pelco D protocol they are quite different:

Bit 7

Bit 6

Bit 5

Bit 4

Bit 3

Bit 2

Bit 1

Bit 0

Command 1

Sense

Reserved

Reserved

Auto / Manual Scan

Camera On / Off

Iris Close

Iris Open

Focus Near

Command 2

Focus Far

Zoom Wide

Zoom Tele

Down

Up

Left

Right

Always 0

The sense bit (command 1 bit 7) indicates the meaning of bits 4 and 3. If the sense bit is on and bits 4 and 3 are on the command will enable autoscan and turn the camera on. If the sense bit is off and bits 4 and 3 are on the command will enable manual scan and turn the camera off. Of course, if either bit 4 or bit 3 are off then no action will be taken for those features.

The reserved bits (6 and 5) should be set to 0.

Word 5 contains the pan speed. Pan speed is in the range $00 (stop) to $3F (high speed) and $FF for “turbo” speed. Turbo speed is the maximum speed the device can obtain and is considered separately because it is not generally a smooth step from high speed to turbo. That is, going from one speed to the next usually looks smooth and will provide for smooth motion with the exception of going into and out of turbo speed.

Word 6 contains the tilt speed. Tilt speed is in the range $00 (stop) to $3F (maximum speed).

Word 7 is the check sum. The check sum is the sum of bytes (excluding the synchronization byte) modulo 256.

Extended Command Set

In addition to the “PTZ” commands shown above, there are control commands that allow you access to the more advanced features of some equipment.

For Pelco P protocol the extended command set will have bit 0 of data byte 2 set and will follow the format in the following table:

Command

Data byte 1

Data byte 2

Data byte 3

Data byte 4

Set Preset XX

00

03

00

01 to FF

Clear Preset XX

00

05

00

01 to FF

Go To Preset XX

00

07

00

01 to FF

Flip (rotate 180º)

00

07

00

21

Zero Pan Position

00

07

00

22

Auto scan

00

09

00

00

Stop auto scan

00

0B

00

00

Remote Reset

00

0F

00

00

Zone Start

00

11

00

01 to 08

Zone End

00

13

00

01 to 08

Write char to screen

00

15

0 to 28

0 to 7F

Clear Screen

00

17

00

00

Alarm Ack

00

19

00

01 to 08

Zone Scan On

00

1B

00

00

Zone Scan Off

00

1D

00

00

Pattern Start

00

1F

00

00

Pattern Stop

00

21

00

00

Run Pattern

00

23

00

00

Zoom Lens Speed

00

25

00

00 to 03

Focus Lens Speed

00

27

00

00 to 03

In Pelco D implementation they are as following:

 

Command <o:p>

Word 3 <o:p>

Word 4 <o:p>

Word 5 <o:p>

Word 6 <o:p>

Set Preset <o:p>

00 <o:p>

03 <o:p>

00 <o:p>

01 to 20 <o:p>

Clear Preset <o:p>

00 <o:p>

05 <o:p>

00 <o:p>

01 to 20 <o:p>

Go To Preset <o:p>

00 <o:p>

07 <o:p>

00 <o:p>

01 to 20 <o:p>

Flip (180° about) <o:p>

00 <o:p>

07 <o:p>

00 <o:p>

21 <o:p>

Go To Zero Pan <o:p>

00 <o:p>

07 <o:p>

00 <o:p>

22 <o:p>

Set Auxiliary <o:p>

00 <o:p>

09 <o:p>

00 <o:p>

01 to 08 <o:p>

Clear Auxiliary <o:p>

00 <o:p>

0B <o:p>

00 <o:p>

01 to 08 <o:p>

Remote Reset <o:p>

00 <o:p>

0F <o:p>

00 <o:p>

00 <o:p>

Set Zone Start <o:p>

00 <o:p>

11 <o:p>

00 <o:p>

01 to 08 <o:p>

Set Zone End <o:p>

00 <o:p>

13 <o:p>

00 <o:p>

01 to 08 <o:p>

Write Char. To Screen <o:p>

00 <o:p>

15 <o:p>

X Position 00 to 28 <o:p>

ASCII Value <o:p>

Clear Screen <o:p>

00 <o:p>

17 <o:p>

00 <o:p>

00 <o:p>

Alarm Acknowledge <o:p>

00 <o:p>

19 <o:p>

00 <o:p>

Alarm No. <o:p>

Zone Scan On <o:p>

00 <o:p>

1B <o:p>

00 <o:p>

00 <o:p>

Zone Scan Off <o:p>

00 <o:p>

1D <o:p>

00 <o:p>

00 <o:p>

Set Pattern Start <o:p>

00 <o:p>

1F <o:p>

00 <o:p>

00 <o:p>

Set Pattern Stop <o:p>

00 <o:p>

21 <o:p>

00 <o:p>

00 <o:p>

Run Pattern <o:p>

00 <o:p>

23 <o:p>

00 <o:p>

00 <o:p>

Set Zoom Speed <o:p>

00 <o:p>

25 <o:p>

00 <o:p>

00 to 03 <o:p>

Set Focus Speed <o:p>

00 <o:p>

27 <o:p>

00 <o:p>

00 to 03 <o:p>

Please note that in Pelco P implementaiton the checksum is calculation by XOR binary sum of bytes 1-7, but in D one is by madulo 256 sum of bytes 1-6

0A        00001010 <o:p>

88        10001000 <o:p>

Subtotal  10010010  92 <o:p>

90        10010000 <o:p>

Subtotal  00100010  22   (modulo 256 allows the high bit to roll off) <o:p>

00        00000000 <o:p>

Subtotal  00100010  22 <o:p>

40        01000000 <o:p>

01100010  62  

Final check sum value


The response of devices not really important, however just to know that in Pelco P the response will be ACK command, when in Pelco D the response to one of these commands is four bytes long. The first byte is the synchronization character (FF), the second byte is the receiver address, the third byte contains the alarm information and the fourth byte is the check sum.

Good Luck

 

Feedback

 I would greatly appreciate any comments on how useful you think this software is to you, suggestions for improvements to the overall architecture and/or the efficiency of the base classes, any bug reports, etc.

To reach me, please send all comments to: tamir@khason.biz. As well you can visit my website at http://www.dotnet.us/ to learn more about me.

Open Source License

THIS SOFTWARE PROGRAM (“PROGRAM”) IS PROVIDED UNDER THE TERMS OF THIS  PUBLIC LICENSE AGREEMENT (“AGREEMENT”). ANY USE, REPRODUCTION, OR DISTRIBUTION OF THE PROGRAM CONSTITUTES YOUR ACCEPTANCE OF THE TERMS OF THIS AGREEMENT.

Permission to use, copy, modify, and distribute the Program and its documentation, if any, for any purpose and without fee is hereby granted by Tamir Khason to you, provided that:  (i) you not charge any fee for the Program, and the Program not be incorporated by you in any software or code for which compensation is expected or received; (ii) the copyright notice listed below appears in all copies; (iii) both the copyright notice and this Agreement appear in all supporting documentation; and (iv) the name of Tamir Khason not be used in advertising or publicity pertaining to distribution of the Program without specific, written prior permission.

THE PROGRAM IS PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.

You are solely responsible for determining the appropriateness of using and distributing the Program and you assume all risks associated with the exercise of rights under this Agreement, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations.

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, TAMIR KHASON SHALL NOT HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.  It is your responsibility to comply with any export laws of your jurisdiction, including without limitation, Israel. This Agreement is governed by the laws of the State of Israel and the intellectual property laws of the Israel. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose.

If you have any comments, suggestions, improvements, modifications, alterations, derivative works, or other changes (collectively “Contributions”) to the Program that you would like to provide to Tamir Khason, please send such Contributions to: tamir@khason.biz.  By sending Contributions to Tamir Khason, you acknowledge and agree that you grant to Tamir Khason a non-exclusive, worldwide, fully paid-up, royalty-free, license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contributions, in source code and/or object code form, and such license is provided by you without any right of accounting.

The copyright notice for the Program is as follows:  Copyright © 2003 Tamir Khason.  All Rights Reserved.  Sponsored by the Tadiran Security Systems ltd.

The contents of those protocols (Not source code or implementation) are considered to be the property of Pelco. Users of this protocol agree to use the protocol only in the interests of Pelco. Any use of this protocol to Pelco’s detriment is prohibited.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect Better Place
Israel Israel
Hello! My name is Tamir Khason, and I am software architect, project manager, system analyst and [of course] programmer. In addition to writing big amount of documentation, I also write code, a lot of code. I used to work as a freelance architect, project manager, trainer, and consultant here, in Israel, but recently join the company with extremely persuasive idea - to make a world better place. I have very pretty wife and 3 charming kids, but unfortunately almost no time for them.

To be updated within articles, I publishing, visit my blog or subscribe RSS feed. Also you can follow me on Twitter to be up to date about my everyday life.

Comments and Discussions

 
SuggestionSteps to compile and test this class Pin
fouzi9822-Sep-17 4:13
fouzi9822-Sep-17 4:13 
QuestionHow can i test this code with specific COM port and Baud rate Pin
nhat_vbnet4-Mar-14 17:35
nhat_vbnet4-Mar-14 17:35 
QuestionStill facing error with the code Pin
hassanayoub8521-Jul-13 7:34
hassanayoub8521-Jul-13 7:34 
AnswerRe: Still facing error with the code Pin
ncvitanovic24-Sep-13 3:11
ncvitanovic24-Sep-13 3:11 
QuestionSome question of data byte 1 Pin
forgetyouyou13-May-12 22:13
forgetyouyou13-May-12 22:13 
QuestionPelco D checksum error Pin
leeky87-Mar-12 16:57
leeky87-Mar-12 16:57 
QuestionHow to use this in VB2005 Pin
total_recall2-Sep-10 4:52
total_recall2-Sep-10 4:52 
GeneralPELCO CAMERA Pin
eder0075-Feb-10 6:57
eder0075-Feb-10 6:57 
GeneralRe: PELCO CAMERA Pin
ncvitanovic24-Sep-13 3:15
ncvitanovic24-Sep-13 3:15 
GeneralMy vote of 1 Pin
Member 433958028-Oct-09 21:58
Member 433958028-Oct-09 21:58 
QuestionWhy do you pass an address as an uint type instead of byte type ? Pin
Sasha_rawenstvo7-May-09 2:16
Sasha_rawenstvo7-May-09 2:16 
Generallilin keyboard Pin
pooya15-Oct-08 3:05
pooya15-Oct-08 3:05 
I want to control speed dome camera ( d protocol) with lilin keyboard .
can you help me ?
GeneralRe: lilin keyboard Pin
Eric Hamilton9-Oct-08 5:38
Eric Hamilton9-Oct-08 5:38 
QuestionNamespace Error [modified] Pin
nabberuk26-Jul-08 10:20
nabberuk26-Jul-08 10:20 
QuestiondeviceAddress Pin
Ehsan Baghaki23-May-08 6:18
Ehsan Baghaki23-May-08 6:18 
AnswerRe: deviceAddress Pin
Eric Hamilton10-Sep-08 4:14
Eric Hamilton10-Sep-08 4:14 
GeneralRe: deviceAddress Pin
SCCSCCSCC8-Jan-09 22:07
SCCSCCSCC8-Jan-09 22:07 
GeneralRe: deviceAddress Pin
Eric Hamilton9-Jan-09 4:05
Eric Hamilton9-Jan-09 4:05 
GeneralHelp me out Pin
Stephen Wandera31-Mar-08 2:16
Stephen Wandera31-Mar-08 2:16 
GeneralRe: Help me out Pin
Eric Hamilton20-May-08 7:02
Eric Hamilton20-May-08 7:02 
QuestionHow to get it working? Pin
David Pell3-Mar-08 4:17
David Pell3-Mar-08 4:17 
AnswerRe: How to get it working? Pin
Eric Hamilton20-May-08 7:01
Eric Hamilton20-May-08 7:01 
GeneralHas someone use it? [modified] Pin
Motyka26-Sep-07 23:15
Motyka26-Sep-07 23:15 
GeneralRe: Has someone use it? Pin
Eric Hamilton29-Oct-07 3:55
Eric Hamilton29-Oct-07 3:55 
GeneralRe: Has someone use it? Pin
AndIosJavaC++19-Feb-09 21:28
AndIosJavaC++19-Feb-09 21:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.