- 论坛徽章:
- 0
|
急,谁知道在UNIX下用C写MODEM传输程序?给您磕头了。 (无内容)
/**************************************************************
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 = NULL;
iSerNum = 0;
}
if( iSerNum >;= MAXSERIALNUM ) return (serOpenMany);
i = 0;
while( i<MAXSERIALNUM && pSerial!=NULL ) i++;
if( i >;= MAXSERIALNUM ) return (serOpenMany);
pSerial = (struct serialDef *)malloc(sizeof(struct serialDef));
if( pSerial == NULL ) return (serAllocMem);
pSer = pSerial;
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);
} |
|