蜂鳴器的控制linux

weixin_34124651發表於2018-07-11

蜂鳴器的控制linux

1裝置的節點

8192135-d7df9698f02ade69.png
image.png

2控制方法,輸入1,蜂鳴器響,輸入0蜂鳴器停止

3控制程式碼

#include <stdio.h>



//檔案操作函式標頭檔案
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#define BUZZ_CMD  2

/*
cmd:0--->buzz off 1--->buzz on

*/
int main(int argc ,char **argv)
{
    
    int fd,buzzer_cmd;
    char *buzz_path="/dev/buzzer_ctl";
    printf("\n argv1 is cmd,argv2 is io\n");
    printf("\ncmd:0--->buzz off 1--->buzz on\n");
    printf("app name:%s\n",argv[0]);
    buzzer_cmd = atoi(argv[1]);

    if(buzzer_cmd>BUZZ_CMD)
    {
    printf("para1 buzz cmd err \n");
    exit(1);      
    }

    if((fd = open(buzz_path,O_RDWR|O_NOCTTY|O_NDELAY))<0)
    {
        
      printf("open path %s  err \n",buzz_path);
      exit(1);       
    }
        
    if((ioctl(fd,buzzer_cmd,0))<0)
    {
         printf("buzz ioctl err \n");
        
    }else
    {
        
         printf("buzz ioctl success \n");
    }
    
    close(fd);
    return 0;
    
}

相關文章