📄 ptz_rs232ctl.c
字号:
/* set tabstop=4 *//******************************************************************************** * * * Copyright(C) 2004 Penta-Micro * * * * ALL RIGHT RESERVED * * * * This software is the property of Penta-Micro and is furnished under * * license by Penta-Micro. This software may be used only in accordance * * with the terms of said license. This copyright notice may not be * * removed, modified or obliterated without the prior written permission * * of Penta-Micro. * * * * This software may not be copyed, transmitted, provided to or otherwise * * made available to any other person, company, corporation or other entity * * except as specified in the terms of said license. * * * * No right, title, ownership or other interest in the software is hereby * * granted or transferred. * * * * The information contained herein is subject to change without notice and * * should not be construed as a commitment by Penta-Micro. * * * ******************************************************************************** MODULE NAME: ptz_rs232ctrl.c REVISION HISTORY: Date Ver Name Description ---------- --- --------------------- ----------------------------------------- 07/09/2003 1.0 Mhpark Created 02/01/2005 1.1 JiGwanKang(xchannel) Modify(arrangement) ............................................................................... DESCRIPTION: This Module contains functions for ptz contro(rs232/422/485). ...............................................................................*/ /** ************************************************************************* ** ** includes ** ************************************************************************* **/#include <stdio.h>#include <signal.h>#include <unistd.h>#include <fcntl.h>#include <string.h>#include <sys/ioctl.h>#include "pthread_ptzctrl.h"#include "ptz_rs232ctrl.h"/** ************************************************************************* ** ** defines ** ************************************************************************* **///#define m_DEBUG(format, args...) printf(format, ## args);#define m_DEBUG(format, args...) #define m_MSG(format, args...) printf(format, ## args) #define m_ERROR(format, args...) printf(format, ## args);fflush(stdout);/** ************************************************************************* ** ** typedefs ** ************************************************************************* **//** ************************************************************************* ** ** globals ** ************************************************************************* **//** ************************************************************************* ** ** locals ** ************************************************************************* **/static struct termios rs232_attr;tRS232 fd_ptz_rs232;/** ************************************************************************* ** ** forward declarations ** ************************************************************************* **//* OPEN SERIAL DEVICE */S32 rs232_open (char const *device){ tRS232 rs232; rs232 = open (device, O_RDWR | O_NOCTTY); if (rs232 == -1) { fprintf (stderr, "error opening %s\n", device); Abort (rs232); return FAILURE; } return rs232;}/* INITIALIZE SERIAL DEVICE (SET COMMUNICATION PARAMETERS) */RETURN rs232_init (tRS232 rs232, tCommParms *commParm){ m_DEBUG("\n[rs232_init]%d-%d-%c-%d", commParm->baudrate, commParm->databits, commParm->parity, commParm->stopbits); rs232_attr.c_oflag = 0; rs232_attr.c_lflag = 0; rs232_attr.c_iflag = ICRNL | IGNPAR; rs232_attr.c_cflag = CREAD | CLOCAL; if (setBaudRate (&rs232_attr, commParm->baudrate) == FAILURE) Abort (rs232); if (setDataBits (&rs232_attr, commParm->databits) == FAILURE) Abort (rs232); if (setParity (&rs232_attr, commParm->parity) == FAILURE) Abort (rs232); if (setStopBits (&rs232_attr, commParm->stopbits) == FAILURE) Abort (rs232); if ( tcsetattr (rs232, TCSANOW, &rs232_attr) == -1) { fprintf (stderr, "error setting tcsetattr on %s\n", commParm->device); Abort (rs232); } return SUCCESS;}/* CONTROL MODEM LINES */RETURN rs232_mdlns (tRS232 rs232, tMdLines *mdLine){ int mdlns; if ( ioctl (rs232,TIOCMGET,&mdlns) == -1) { fprintf (stderr,"error ioctl TIOCMGET\n"); Abort (rs232); } modemLine (&mdlns, mdLine->dtr, (int)TIOCM_DTR); modemLine (&mdlns, mdLine->rts, (int)TIOCM_RTS); if ( ioctl (rs232,TIOCMSET,&mdlns) == -1) { fprintf(stderr,"error ioctl(TIOCMSET)\n"); Abort (rs232); } return SUCCESS;}/* SEND STRING TO SERIAL DEVICE */RETURN rs232_send (tRS232 rs232, char *send_str, UNS16 str_size){ int i; UNS8 ch; m_DEBUG("\n[Send Data]="); for(i=0;i < str_size;i++) { ch = (UNS8) *send_str++; m_DEBUG("0x%02x:", ch); if (write (rs232, &ch, 1) != 1) { fprintf (stderr, "error: write\n"); Abort (rs232); return FAILURE; } } return SUCCESS;}/* WAIT FOR # of SECONDS */void rs232_wait (float waittime, int mSec){ if (mSec == TRUE) { usleep (waittime * 1E3); m_DEBUG("\nwait %f mSec", waittime); } else { usleep (waittime * 1E6); m_DEBUG("\nwait %f seconds", waittime); }}/* READ # of BYTES FROM SERIAL DEVICE OR UNTIL STOP CHARACTER IS FOUND */UNS8 * rs232_read (tRS232 rs232, char *readbuf, int readlength){ m_DEBUG("\nread %d characters", readlength); if (read (rs232, readbuf, readlength) == -1) Abort (rs232); readbuf[readlength]='\0'; m_DEBUG("\n"); return readbuf;}/* CLOSE SERIAL DEVICE */RETURN rs232_close (tRS232 rs232){ if (close (rs232) == -1) { fprintf (stderr, "error closing serial I/O device\n"); Abort (-1); } return SUCCESS;}/* SET BAUD RATE */RETURN setBaudRate (struct termios *rs232_attr, int baudrate){ switch (baudrate) { case 50: rs232_attr->c_cflag |= B50; break; case 75: rs232_attr->c_cflag |= B75; break; case 110: rs232_attr->c_cflag |= B110; break; case 134: rs232_attr->c_cflag |= B134; break; case 150: rs232_attr->c_cflag |= B150; break; case 200: rs232_attr->c_cflag |= B200; break; case 300: rs232_attr->c_cflag |= B300; break; case 600: rs232_attr->c_cflag |= B600; break; case 1200: rs232_attr->c_cflag |= B1200; break; case 2400: rs232_attr->c_cflag |= B2400; break; case 4800: rs232_attr->c_cflag |= B4800; break; case 9600: rs232_attr->c_cflag |= B9600; break; case 19200: rs232_attr->c_cflag |= B19200; break; case 38400: rs232_attr->c_cflag |= B38400; break; case 57600: rs232_attr->c_cflag |= B57600; break; case 115200: rs232_attr->c_cflag |= B115200; break; case 230400: rs232_attr->c_cflag |= B230400; break; default: fprintf (stderr, "error: unsupported baud rate\n"); return FAILURE; } return SUCCESS;}/* SET CHARACTER SIZE */RETURN setDataBits (struct termios *rs232_attr, int databits){ switch (databits) { case 5: rs232_attr->c_cflag |= 5; break; case 6: rs232_attr->c_cflag |= CS6; break; case 7: rs232_attr->c_cflag |= CS7; break; case 8: rs232_attr->c_cflag |= CS8; break; default: fprintf (stderr, "error: unsupported character size databits[%d]\n", databits); return FAILURE; } return SUCCESS;}/* SET PARITY */RETURN setParity (struct termios *rs232_attr, char parity){ switch (parity) { case 'n': break; case 'e': rs232_attr->c_cflag |= PARENB; break; case 'o': rs232_attr->c_cflag |= PARODD; break; default: fprintf (stderr, "error: unsupported parity mode\n"); return FAILURE; } return SUCCESS;}/* SET STOP BITS */RETURN setStopBits (struct termios *rs232_attr, int stopbits){ switch (stopbits) { case 1: break; case 2: rs232_attr->c_cflag |= CSTOPB; break; default: fprintf (stderr, "error: unsupported number of stop bits\n"); return FAILURE; } return SUCCESS;}/* SET HARDWARE and SOFTWARE FLOW CONTROL */RETURN setFlowCtl (struct termios *rs232_attr, int xonxoff, int rtscts){ if (xonxoff) { rs232_attr->c_iflag |= IXON; rs232_attr->c_iflag |= IXOFF; } if (rtscts) rs232_attr->c_cflag |= CRTSCTS; return SUCCESS;}/* SET AN INDIVIDUAL MODEM LINE */void modemLine (int *mdlns, int enabled, int mask){ if (enabled) *mdlns |= mask; else *mdlns &= ~mask;}RETURN SendComm(UNS8 *buff_sg, S32 size, UNS32 model_id){ S8 devicename[MAXFILECHARS]; tCommParms commParm; /* serial I/O parameters */ tCmdParms cmdParm; /* serial I/O command parameters */ strcpy (devicename, "/dev/ttyS1");//Device Name commParm.device = devicename; commParm.baudrate = 9600; commParm.databits = 8; commParm.parity = 'n'; commParm.stopbits = 1; commParm.xonxoff = OFF; commParm.rtscts = OFF; cmdParm.write = ON; cmdParm.wait = 0.2; /* old val = 0.5 */ cmdParm.read = OFF; cmdParm.readlength = 255; cmdParm.pause = cmdParm.wait; cmdParm.msec = OFF; Get_PTZ_Params(model_id, &commParm); rs232_main (&commParm, &cmdParm, buff_sg, size); return SUCCESS;}void Get_PTZ_Params(UNS32 vendor , tCommParms *commParm){ switch(vendor) { case PTZ_NK97CHE_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); case PTZ_LVCA720_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); case PTZ_DRX502A_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); //case PTZ_DRX502A_RX_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); case PTZ_MRX1000_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); //case PTZ_GC755NP_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); case PTZ_SPD1600_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); case PTZ_SCC641_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); //case PTZ_CNBTECH_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); case PTZ_PelcoP_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); //case PTZ_TC8560_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); //case PTZ_WPT101_ID: //m_Com.SetComPort( m_iPort, 9600, 8, 1, NOPARITY ); commParm->baudrate = 9600; commParm->databits = 8; commParm->parity = 'n'; commParm->stopbits = 1; break; case PTZ_SJ2819RX_ID: //m_Com.SetComPort( m_iPort, 4800, 8, 1, EVENPARITY ); commParm->baudrate = 4800; commParm->databits = 8; commParm->parity = 'e'; commParm->stopbits = 1; break; case PTZ_PelcoD_ID: //m_Com.SetComPort( m_iPort, 2400, 8, 1, NOPARITY ); commParm->baudrate = 2400; commParm->databits = 8; commParm->parity = 'n'; commParm->stopbits = 1; break; default: m_DEBUG("\nPTZ_UNKNOWN_ID\n"); break; }}/***************************************************************************** * MAIN LOOP * --------------------------------------------------------------------------- * inputs: * commParm serial I/O settings * cmdParm serial I/O command settings * mdln RS-232 modem control line settings - not used ***************************************************************************/void rs232_main (tCommParms *commParm, tCmdParms *cmdParm, UNS8 *str, UNS16 str_size){ char *readptr; char readbuf[MAXREADCHARS]; readptr = readbuf; signal (SIGINT, sighandler); signal (SIGTERM, sighandler); fd_ptz_rs232 = rs232_open (commParm->device); rs232_init (fd_ptz_rs232, commParm); if (cmdParm->write) { rs232_send (fd_ptz_rs232, str, str_size); rs232_wait (cmdParm->wait, FALSE); } printf("\n send "); fflush(stdout);#if 0//org code if (cmdParm->read) readptr = rs232_read (fd_ptz_rs232, readbuf, cmdParm->readlength);#endif rs232_close(fd_ptz_rs232);}/* SIGNAL HANDLER - CLEANUP */void sighandler (int whatever ){ rs232_close(fd_ptz_rs232);}void Abort (S32 rs232){ if (rs232 != -1) rs232_close(fd_ptz_rs232);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -