linux 用open打不开串口

linux 用open打不开串口,第1张

*** 作硬件之前都是要先open设备,先来分析下这里的open函数具体做了那些工作(做了大量工作 ,真的!)。 应用层通过open系统调用open(“/dev/s3c2410_serial0”,)一层一层调用到会调用到tty_open。因为串口在linux下是作为tty设备的,结合前面的注册过程可以分析这里首先调用的就是tty_open这个函数。cdev_init(&driver->cdev, &tty_fops)cdev_init(&driver->cdev, &tty_fops)因为根据注册的时候将s3c2410_serial0注册为一个字符设备,字符设备对应的驱动为tty_fops 详细介绍查看下《linux就该这么学》

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#include <errno.h>

#define BUFFER_SIZE 1024

int main(int argc,char *argv[])

{

int from_fd,to_fd

int bytes_read,bytes_write

char buffer[BUFFER_SIZE]

char *ptr

if(argc!=3)

{

fprintf(stderr,"usage:%s fromfile tofile\n",argv[0])

exit(1)

}

if(from_fd=open(argv[1],O_RDONLY|O_NONBLOCK)==-1)

{

fprintf(stderr,"open %s error!\n",argv[1])

exit(1)

}

memset(buffer, 0x00,sizeof(buffer))

printf("read_start....\n")

bytes_read=read(from_fd,buffer,BUFFER_SIZE)

printf("bytes_read=[%d],buffer=[%s]\n", bytes_read,buffer)

}

这个是3.c文件,编译 gcc -o 33 3.c;

执行 ./33 a.c(其中a.c中的内容是'hello world');

但是程序执行到最后的时候就阻塞掉了,不知道为啥? 各位大虾帮帮忙if(argc!=3)

{

fprintf(stderr,"usage:%s fromfile tofile\n",argv[0])

exit(1)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7333606.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存