1.该协议中所有数值都为十六进制数
2.同步字节始终为FFH
3.地址码为摄像机的逻辑地址号,地址范围:00H–FFH
4.指令码表示不同的动作
5.数据码1、2分别表示水平、垂直方向速度(00-3FH),FFH表示“turbo”速度
6.
校验码 = MOD[(字节2 + 字节3 + 字节4 + 字节5 + 字节6)/100H]
以地址码0x01为例:
{0xff,0x01,0x00,0x08,0x00,0xff,0x08,}//上
{0xff,0x01,0x00,0x10,0x00,0xff,0x10,}//下
{0xff,0x01,0x00,0x04,0xff,0x00,0x04,}//左
{0xff,0x01,0x00,0x02,0xff,0x00,0x02,}//右
{0xff,0x01,0x00,0x20,0x00,0x00,0x21,}//变倍短
{0xff,0x01,0x00,0x40,0x00,0x00,0x41,}//变倍长
{0xff,0x01,0x00,0x80,0x00,0x00,0x81,}//聚焦近
{0xff,0x01,0x01,0x00,0x00,0x00,0x02,}//聚焦远
{0xff,0x01,0x02,0x00,0x00,0x00,0x03,}//光圈小
{0xff,0x01,0x04,0x00,0x00,0x00,0x05,}//光圈大
{0xff,0x01,0x00,0x0b,0x00,0x01,0x0d,}//灯光关
{0xff,0x01,0x00,0x09,0x00,0x01,0x0b,}//灯光开
{0xff,0x01,0x00,0x07,0x00,0x01,0x09,}//转至预置点001
{0xff,0x01,0x00,0x03,0x00,0x01,0x05,}//设置预置点001
{0xff,0x01,0x00,0x05,0x00,0x01,0x07,}//删除预置点001
以上对应的停命令均是:
{0xff,0x01,0x00,0x00,0x00,0x00,0x01,}//停命令
按照以上协议格式,编写代码seri.c如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define TTYS "/dev/ttyS0" //串口设备
#define ADDR 0x01 //云台设备逻辑地址
#define BAUD_RATE 2400 //波特率
#define DATA_BITS 8 //数据位
#define NEVENT 'N' //校验
#define NSTOP 1 //停止位
//#define DBG_YT //调试信息打印
/*串口设置函数*/
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch( nEvent )
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
switch( nSpeed )
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
#ifdef DBG_YT
printf("set done!\n");
#endif
return 0;
}
/*打开串口函数*/
int open_port()
{
int fd;
fd = open( TTYS , O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
#ifdef DBG_YT
else
printf("open ttyS0 .....\n");
#endif
if(fcntl(fd, F_SETFL, 0)<0)
printf("fcntl failed!\n");
#ifdef DBG_YT
else
printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
#endif
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
#ifdef DBG_YT
else
printf("isatty success!\n");
printf("fd-open=%d\n",fd);
#endif
return fd;
}
int main(void)
{
int fd;
int id,speed,i;
char flag;
char a[7]={0xff,ADDR,0,0,0,0,0};