日期:2014-05-16  浏览次数:20891 次

linux 中断处理串口通讯
诸位大侠好,第一次发帖,多多帮助。

 想用上位机串口连接S3C2440,上位机发送命令给S3C2440。 我想利用中断实现S3C2440的串口接受数据部分,不知道怎么实现。在linux下的编程。
  我的想法是, S3C2440 处理其他事情,当有数据传送过来的时候,再进行处理数据。不知道怎么实现? 请诸位帮忙,最好有代码给我模仿一下。谢谢。谢谢。

------解决方案--------------------
呵呵,很简单啊,只要把select函数好好看看就行了,祝你成功,首先得心静下来
------解决方案--------------------
使用异步io

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termio.h>

int tty_mode(int how)
{
static struct termios original_mode;
if(how==0)
 tcgetattr(0,&original_mode);
else return tcsetattr(0,TCSANOW,&original_mode);
}
void set_crmode()
{
struct termios ttystate;
tcgetattr(0,&ttystate);
ttystate.c_lflag &= ~ICANON;
ttystate.c_lflag &=~ECHO;
ttystate.c_cc[VMIN]=1;
tcsetattr(0,TCSANOW,&ttystate);
}

int signal_io(int sig)
{
static char buf[128],ch=0;
static int len=0;
//memset(buf,0,sizeof(buf));
//printf("aaaa\n");
if(read(0,&ch,1)==1)
{
if(ch!=127)
{
printf("%c",ch);
buf[len++]=ch;
}
else {
if(len>0)
buf[--len]=0;
printf("\b \b");
}
}
//perror(NULL);
//printf("%d\n",ch);
fflush(stdout);
}
int main()
{
int flag=0;
tty_mode(0);
set_crmode();
signal(SIGIO,signal_io);
fcntl(0,F_SETOWN,getpid());
flag=fcntl(0,F_GETFL);
fcntl(0,F_SETFL,flag|O_ASYNC);
while(1)
pause();
tty_mode(1);
}