您的位置:首页技术文章
文章详情页

UNIX下用C写MODEM传输程序

【字号: 日期:2024-06-11 15:37:06浏览:4作者:猪猪

首先要打开串口,然后通过串口来向modem发at指令来进行通讯,编程的时候要特别注意要用适当的方式打开串口,其实对TTY的操作跟一般的文件操作没有特别的区别,区别就是在于串口通讯需要设置串口属性.

/************************************************************** Function : open serial comport Param In : pszDevName : comport device name, eg.'/dev/tty1a' uiBaud : baudrate. 50 -- 9600 bParity : parity. NOPRAITY, ODDPARITY, EVENPARITY bByteSize : size of a byte. 5 - 8 bStopBits : stop bits. 1 - 2 bFlowControl : flow control. NOFLOW, CTSRTS, XONXOFF uiOutQueueLen : length of output buffer queue uiInQueueLen : length of input buffer queue iDefTimeOut : default timeout bOperFlag : OP_READ , OP_WRITE Param Out : none Return Code >=0 : success, handle of this comport <0 : serOpenMany serAllocMem serOpenInFp serOpenOutFp serFlowCtrlBad serBaudBad serByteSizeBad serStopBitsBad serParityBad serSetInBuffer serSetOutBuffer ***************************************************************/ int serOpen(char *pszDevName, uint uiBaud, uchar bParity, uchar bByteSize, uchar bStopBits, uchar bFlowControl, uint uiOutQueueLen, uint uiInQueueLen, int iDefTimeOut, uchar bOperFlag ) { struct serialDef *pSer; struct termio termioNew; int i, fdIn, fdOut; int serHandle;

if( iSerNum == -1 ) // initial struct pSerial { for( i=0; i<MAXSERIALNUM; i++ ) pSerial[i] = NULL; iSerNum = 0; }

if( iSerNum >= MAXSERIALNUM ) return (serOpenMany);

i = 0; while( i<MAXSERIALNUM && pSerial[i]!=NULL ) i++; if( i >= MAXSERIALNUM ) return (serOpenMany); pSerial[i] = (struct serialDef *)malloc(sizeof(struct serialDef)); if( pSerial[i] == NULL ) return (serAllocMem);

pSer = pSerial[i]; pSer->pusInBuffer = (uchar *)malloc(uiInQueueLen); if( pSer->pusInBuffer==NULL ) { free(pSer); return (serAllocMem); } pSer->pusOutBuffer = (uchar *)malloc(uiOutQueueLen); if( pSer->pusOutBuffer==NULL ) { free(pSer->pusInBuffer); free(pSer); return (serAllocMem); } pSer->uiInQueueLen = uiInQueueLen; pSer->uiOutQueueLen = uiOutQueueLen;

serHandle = i;

if( bOperFlag & OP_READ ) { if( (pSer->fpIn=fopen(pszDevName, 'rb')) == NULL ) return (serOpenInFp); fdIn = fileno(pSer->fpIn); ioctl( fdIn, TCGETA, &termioNew ); }

if( bOperFlag & OP_WRITE ) { if( (pSer->fpOut=fopen(pszDevName, 'wb')) == NULL ) return (serOpenOutFp); fdOut = fileno(pSer->fpOut); ioctl( fdOut, TCGETA, &termioNew ); }

pSer->iDefTimeOut = iDefTimeOut;

/* termioNew.c_iflag=0; termioNew.c_oflag=0; termioNew.c_lflag=0; termioNew.c_line=0; termioNew.c_cflag = ( 0x0CBD&~CBAUD | CTSFLOW | RTSFLOW ) ; termioNew.c_cc[VEOF]=1; termioNew.c_cc[VEOL]=0; */

termioNew.c_cflag = ( 0x0CBD&~CBAUD ) ; termioNew.c_cc[VMIN] = 1; // A read operation is not satisfIEd until receive // one character termioNew.c_cc[VTIME] = 0; // A read operation will waiting

switch( bFlowControl ) { case NOFLOW: break; case CTSRTS: termioNew.c_cflag |= CTSFLOW | RTSFLOW; break; case XONXOFF: termioNew.c_iflag |= IXON | IXOFF; break; default : return (serFlowCtrlBad); }//switch;

switch( uiBaud ) { case 50: termioNew.c_cflag|=B50; break; case 75: termioNew.c_cflag|=B75; break; case 110: termioNew.c_cflag|=B110; break; case 134: termioNew.c_cflag|=B134; break; case 150: termioNew.c_cflag|=B150; break; case 200: termioNew.c_cflag|=B200; break; case 300: termioNew.c_cflag|=B300; break; case 600: termioNew.c_cflag|=B600; break; case 1200: termioNew.c_cflag|=B1200; break; case 1800: termioNew.c_cflag|=B1800; break; case 2400: termioNew.c_cflag|=B2400; break; case 4800: termioNew.c_cflag|=B4800; break; case 9600: termioNew.c_cflag|=B9600; break; default: return (serBaudBad); }

switch( bByteSize ) { case 5: termioNew.c_cflag|=CS5; break; case 6: termioNew.c_cflag|=CS6; break; case 7: termioNew.c_cflag|=CS7; break; case 8: termioNew.c_cflag|=CS8; break; default: return (serByteSizeBad); }

switch( bStopBits ) { case 1: break; case 2: termioNew.c_cflag|=CSTOPB; break; default: return (serStopBitsBad); }

switch( bParity ) { case NOPARITY: break; case ODDPARITY: termioNew.c_cflag|=PARODD; break; case EVENPARITY: termioNew.c_cflag|=PARENB; break; default: return (serParityBad); } termioNew.c_cflag |= CLOCAL; if( bOperFlag & OP_WRITE ) { ioctl(fdOut, TCGETA, &pSer->termioOld); ioctl(fdOut, TCSETA, &termioNew); }

if(bOperFlag & OP_READ) { ioctl( fdIn, TCGETA, &pSer->termioOld ); termioNew.c_cflag|=CREAD; ioctl( fdIn, TCSETA, &termioNew); }

if(bOperFlag & OP_READ) { if( setvbuf( pSer->fpIn,(char *)pSer->pusInBuffer,_IOFBF,uiInQueueLen )) return (serSetInBuffer); fflush(pSer->fpIn); } if(bOperFlag & OP_WRITE) { if( setvbuf(pSer->fpOut,(char *)pSer->pusOutBuffer,_IOFBF,uiOutQueueLen )) return (serSetOutBuffer); fflush(pSer->fpOut); } pSer->bOperFlag = bOperFlag;

iSerNum++;

return (serHandle); }

/************************************************************** Function : receive bytes from serial comport Param In : iHandle : handle returned by serOpen uiRecvCnt: bytes of accii buffer iTimeOut : timeout if equals 0 use default timeout Param Out : psBuf : ascii buffer which will be received Return Code: =0 : OK <0 : serTimeOut serRecvFail ***************************************************************/ int serReceiveBuf( int iHandle, char *psBuf, uint uiRecvCnt, int iTimeOut ) { int iCnt, iRet; int fdIn;

fdIn = fileno( pSerial[iHandle]->fpIn );

if( setjmp(tohere2) ) return (serTimeOut); signal( SIGALRM, alrm2 ); if( iTimeOut>0 ) alarm(iTimeOut); else if( iTimeOut==0 ) alarm( pSerial[iHandle]->iDefTimeOut );

for( iCnt=0; iCnt<uiRecvCnt; iCnt++ ) { while( ( iRet=read(fdIn,psBuf+iCnt,1) ) == 0 ); if( iRet<0 ) { alarm(0); return (serRecvFail); } } alarm(0); return (serOK); }

标签: Unix系统