转自:http://blog.csdn.net/sunrier/article/details/7661289
//serialport.h
-
-
-
-
-
-
-
-
-
#ifndef _SERIALPORT_H_
-
#define _SERIALPORT_H_
-
-
#ifdef __cplusplus
-
extern "C" {
-
#endif
-
-
int open_port(int iPortNumber);
-
int set_port(int fd,int iBaudRate,int iDataSize,char cParity,int iStopBit);
-
int read_port(int fd,void *buf,int iByte);
-
int write_port(int fd,void *buf,int iByte);
-
int close_port(int fd);
-
-
#ifdef __cplusplus
-
}
-
#endif
-
-
#endif
//serialport.c
-
-
-
-
-
-
-
-
-
-
#include
-
#include
-
#include
-
#include
-
#include
-
-
int open_port(int iPortNumber)
-
{
-
int fd = -1;
-
char *pDev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2","/dev/ttyS3",
-
"/dev/ttyS4","/dev/ttyS5","/dev/ttyS6","/dev/ttyS7",
-
"/dev/ttyS8","/dev/ttyS9","/dev/ttyS10","/dev/ttyS11"};
-
-
switch( iPortNumber )
-
{
-
case 1:
-
case 2:
-
case 3:
-
case 4:
-
case 5:
-
case 6:
-
case 7:
-
case 8:
-
case 9:
-
case 10:
-
case 11:
-
case 12:
-
fd = open(pDev[iPortNumber-1],O_RDWR|O_NOCTTY|O_NDELAY);
-
if( fd<0 )
-
{
-
perror("Can't Open Serial Port !");
-
return (-1);
-
}
-
else
-
{
-
printf("Open ttyS%d ......\n",iPortNumber-1);
-
}
-
break;
-
default:
-
-
printf("Don't exist iPortNumber%d under /dev/? !\n",iPortNumber);
-
return (-1);
-
}
-
-
if( fcntl(fd,F_SETFL,0)<0 )
-
{
-
printf("fcntl failed !\n");
-
return (-1);
-
}
-
else
-
{
-
printf("fcntl = %d !\n",fcntl(fd,F_SETFL,0));
-
}
-
-
-
if( !isatty(STDIN_FILENO) )
-
{
-
printf("Standard input isn't a terminal device !\n");
-
return (-1);
-
}
-
else
-
{
-
printf("It's a serial terminal device!\n");
-
}
-
-
printf("open_port file ID= %d !\n",fd);
-
-
return fd;
-
-
}
-
-
int set_port(int fd,int iBaudRate,int iDataSize,char cParity,int iStopBit)
-
{
-
int iResult = 0;
-
struct termios oldtio,newtio;
-
-
-
iResult = tcgetattr(fd,&oldtio);
-
if( iResult )
-
{
-
perror("Can't get old terminal description !");
-
return (-1);
-
}
-
-
-
bzero(&newtio,sizeof(newtio));
-
newtio.c_cflag |= CLOCAL | CREAD;
-
-
-
switch( iBaudRate )
-
{
-
case 2400:
-
cfsetispeed(&newtio,B2400);
-
cfsetospeed(&newtio,B2400);
-
break;
-
case 4800:
-
cfsetispeed(&newtio,B4800);
-
cfsetospeed(&newtio,B4800);
-
break;
-
case 9600:
-
cfsetispeed(&newtio,B9600);
-
cfsetospeed(&newtio,B9600);
-
break;
-
case 19200:
-
cfsetispeed(&newtio,B19200);
-
cfsetospeed(&newtio,B19200);
-
break;
-
case 38400:
-
cfsetispeed(&newtio,B38400);
-
cfsetospeed(&newtio,B38400);
-
break;
-
case 57600:
-
cfsetispeed(&newtio,B57600);
-
cfsetospeed(&newtio,B57600);
-
break;
-
case 115200:
-
cfsetispeed(&newtio,B115200);
-
cfsetospeed(&newtio,B115200);
-
break;
-
case 460800:
-
cfsetispeed(&newtio,B460800);
-
cfsetospeed(&newtio,B460800);
-
break;
-
default :
-
-
printf("Don't exist iBaudRate %d !\n",iBaudRate);
-
return (-1);
-
}
-
-
-
newtio.c_cflag &= (~CSIZE);
-
switch( iDataSize )
-
{
-
case 7:
-
newtio.c_cflag |= CS7;
-
break;
-
case 8:
-
newtio.c_cflag |= CS8;
-
break;
-
default:
-
-
printf("Don't exist iDataSize %d !\n",iDataSize);
-
return (-1);
-
}
-
-
-
switch( cParity )
-
{
-
case 'N':
-
newtio.c_cflag &= (~PARENB);
-
break;
-
case 'O':
-
newtio.c_cflag |= PARENB;
-
newtio.c_cflag |= PARODD;
-
newtio.c_iflag |= (INPCK | ISTRIP);
-
break;
-
case 'E':
-
newtio.c_cflag |= PARENB;
-
newtio.c_cflag &= (~PARODD);
-
newtio.c_iflag |= (INPCK | ISTRIP);
-
break;
-
default:
-
-
printf("Don't exist cParity %c !\n",cParity);
-
return (-1);
-
}
-
-
-
switch( iStopBit )
-
{
-
case 1:
-
newtio.c_cflag &= (~CSTOPB);
-
break;
-
case 2:
-
newtio.c_cflag |= CSTOPB;
-
break;
-
default:
-
-
printf("Don't exist iStopBit %d !\n",iStopBit);
-
return (-1);
-
}
-
-
newtio.c_cc[VTIME] = 0;
-
newtio.c_cc[VMIN] = 0;
-
tcflush(fd,TCIFLUSH);
-
iResult = tcsetattr(fd,TCSANOW,&newtio);
-
-
if( iResult )
-
{
-
perror("Set new terminal description error !");
-
return (-1);
-
}
-
-
printf("set_port success !\n");
-
-
return 0;
-
}
-
-
int read_port(int fd,void *buf,int iByte)
-
{
-
int iLen = 0;
-
if( !iByte )
-
{
-
printf("Read byte number error !\n");
-
return iLen;
-
}
-
-
iLen = read(fd,buf,iByte);
-
-
return iLen;
-
}
-
-
int write_port(int fd,void *buf,int iByte)
-
{
-
int iLen = 0;
-
if( !iByte )
-
{
-
printf("Write byte number error !\n");
-
return iLen;
-
}
-
-
iLen = write(fd,buf,iByte);
-
-
return iLen;
-
}
-
-
-
int close_port(int fd)
-
{
-
int iResult = -1;
-
-
iResult = close(fd);
-
-
return iResult;
-
}
//demo.c
-
-
-
-
-
-
-
-
-
#include
-
#include
-
#include "serialport.h"
-
-
int main(int argc,char *argv[])
-
{
-
int iResult = -1;
-
int fd = -1,iCommPort,iBaudRate,iDataSize,iStopBit;
-
char cParity;
-
int iLen;
-
char szBuffer[30];
-
-
iCommPort = 1;
-
fd = open_port(iCommPort);
-
if( fd<0 )
-
{
-
perror("open_port error !");
-
return 1;
-
}
-
-
iBaudRate = 115200;
-
iDataSize = 8;
-
cParity = 'N';
-
iStopBit = 1;
-
iResult = set_port(fd,iBaudRate,iDataSize,cParity,iStopBit);
-
if( iResult<0 )
-
{
-
perror("set_port error !");
-
return 1;
-
}
-
-
printf("fd = %d \n",fd);
-
-
-
-
iLen = write_port(fd,"Hello",5);
-
memset(szBuffer,0,sizeof(szBuffer));
-
iLen = read_port(fd,szBuffer,5);
-
if( iLen>0 )
-
-
-
printf("iLen = %d ,szBuffer = %s \n",iLen,szBuffer);
-
-
return 0;
-
}
阅读(1857) | 评论(0) | 转发(0) |