UNIX下用C写MODEM传输程序


首先要打开串口,然后通过串口来向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; ipSerial[i] = NULL;
iSerNum = 0;
}if( iSerNum >= MAXSERIALNUM ) return (serOpenMany);i = 0;
while( iif( 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 waitingswitch( 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;

推荐阅读