怎样操作串口

ShowMan 2008-03-01 10:33:35
我想用linux的标准接口来操作串口设备,写一个操作串口的应用程序,
问一下怎么操作,要用到linux哪些系统接口,哪个做过的大虾帮忙一下,DX。。
...全文
57 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ShowMan 2008-03-02
  • 打赏
  • 举报
回复
多谢。。
cceczjxy 2008-03-01
  • 打赏
  • 举报
回复
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix 标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX 终端控制定义*/
#include <errno.h> /*错误号定义*/
#include <termios.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


//应该把中断都屏蔽到
int main()
{
int fd;
struct termios Options;
int buff[512];
int nread;
/*以读写方式打开串口*/
fd = open( "/dev/ttyS1", O_RDWR);
if (-1 == fd)
{
/* 不能打开串口一 ,则提示错误,*/
perror(" 提示错误!");
}

tcgetattr(fd, &Options);
cfsetispeed(&Options,B9600); /*设置为波特率为9600Bps*/
cfsetospeed(&Options,B9600);
Options.c_cflag &= ~CSIZE;
Options.c_cflag |= CS8; //设置为8个字节
Options.c_cflag &= ~PARENB;
Options.c_cflag &= ~CSTOPB ; //无校验位
Options.c_cflag &= ~CSTOPB; //一个停止位
tcsetattr(fd,TCSANOW,&Options) ; //使设置的波特率生效

/*下面是读串口的程序*/
int status;
ioctl(fd, FIONREAD, &status);
if(status>0)
nread = read(fd, buff, status);
printf("\nLen %d\n",nread);

close(fd);
exit (0);
}

简单的例子。
把read那换成write就成写程序了。
独孤过儿 2008-03-01
  • 打赏
  • 举报
回复
你的应用程序都涉及到什么操作啊?通过串口读写操作?

给你篇文章,希望对你有用...

http://hi.baidu.com/lenev/blog/item/c8540a44667afb86b2b7dcb9.html

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧