1.6.1 有名管道附属

(这是友善之臂的代码和备注。希望初学者有用~~~~~~~~)

有名管道附属

一.简介

本列程是由友善之臂说明书中的管道列程的备注版。有助于大家了解select函数以及程序的实现方式。

二.重点掌握函数:

        select; struct timeval结构体;  FD_ZERO; FD_SET;  

三.要求水平

可使用内来进行进程通信。

#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <string.h>

static int led_fd;
static int type = 1;//貌似模式选择

static void push_leds(void)
{
	static unsigned step;       //静态变量
	unsigned led_bitmap;
	int i;

	switch(type) {
	case 0:     //流水灯
		if (step >= 6) {
			step = 0;
		}
		if (step < 3) {
			led_bitmap = 1 << step;
		} else {
			led_bitmap = 1 << (6 - step);
		}
		break;
	case 1:         //级数灯
		if (step > 255) {
			step = 0;
		}
		led_bitmap = step;
		break;
	default:
		led_bitmap = 0;
	}
	step++;
	for (i = 0; i < 4; i++) {
		ioctl(led_fd, led_bitmap & 1, i);//最后输出到led,led_bitmap第一位与一相与
		led_bitmap >>= 1;
	}
}

int main(void)
{
	int led_control_pipe;
	int null_writer_fd; // for read endpoint not blocking when control process exit

	double period = 0.5;        //貌似周期

	led_fd = open("/dev/leds0", 0);
	if (led_fd < 0) {
		led_fd = open("/dev/leds", 0);
	}
	if (led_fd < 0) {
		perror("open device leds");
		exit(1);
	}
	unlink("/tmp/led-control");        //清空名字为"/tmp/led-control"的文件
	mkfifo("/tmp/led-control", 0666);  //创建管道

	led_control_pipe = open("/tmp/led-control", O_RDONLY | O_NONBLOCK);//只读 非阻塞
	if (led_control_pipe < 0) {
		perror("open control pipe for read");
		exit(1);
	}
	null_writer_fd = open("/tmp/led-control", O_WRONLY | O_NONBLOCK);//只写 非阻塞
	if (null_writer_fd < 0) {
		perror("open control pipe for write");
		exit(1);
	}

	for (;;) {
		fd_set rds;                 //一组文件描述字(fd)的集合
		struct timeval step;        //关于时间的结构体,内部有秒和微秒,1M的微秒等于一秒
		int ret;

		FD_ZERO(&rds);              //将指定的文件描述符集清空
		FD_SET(led_control_pipe, &rds); //文件描述符集合中增加一个新的文件描述符
                                        //这里未用:FD_ISSET(int fd,fd_set *fdset);用于测试指定的文件描述符是否在该集合中
		step.tv_sec  = period;
		step.tv_usec = (period - step.tv_sec) * 1000000L;// 时间设置判断rds中是否有可读文件,判断时间由step决定,第一个NULL为“写”,第二个为“错误
                            ret = select(led_control_pipe + 1, &rds, NULL, NULL, &step);
                                                                    //该死的程序员,竟然还拿着个函数当延时用,看了好长时间才理解
		if (ret < 0) {
			perror("select");
			exit(1);
		}
		if (ret == 0) {
			push_leds();                //如果不可读
		} else if (FD_ISSET(led_control_pipe, &rds)) {  //如果可读
			static char buffer[200];
			for (;;) {
				char c;
				int len = strlen(buffer);//貌似是读字符串的长度(字节数),而且是						                                     //\0前的长度,
				if (len >= sizeof buffer - 1) {//sizeof是数分配内存的字符数判断							                                    //buffer是否满
					memset(buffer, 0, sizeof buffer);   //清空字符串
					break;
				}
				if (read(led_control_pipe, &c, 1) != 1) {   //读管道的的数据一个字节
					break;
				}
				if (c == '\r') {
					continue;                               //跳出本次循环
				}
				if (c == '\n') {
					int tmp_type;
					double tmp_period;
					if (sscanf(buffer,"%d%lf", &tmp_type, &tmp_period) == 2) {
                                         //将buffer中的整形放入tmp_type,浮点型放入tmp_period 成功返回成功数目
						type = tmp_type;
						period = tmp_period;
					}
					fprintf(stderr, "type is %d, period is %lf\n", type, period);
					memset(buffer, 0, sizeof buffer);
					break;
				}
				buffer[len] = c;    //将管道中的数据装入buffer中
			}
		}
	}

	close(led_fd);
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值