歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux環境下2410開發板串口讀寫關鍵代碼

Linux環境下2410開發板串口讀寫關鍵代碼

日期:2017/3/1 10:33:43   编辑:Linux編程
整理原來的項目開發文檔,找到了曾經在2410開發板上做的串口讀寫程序的代碼。

現在貼出來供大家參考。

#include <qtopia/qpeapplication.h>

/************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
/***********************************/
#include <pthread.h>
#include <qnamespace.h>

int argc;
char **argv;
int fd,rc,ii,ret;
char rbuf[8];
char wbuf[8]="12345";

/****************/
int file,size,len;
int tmp_id=0;
int id_count=0;//已經收到的ID,計數
/****************/
char *dev ="/dev/ttyS1"; //串口號 /dev/ttyS1 對應於串口1


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;
}

void writeport(int fd,char *buf,int len) //發送數據
{
write(fd,buf,len);
}

void clearport(int fd) //如果出現數據與規約不符合,可以調用這個函數來刷新串口讀寫數據
{
tcflush(fd,TCIOFLUSH);
}

void *thread1(void *)
{
//線程 1
while(1)
{
rbuf[0]='\0';
/*超時太長,會丟失數據包,如果連續*/
rc=readport(fd,rbuf,5,50); //讀取5個字節,超時時間為100,500毫秒
if(rc!=-1)
{
rbuf[5]='\0';
//writeport(fd,wbuf,rc);
writeport(fd,rbuf,rc); //把收到的數據 發回
printf("Rev id:%s\n",rbuf);
}
//else
//{
// printf("Lost recv:%d\t Lost Text:%s\n",rc,rbuf);
// writeport(fd,wbuf,rc);
//}

}

close(fd); //關閉串口
pthread_exit(0);
}

void *thread2(void *)
{
QPEApplication a( argc, argv );
HelloForm f(0,"yunfly",Qt::WStyle_Customize|Qt::WStyle_NoBorder);

a.showMainWidget( &f );
//int result=a.exec();
a.exec();
pthread_exit(0);
}

int main( int a_rgc, char *a_rgv[] )
{
argc=a_rgc;
argv=a_rgv;
/********************初始化串口*************************/
// for(ii=0;ii<256;ii++)wbuf[ii]=ii;
fd=openport(dev); //打開串口
if(fd>0){
ret=setport(fd,115200,8,1,'N'); //設置串口,波特率,數據位,停止位,校驗
if(ret<0){
printf("Can't Set Serial Port!\n");
exit(0);
}
}
else
{
printf("Can't Open Serial Port!\n");
exit(0);
}
/*****************************************************/

pthread_t id1,id2;
int ret;
/*Create Thread 1: RS232 Serial */
ret=pthread_create(&id1,NULL,thread1,NULL);
if(ret!=0){
printf("Create pthread error!\n");
exit(1);
}
/*Create Thread 2:QT Window*/
ret=pthread_create(&id2,NULL,thread2,NULL);
if(ret!=0){
printf("Create pthread error!\n");
exit(1);
}

pthread_join(id1,NULL);
pthread_join(id2,NULL);
exit(0);
}

Copyright © Linux教程網 All Rights Reserved