Skip to content

Instantly share code, notes, and snippets.

@acassis
Created January 24, 2013 17:08
Show Gist options
  • Save acassis/4625198 to your computer and use it in GitHub Desktop.
Save acassis/4625198 to your computer and use it in GitHub Desktop.
Simple PTZ test sample code
#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)
{
perror(devicename);
exit(1);
}
// set new port settings for canonical input processing
//newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
// loop while ESC not pressed twice
while (!(chant == 27 && ch == 27))
{
// save previous pressed key
chant = ch;
//
// read characters typed by user in non-blocking way
ch = getch();
if(ch > 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment