- 论坛徽章:
- 0
|
/*---------------------------------
-- uart.c --------------
-- COM2 RECV&SEND --------------
-- Author Fox.Yu --------------
-- 2008 - 06 - 23 --------------
----------------------------------*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "uart.h"
#define TTY_DEV "/dev/ttyS"
#define TIMEOUT_SEC(buflen,baud) (buflen*20/baud+2) //recerve timeout
#define TIMEOUT_USEC 0
/*----- GET THE DEVICE NAME -----------------*/
char *get_ptty(pportinfo_t pportinfo)
{
char *ptty;
switch(pportinfo->tty){
case '0':
ptty=TTY_DEV"0";
break;
case '1':
ptty=TTY_DEV"1";
break;
case '2':
ptty=TTY_DEV"2";
break;
}
return(ptty);
}
/*---- CONVERT THE BAUDRATE -------------------*/
int convbaud(unsigned long int baudrate)
{
switch(baudrate){
case 2400:
return B2400;
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
default:
return B9600;
}
}
/*---------- SET THE COM PORT -------------------*/
int PortSet(int fdcom,const pportinfo_t pportinfo)
{
struct termios termios_old,termios_new;
int baudrate,tmp;
char databit,stopbit,parity,fctl;
bzero(&termios_old,sizeof(termios_old));// clear the sizeof old
bzero(&termios_new,sizeof(termios_new));// clear the sizeof new
cfmakeraw(&termios_new);
tcgetattr(fdcom,&termios_old);
baudrate = convbaud(pportinfo->baudrate);
cfsetispeed(&termios_new,baudrate);
cfsetospeed(&termios_new,baudrate);
termios_new.c_cflag |= CLOCAL ;
//control mode,promise the PROGRAM is not the owner of the COM
termios_new.c_cflag |= CREAD; //control mode,enable it read the input data
/*----- flow control ----------*/
fctl=pportinfo->fctl;
switch(fctl){
case '0':
termios_new.c_cflag &= ~CRTSCTS; //no flow control
break;
case '1':
termios_new.c_cflag |= CRTSCTS; //hardware flow control
break;
case '2':
termios_new.c_cflag |= IXON|IXOFF|IXANY;//software flow control
break;
}
termios_new.c_cflag &=~CSIZE;//control mode ,disable the char bit
/*----- DATA BIT ------------*/
databit = pportinfo ->databit;
switch(databit){
case '5':
termios_new.c_cflag |= CS5;
case '6':
termios_new.c_cflag |= CS6;
case '7':
termios_new.c_cflag |= CS7;
default:
termios_new.c_cflag |= CS8;
}
/*---- PARITY CHECK ----------*/
parity = pportinfo->parity;
switch(parity){
case '0':
termios_new.c_cflag &=~PARENB; //no parity check
break;
case '1':
termios_new.c_cflag |=PARENB; //odd check
termios_new.c_cflag &=~PARODD;
break;
case '2':
termios_new.c_cflag |=PARENB;
termios_new.c_cflag |=PARODD;
break;
}
/*-------STOPBIT --------------*/
stopbit = pportinfo -> stopbit;
if(stopbit == '2'){
termios_new.c_cflag |=CSTOPB; //2 stop bits
}
else{
termios_new.c_cflag &=~CSTOPB; //1 stop bits
}
/*----other attributions default -----*/
termios_new.c_oflag &=~OPOST;
termios_new.c_cc[VMIN] = 1;
termios_new.c_cc[VTIME] = 1;
tcflush(fdcom,TCIFLUSH);
tmp=tcsetattr(fdcom,TCSANOW,&termios_new);
tcgetattr(fdcom,&termios_old);
return(tmp);
}
/*--- OPEN THE COM PORT -----------*/
int PortOpen(pportinfo_t pportinfo){
int fdcom;
char *ptty;
ptty = get_ptty(pportinfo);
fdcom = open(ptty,O_RDWR|O_NOCTTY|O_NONBLOCK);
return (fdcom);
}
/*------CLOSE THE COM PORT ---------*/
void PortClose(int fdcom){
close(fdcom);
}
/*------ SEND THE DATA ------------*/
int PortSend(int fdcom,char *data,int datalen){
int len =0;
len = write(fdcom,data,datalen);
if(len == datalen){
return(len);
}
else{
tcflush(fdcom,TCOFLUSH);
return -1;
}
}
/*------ RECEIVE THE DATA ------------*/
int PortRecv(int fdcom,char *data,int datalen,int baudrate){
int readlen,fs_sel;
fd_set fs_read;
struct timeval tv_timeout;
FD_ZERO(&fs_read);
FD_SET(fdcom,&fs_read);
tv_timeout.tv_sec = TIMEOUT_SEC(datalen,baudrate);
tv_timeout.tv_usec = TIMEOUT_USEC;
fs_sel = select(fdcom+1,&fs_read,NULL,NULL,&tv_timeout);
if(fs_sel){
readlen= read(fdcom,data,datalen);
return (readlen);
}
else{
return (-1);
}
return (readlen);
}
/****************************************
** test *****************************
****************************************/
int main(int argc,char *argv[])
{
int fdcom,i,SendLen,RecvLen;
struct termios termios_cur;
char RecvBuf[10];
portinfo_t portinfo = {
'0',
38400,
'8',
'0',
'0',
'0',
'1',
'0',
'1',
0
};
if(argc != 2){
printf("Usage: \n");
printf(" eg:");
printf(" MyPort 3");
exit(-1);
}
fdcom = PortOpen(&portinfo);
if(fdcom 0){
printf("No %d send %d data 1234567890.\n",i,SendLen);
}
else{
printf("Error,send failed.\n");
}
sleep(1);
}
PortClose(fdcom);
}
else{
for(;;){
RecvLen = PortRecv(fdcom,RecvBuf,10,portinfo.baudrate);
if(RecvLen >0){
for(i=0;i
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/74296/showart_1091969.html |
|