Controlling a PTZ camera from Linux

Today I received a mission to get a PTZ camera (COPTEK model MSPD650EX) working on Linux using protocolo PELCO-D.

I searched on Internet but didn’t find a sample code which work on Linux, but I found this one for a dirt and inspiring idea:
http://www.pudn.com/downloads139/sourcecode/unix_linux/detail595271.html

This example will not work correctly, because it doesn’t setup the pelco-d baudrate (2400 8n1), unless you setup baudrate on Linux terminal using stty command.

Then I just modified it to setup it correctly:

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <time.h>

#define BAUDRATE B2400
#define FALSE 0
#define TRUE 1

char devicename[80] = "/dev/ttyUSB0";
int status;

int getch(void) {
      int c=0;

      struct termios org_opts, new_opts;
      int res=0;
          //-----  store old settings -----------
      res=tcgetattr(fileno(stdin), &org_opts);
      assert(res==0);
          //---- set new terminal parms --------
      memcpy(&new_opts, &org_opts, sizeof(new_opts));
      new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
      tcsetattr(fileno(stdin), TCSANOW, &new_opts);
      c=getchar();
          //------  restore old settings ---------
      res=tcsetattr(fileno(stdin), TCSANOW, &org_opts);
      assert(res==0);
      return(c);
}

int main(int argc, char *argv[])
{
	int fd, res, i;
	int ch = 0, chant = 0;
	struct termios newtio;
	char buf[255];

	unsigned char cmd485[5][7]=
                            {{0xFF,0x01,0x00,0x10,0x2F,0x2F,0x6F},
                            {0xFF,0x01,0x00,0x08,0x2F,0x2F,0x67},
                            {0xFF,0x01,0x00,0x04,0x2F,0x2F,0x63},
                            {0xFF,0x01,0x00,0x02,0x2F,0x2F,0x61},
                            {0xFF,0x01,0x00,0x00,0x00,0x00,0x01}};
                            //up,down,left,right,stop


	//open the device in non-blocking way (read will return immediately)
	fd = open(devicename, O_RDWR | O_NOCTTY);
	if (fd  0){
                        if (ch == 65) //Up
			{
				//printf("Up\n");
                                write(fd, cmd485[0], 8);
			}
                        if (ch == 66) //Down
			{
				//printf("Down\n");
                                write(fd, cmd485[1], 8);
			}
                        if (ch == 67) //Right
			{
				//printf("Right\n");
                                write(fd, cmd485[3], 8);
			}
                        if (ch == 68) //Left
			{
				//printf("Left\n");
                                write(fd, cmd485[2], 8);
			}
			if (ch == 32) //stop
			{
			        //printf("Stop\n");
                                write(fd, cmd485[4], 8);
			}

			fsync(fd);
                }
                usleep(1000);
	}
	close(fd);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s