int openport(char *Dev) //打开串口
int setport(int fd, int baud,int databits,int stopbits,int parity)//设置串口,波特率,数据位,停止位,校验
int readport(int fd,char *buf,int len,int maxwaittime)//读数据,参数为串口,BUF,长度,超时时间
int writeport(int fd,char *buf,int len) //发送数据
void clearport(int fd) //如果出现数据与规约不符合,可以调用这个函数来刷新串口读写数据
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/time.h>
int openport(char *Dev)
{
int fd = open( Dev, O_RDWR|O_NOCTTY|O_NDELAY );
if (-1 == fd)
{
perror("Can''t Open Serial Port");
return -1;
}
else
return fd;
}
int setport(int fd, int baud,int databits,int stopbits,int parity)
{
int baudrate;
struct termios newtio;
switch(baud)
{
case 300:
baudrate=B300;
break;
case 600:
baudrate=B600;
break;
case 1200:
baudrate=B1200;
break;
case 2400:
baudrate=B2400;
break;
case 4800:
baudrate=B4800;
break;
case 9600:
baudrate=B9600;
break;
case 19200:
baudrate=B19200;
break;
case 38400:
baudrate=B38400;
break;
default :
baudrate=B9600;
break;
}
tcgetattr(fd,&newtio);
bzero(&newtio,sizeof(newtio));
//setting c_cflag
newtio.c_cflag &=~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
newtio.c_cflag |= CS7; //7位数据位
break;
case 8:
newtio.c_cflag |= CS8; //8位数据位
break;
default:
newtio.c_cflag |= CS8;
break;
}
switch (parity) //设置校验
{
case 'n':
case 'N':
newtio.c_cflag &= ~PARENB; /* Clear parity enable */
newtio.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
newtio.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
newtio.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
newtio.c_cflag |= PARENB; /* Enable parity */
newtio.c_cflag &= ~PARODD; /* 转换为偶效验*/
newtio.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;break;
default:
newtio.c_cflag &= ~PARENB; /* Clear parity enable */
newtio.c_iflag &= ~INPCK; /* Enable parity checking */
break;
}
switch (stopbits)//设置停止位
{
case 1:
newtio.c_cflag &= ~CSTOPB; //1
break;
case 2:
newtio.c_cflag |= CSTOPB; //2
break;
default:
newtio.c_cflag &= ~CSTOPB;
break;
}
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
newtio.c_cflag |= (CLOCAL|CREAD);
newtio.c_oflag|=OPOST;
newtio.c_iflag &=~(IXON|IXOFF|IXANY);
cfsetispeed(&newtio,baudrate);
cfsetospeed(&newtio,baudrate);
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&newtio) != 0)
{
perror("SetupSerial 3");
return -1;
}
return 0;
}
int readport(int fd,char *buf,int len,int maxwaittime)//读数据,参数为串口,BUF,长度,超时时间
{
int no="0";int rc;int rcnum="len";
struct timeval tv;
fd_set readfd;
tv.tv_sec=maxwaittime/1000; //SECOND
tv.tv_usec=maxwaittime%1000*1000; //USECOND
FD_ZERO(&readfd);
FD_SET(fd,&readfd);
rc=select(fd+1,&readfd,NULL,NULL,&tv);
if(rc>0)
{
while(len)
{
rc=read(fd,&buf[no],1);
if(rc>0)
no=no+1;
len=len-1;
}
if(no!=rcnum)
return -1; //如果收到的长度与期望长度不一样,返回-1
return rcnum; //收到长度与期望长度一样,返回长度
}
else
{
return -1;
}
return -1;
}
int writeport(int fd,char *buf,int len) //发送数据
{
write(fd,buf,len);
}
void clearport(int fd) //如果出现数据与规约不符合,可以调用这个函数来刷新串口读写数据
{
tcflush(fd,TCIOFLUSH);
}
main()
{
int fd,rc,i,ret;
unsigned char rbuf[256];
unsigned char wbuf[256]="";
for(i=0;i<256;i++)
wbuf[i]=i;
char *dev ="/dev/ttyS0"; //串口号 /dev/ttyS0 对应于串口1
fd = openport(dev); //打开串口
if(fd>0)
{
ret=setport(fd,4800,8,1,'o'); //设置串口,波特率,数据位,停止位,校验
if(ret<0)
{
printf("Can't Set Serial Port!\n");
exit(0);
}
}
else
{
printf("Can't Open Serial Port!\n");
exit(0);
}
while(1){
rc=readport(fd,rbuf,5,500); //读取5个字节,超时时间为500毫秒
if(rc>0)
{
writeport(fd,wbuf,rc);
printf("recv:%d\n",rc);
for(i=0;i<rc;i++)
printf("%02x ",rbuf[i]);
printf("\n");
}
else
printf("recv none\n");
}
close(fd);
}
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/time.h>
int openport(char *Dev)
{
int fd = open( Dev, O_RDWR|O_NOCTTY|O_NDELAY );
if (-1 == fd)
{
perror("Can''t Open Serial Port");
return -1;
}
else
return fd;
}
int setport(int fd, int baud,int databits,int stopbits,int parity)
{
int baudrate;
struct termios newtio;
switch(baud)
{
case 300:
baudrate=B300;
break;
case 600:
baudrate=B600;
break;
case 1200:
baudrate=B1200;
break;
case 2400:
baudrate=B2400;
break;
case 4800:
baudrate=B4800;
break;
case 9600:
baudrate=B9600;
break;
case 19200:
baudrate=B19200;
break;
case 38400:
baudrate=B38400;
break;
default :
baudrate=B9600;
break;
}
tcgetattr(fd,&newtio);
bzero(&newtio,sizeof(newtio));
//setting c_cflag
newtio.c_cflag &=~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
newtio.c_cflag |= CS7; //7位数据位
break;
case 8:
newtio.c_cflag |= CS8; //8位数据位
break;
default:
newtio.c_cflag |= CS8;
break;
}
switch (parity) //设置校验
{
case 'n':
case 'N':
newtio.c_cflag &= ~PARENB; /* Clear parity enable */
newtio.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
newtio.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
newtio.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
newtio.c_cflag |= PARENB; /* Enable parity */
newtio.c_cflag &= ~PARODD; /* 转换为偶效验*/
newtio.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;break;
default:
newtio.c_cflag &= ~PARENB; /* Clear parity enable */
newtio.c_iflag &= ~INPCK; /* Enable parity checking */
break;
}
switch (stopbits)//设置停止位
{
case 1:
newtio.c_cflag &= ~CSTOPB; //1
break;
case 2:
newtio.c_cflag |= CSTOPB; //2
break;
default:
newtio.c_cflag &= ~CSTOPB;
break;
}
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
newtio.c_cflag |= (CLOCAL|CREAD);
newtio.c_oflag|=OPOST;
newtio.c_iflag &=~(IXON|IXOFF|IXANY);
cfsetispeed(&newtio,baudrate);
cfsetospeed(&newtio,baudrate);
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&newtio) != 0)
{
perror("SetupSerial 3");
return -1;
}
return 0;
}
int readport(int fd,char *buf,int len,int maxwaittime)//读数据,参数为串口,BUF,长度,超时时间
{
int no="0";int rc;int rcnum="len";
struct timeval tv;
fd_set readfd;
tv.tv_sec=maxwaittime/1000; //SECOND
tv.tv_usec=maxwaittime%1000*1000; //USECOND
FD_ZERO(&readfd);
FD_SET(fd,&readfd);
rc=select(fd+1,&readfd,NULL,NULL,&tv);
if(rc>0)
{
while(len)
{
rc=read(fd,&buf[no],1);
if(rc>0)
no=no+1;
len=len-1;
}
if(no!=rcnum)
return -1; //如果收到的长度与期望长度不一样,返回-1
return rcnum; //收到长度与期望长度一样,返回长度
}
else
{
return -1;
}
return -1;
}
int writeport(int fd,char *buf,int len) //发送数据
{
write(fd,buf,len);
}
void clearport(int fd) //如果出现数据与规约不符合,可以调用这个函数来刷新串口读写数据
{
tcflush(fd,TCIOFLUSH);
}
main()
{
int fd,rc,i,ret;
unsigned char rbuf[256];
unsigned char wbuf[256]="";
for(i=0;i<256;i++)
wbuf[i]=i;
char *dev ="/dev/ttyS0"; //串口号 /dev/ttyS0 对应于串口1
fd = openport(dev); //打开串口
if(fd>0)
{
ret=setport(fd,4800,8,1,'o'); //设置串口,波特率,数据位,停止位,校验
if(ret<0)
{
printf("Can't Set Serial Port!\n");
exit(0);
}
}
else
{
printf("Can't Open Serial Port!\n");
exit(0);
}
while(1){
rc=readport(fd,rbuf,5,500); //读取5个字节,超时时间为500毫秒
if(rc>0)
{
writeport(fd,wbuf,rc);
printf("recv:%d\n",rc);
for(i=0;i<rc;i++)
printf("%02x ",rbuf[i]);
printf("\n");
}
else
printf("recv none\n");
}
close(fd);
}
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1350/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表