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:

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);
          //---- 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);
          //------  restore old settings ---------
      res=tcsetattr(fileno(stdin), TCSANOW, &org_opts);

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]=

	//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
                                write(fd, cmd485[0], 8);
                        if (ch == 66) //Down
                                write(fd, cmd485[1], 8);
                        if (ch == 67) //Right
                                write(fd, cmd485[3], 8);
                        if (ch == 68) //Left
                                write(fd, cmd485[2], 8);
			if (ch == 32) //stop
                                write(fd, cmd485[4], 8);


Leave a Reply

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

You are commenting using your 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