当前位置: 技术问答>linux和unix
Linux下串口编程问题
来源: 互联网 发布时间:2016-07-13
本文导语: #include #include #include #include #include #include #include #include #include #define TRUE 1 //初始化串口选项: void setTermios(struct termios * pNewtio,...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define TRUE 1
//初始化串口选项:
void setTermios(struct termios * pNewtio, int uBaudRate)
{
bzero(pNewtio, sizeof(struct termios)); /* clear struct for new port settings */
//8N1
pNewtio->c_cflag = uBaudRate | CS8 | CREAD | CLOCAL;
pNewtio->c_iflag = IGNPAR;
pNewtio->c_oflag = 0;
pNewtio->c_lflag = 0; //non ICANON
/*
initialize all control characters
default values can be found in /usr/include/termios.h, and
are given in the comments, but we don't need them here
*/
pNewtio->c_cc[VINTR] = 0; /* Ctrl-c */
pNewtio->c_cc[VQUIT] = 0; /* Ctrl- */
pNewtio->c_cc[VERASE] = 0; /* del */
pNewtio->c_cc[VKILL] = 0; /* @ */
pNewtio->c_cc[VEOF] = 4; /* Ctrl-d */
pNewtio->c_cc[VTIME] = 5; /* inter-character timer, timeout VTIME*0.1 */
pNewtio->c_cc[VMIN] = 0; /* blocking read until VMIN character arrives */
pNewtio->c_cc[VSWTC] = 0; /* '' */
pNewtio->c_cc[VSTART] = 0; /* Ctrl-q */
pNewtio->c_cc[VSTOP] = 0; /* Ctrl-s */
pNewtio->c_cc[VSUSP] = 0; /* Ctrl-z */
pNewtio->c_cc[VEOL] = 0; /* '' */
pNewtio->c_cc[VREPRINT] = 0; /* Ctrl-r */
pNewtio->c_cc[VDISCARD] = 0; /* Ctrl-u */
pNewtio->c_cc[VWERASE] = 0; /* Ctrl-w */
pNewtio->c_cc[VLNEXT] = 0; /* Ctrl-v */
pNewtio->c_cc[VEOL2] = 0; /* '' */
}
#define BUFSIZE 512
int main(int argc, char **argv)
{
int fd;
int nread;
char buff[BUFSIZE];
struct termios oldtio, newtio;
struct timeval tv;
char *dev ="/dev/ttyS0";
fd_set rfds;
if ((fd = open(dev, O_RDWR | O_NOCTTY))0)
{
printf("wait...n");
if (FD_ISSET(fd, &rfds))
{
nread=read(fd, buff, BUFSIZE);
printf("readlength=%dn", nread);
buff[nread]='';
printf("%sn", buff);
}
}
}
tcsetattr(fd, TCSANOW, &oldtio);
close(fd);
}
这是我在网上找的程序,写串口,能够正常运行,但是有个疑问,就是在运行的时候是怎么实现等待的?那个fd_set这里有什么用啊?这里的等待我是否可以写入其他的程序?
谢谢
#include
#include
#include
#include
#include
#include
#include
#include
#define TRUE 1
//初始化串口选项:
void setTermios(struct termios * pNewtio, int uBaudRate)
{
bzero(pNewtio, sizeof(struct termios)); /* clear struct for new port settings */
//8N1
pNewtio->c_cflag = uBaudRate | CS8 | CREAD | CLOCAL;
pNewtio->c_iflag = IGNPAR;
pNewtio->c_oflag = 0;
pNewtio->c_lflag = 0; //non ICANON
/*
initialize all control characters
default values can be found in /usr/include/termios.h, and
are given in the comments, but we don't need them here
*/
pNewtio->c_cc[VINTR] = 0; /* Ctrl-c */
pNewtio->c_cc[VQUIT] = 0; /* Ctrl- */
pNewtio->c_cc[VERASE] = 0; /* del */
pNewtio->c_cc[VKILL] = 0; /* @ */
pNewtio->c_cc[VEOF] = 4; /* Ctrl-d */
pNewtio->c_cc[VTIME] = 5; /* inter-character timer, timeout VTIME*0.1 */
pNewtio->c_cc[VMIN] = 0; /* blocking read until VMIN character arrives */
pNewtio->c_cc[VSWTC] = 0; /* '' */
pNewtio->c_cc[VSTART] = 0; /* Ctrl-q */
pNewtio->c_cc[VSTOP] = 0; /* Ctrl-s */
pNewtio->c_cc[VSUSP] = 0; /* Ctrl-z */
pNewtio->c_cc[VEOL] = 0; /* '' */
pNewtio->c_cc[VREPRINT] = 0; /* Ctrl-r */
pNewtio->c_cc[VDISCARD] = 0; /* Ctrl-u */
pNewtio->c_cc[VWERASE] = 0; /* Ctrl-w */
pNewtio->c_cc[VLNEXT] = 0; /* Ctrl-v */
pNewtio->c_cc[VEOL2] = 0; /* '' */
}
#define BUFSIZE 512
int main(int argc, char **argv)
{
int fd;
int nread;
char buff[BUFSIZE];
struct termios oldtio, newtio;
struct timeval tv;
char *dev ="/dev/ttyS0";
fd_set rfds;
if ((fd = open(dev, O_RDWR | O_NOCTTY))0)
{
printf("wait...n");
if (FD_ISSET(fd, &rfds))
{
nread=read(fd, buff, BUFSIZE);
printf("readlength=%dn", nread);
buff[nread]='';
printf("%sn", buff);
}
}
}
tcsetattr(fd, TCSANOW, &oldtio);
close(fd);
}
这是我在网上找的程序,写串口,能够正常运行,但是有个疑问,就是在运行的时候是怎么实现等待的?那个fd_set这里有什么用啊?这里的等待我是否可以写入其他的程序?
谢谢
|
你还是了解一下 select 模型是如何工作的吧。。。
fd_set 里存放的是你所打开的 串口设备的句柄,
内核会检查串口上是否有数据可读, 有数据就会填充 fd_set, 并唤醒 select,
select 超时返回 或者 被 kernel 唤醒, 接着就去检查 fd_set, 看看是否有数据可读。
fd_set 里存放的是你所打开的 串口设备的句柄,
内核会检查串口上是否有数据可读, 有数据就会填充 fd_set, 并唤醒 select,
select 超时返回 或者 被 kernel 唤醒, 接着就去检查 fd_set, 看看是否有数据可读。