资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言中ioctl函数 c语言标准io函数

请教c语言ioctl函数的用法.急...

这个函数本身没什么特别的, 但是参数的使用跟具体设备和驱动有关系, 所以其实没法解释这个东西具体该怎么用, 你要操作什么设备就得仔细去读这个设备驱动的文档

专注于为中小企业提供网站制作、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业马山免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

ioctl()函数的参数和作用

因为用户层定义它是个变参函数

ioctl (int __fd, unsigned long int __request, ...)

跟printf似的

linux下怎么用c获取硬盘物理序列号

1、在Linux系统中通过C语言获取硬盘序列号,可以借助于ioctl()函数,该函数原型如下:

int ioctl(int fd, unsigned long request, ...);

ioctl的第一个参数是文件标识符,用open()函数打开设备时获取。

ioctl第二个参数为用于获得指定文件描述符的标志号,获取硬盘序列号,一般指明为HDIO_GET_IDENTITY。

ioctl的第三个参数为一些辅助参数,要获取硬盘序列号,需要借助于struct hd_driveid结构体来保存硬盘信息 ,该结构体在Linux/hdreg.h中,struct hd_driveid的声明如下

struct hd_driveid {

unsigned short    config;        / lots of obsolete bit flags */

unsigned short    cyls;        /* Obsolete, "physical" cyls */

unsigned short    reserved2;    /* reserved (word 2) */

unsigned short    heads;        /* Obsolete, "physical" heads */

unsigned short    track_bytes;    /* unformatted bytes per track */

unsigned short    sector_bytes;    /* unformatted bytes per sector */

unsigned short    sectors;    /* Obsolete, "physical" sectors per track */

unsigned short    vendor0;    /* vendor unique */

unsigned short    vendor1;    /* vendor unique */

unsigned short    vendor2;    /* Retired vendor unique */

unsigned char    serial_no[20];    /* 0 = not_specified */

unsigned short    buf_type;    /* Retired */

unsigned short    buf_size;    /* Retired, 512 byte increments

* 0 = not_specified

*/

……

};

2、源代码如下

#include stdio.h

//ioctl()的声明头文件

#include sys/ioctl.h

//硬盘参数头文件, hd_driveid结构声明头文件

#include linux/hdreg.h

//文件控制头文件

#include sys/fcntl.h

int main()

{

//用于保存系统返回的硬盘数据信息

struct hd_driveid id;

//这里以第一块硬盘为例,用户可自行修改

//用open函数打开获取文件标识符,类似于windows下的句柄

int fd = open("/dev/sda", O_RDONLY|O_NONBLOCK);

//失败返回 

if (fd  0) {

perror("/dev/sda");

return 1; }

//调用ioctl()

if(!ioctl(fd, HDIO_GET_IDENTITY, id))

{

printf("Serial Number=%s\n",id.serial_no);

}

return 0;

}

编译完成后,执行效果如下:

请问程序中ioctl()函数的功能是什么

使用i2c通信先配置地址,数据位,超时时间等等通信规则。

这里的ioctl函数就是用来配置这些参数的。

I2C_SLAVE:安全的配置,如果i2c已经配置过会返回失败。

I2C_SLAVE_FORCE:总是成功,不管其他人有没有在使用,确定只有你使用这个i2c的时候使用。

同时ioctl还能使用I2C_TENBIT,I2C_PEC,I2C_TIMEOUT等参数。

这里只是配置i2c通信规则,还没有发送数据。

配置完成后,调用write,read函数可以接收发送数据。

计算机网络编程,关于ioctl函数,求大神指教。

下面的清单介绍了一些最重要的结构,使用 ioctl 套接字命令时常常用到这些结构。

清单 1. struct ifreq (/usr/include/net/if.h)

/* Interface request structure used for socket

* ioctl's. All interface ioctl's must have parameter

* definitions which begin with ifr_name. The

* remainder may be interface specific.

*/

struct ifreq {

#ifndef IFNAMSIZ

#define IFNAMSIZ 16

#endif

char ifr_name[IFNAMSIZ];

union {

struct sockaddr ifru_addr;

struct sockaddr ifru_dstaddr;

struct sockaddr ifru_broadaddr;

__ulong32_t ifru_flags;

int ifru_metric;

caddr_t ifru_data;

u_short ifru_site6;

__ulong32_t ifru_mtu;

int ifru_baudrate;

} ifr_ifru;

Following macros are provided for convenience

#define ifr_addr ifr_ifru.ifru_addr /* address */

#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */

#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */

#define ifr_flags ifr_ifru.ifru_flags /* flags */

#define ifr_metric ifr_ifru.ifru_metric /* metric */

#define ifr_data ifr_ifru.ifru_data /* for use by interface */

#define ifr_site6 ifr_ifru.ifru_site6 /* IPv6 site index */

#define ifr_mtu ifr_ifru.ifru_mtu /* mtu of interface */

#define ifr_isno ifr_ifru.ifru_data /* pointer to if_netopts */

#define ifr_baudrate ifr_ifru.ifru_baudrate /* baudrate of interface */

};

清单 2. struct ifconf (/usr/include/net/if.h)

/*

* Structure used in SIOCGIFCONF request.

* Used to retrieve interface configuration

* for machine (useful for programs which

* must know all networks accessible).

*/

struct ifconf {

int ifc_len; /* size of associated buffer */

union {

caddr_t ifcu_buf;

struct ifreq *ifcu_req;

} ifc_ifcu;

Following macros are provided for convenience

#define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */

#define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */

};

可以去百度上搜下编程回忆录之Unity3D,他们有公开课,你们看看就知道,我感觉非常的好。


网页标题:c语言中ioctl函数 c语言标准io函数
转载源于:http://cdkjz.cn/article/docpspj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220