之前有视频,有大牛用光驱带动婴儿床实现自动摇摆,这下自己也会了,特此留恋一下,算是给自己献宝吧!
<pre name="code" class="cpp">#include <linux/cdrom.h>
#include <stdio.h>
#include <fcntl.h>
int main(void){
int fd = open("/dev/cdrom",O_RDONLY|O_NONBLOCK);
if(fd < 0){
fprintf(stdout,"Open CDROM failed.\n");
return -1;
}
while(1){
// send CDROMEJECT request to linux kernerl
if(!ioctl(fd,CDROMEJECT,NULL)){
fprintf(stdout,"Eject successfully.\n");
} else {
fprintf(stdout,"Eject failed.\n");
}
sleep(1);
// send Close request to linux kernerl
if(!ioctl(fd,CDROMCLOSETRAY,NULL)){
fprintf(stdout,"Close successfully.\n");
} else {
fprintf(stdout,"Close failed.\n");
}
sleep(2);
}
close(fd);
return 0;
}
<pre name="code" class="cpp">CDROMEJECT、CDROMCLOSETRAY等 宏定义在cdrom.h的头文件中。