Motion - Support Question 2004x 12x 14x 221811

Pelco-D Protocol

Question

I have a Pelco-D compatible PTZ camera (Elitar ELPT806) and whant it to work in Linux/Motion. Does anybody know how i can send Hex code to the serial port, ttyS0. Pelco says send 0xFF, 0x01, 0x88, 0x90, 0x20, 0x00, 0x40 to the serialport and the camera goes on, focus far and tilts down. But I dont know how. I have tried with echo -ne \oct > /dev/ttyS0 and cat file.hex > /dev/ttyS0 without any luck. I have a converter from rs-232 to rs-485 on the serialport. Thanks Niklas

Environment

Motion version: 3.1.17
Libraries: curl, xmlrpc, ffmpeg, mysql, postgresql
Server OS: Debian

-- NiklasGlimner - 14 Dec 2004

Answer

The small C program below will send one byte given by the command line argument to the serial port.

Copy and paste it into a file sendserial.c

gcc -o sendserial sendserial.c

You need to make sure that the user you run as has access rights to the serial port.

/*   sendserial.c
 *
 *   Copyright 2004, Kenneth Lavrsen
 *   This program is published under the GNU Public license
 */

#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h> 

#define STEPPER_BAUDRATE B9600
#define DEVICENAME "/dev/ttyS0"

int main(int argc, char *argv[])
{
   struct termios adtio;
   int serdevice;
   int command[1];
   
   if ((serdevice=open(DEVICENAME, O_RDWR | O_NOCTTY)) < 0) {
      perror("Unable to open serial device");
      exit(1);
   }

   memset (&adtio, 0, sizeof(adtio));
   adtio.c_cflag= STEPPER_BAUDRATE | CS8 | CLOCAL | CREAD;
   adtio.c_iflag= IGNPAR;
   adtio.c_oflag= 0;
   adtio.c_lflag= 0;   /* non-canon, no echo */
   adtio.c_cc[VTIME]=0;   /* timer unused */
   adtio.c_cc[VMIN]=0;   /* blocking read until 1 char */
   tcflush (serdevice, TCIFLUSH);
   if (tcsetattr(serdevice, TCSANOW, &adtio) < 0) {
      perror("Unable to initialize serial device");
      exit(1);
   }
   
   command[0]= atoi(argv[1]);
   if (command[0]>255) command[0]=255;
   if (command[0]<0) command[0]=0;

   write(serdevice, command, 1);

   close(serdevice);

   return 0;
}

For more advanced serial communication you can study the code in open2300 weather station code made by me - http://open2300.sourceforge.net/

-- KennethLavrsen - 20 Dec 2004

Sorry to reopen such an old thread but I'm trying to send a pelco-D command to my PTZ camera with no success. Using the above C code, do I run ./sendserial "0xFF 0x02 0x88 0x90 0x20 0x00 0x40"?

-- RomanGaufman - 17 Feb 2008

I also tried for i in 0xFF 0x01 0x88 0x90 0x20 0x00 0x40; do ./sendserial $i; done with no success frown, sad smile - Any ideas?

-- RomanGaufman - 18 Feb 2008

Ah, I had a bad RS232 to RS485 adaptor, when I tried a different one it all worked - one more note is you have to send decimals instead of hex (using a hex to decimal convertor) - like so: for i in 255 1 0 4 32 0 37 ; do ./sendserial $i; done (this is to make the camera go left endlessly). If anyone else is stuck like I was, another gotcha is the last decimal is a sum of all the previous decimals excluding 255, so if the camera ID is 2 instead of 1, the last decimal will be 38 instead of 37. Hope someone finds this useful smile

-- RomanGaufman - 18 Feb 2008
Topic revision: r5 - 19 Feb 2008, KennethLavrsen
Copyright © 1999-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Please do not email Kenneth for support questions (read why). Use the Support Requests page or join the Mailing List.
This website only use harmless session cookies. See Cookie Policy for details. By using this website you accept the use of these cookies.