首页 | 资讯动态 | linux基础 | 系统管理 | 网络管理 | 编程开发 | linux数据库 | 服务器技术 | linux相关 | linux认证 | 嵌入式 | 下载中心 | 专题 | linux招聘 | 镜像站
OKLinux中文技术站
·设为首页
·加入收藏
·联系我们
系统管理: 中文环境 系统管理 桌面应用 内核技术 | Linux基础: 基础入门 安装配置 常用命令 经验技巧 软件应用 | Linux数据库: Mysql Postgre Oracle DB2 Sybase other
网络管理: 网络安全 网络应用 Linux服务器 环境配置 黑客安全 | 编程开发: PHP CC++ Python Perl Shell 嵌入式开发 java jsp | PHP技术: PHP基础 PHP技巧 PHP应用 PHP文摘
Linux资讯 Linux招聘 Linux专题 Apache | Linux相关: 硬件相关 Linux解决方案 Linux认证 企业应用 其它Unix | 相关下载: 资料下载 参考手册 开发工具 服务器类 软路由 其它
 技术搜索:
会员中心 注册会员 高级搜索  
  → 当前位置:首页>编程开发>cc++>正文

解析Linux下的并口编程(英文版)

http://www.oklinux.cn  2007-02-09  来源: oklinux收集整理   会员收藏  游客收藏  【 】 


  The printer is accessible through /dev/lp0; in the same way, the parallel port itself is accessible through /dev/parport0. The difference is in the level of control that you have over the wires in the parallel port cable.
  
  With the printer driver, a user-space program (such as the printer spooler) can send bytes in "printer protocol". Briefly, this means that for each byte, the eight data lines are set up, then a "strobe" line tells the printer to look at the data lines, and the printer sets an "acknowledgement" line to say that it got the byte. The printer driver also allows the user-space program to read bytes in "nibble mode", which is a way of transferring data from the peripheral to the computer half a byte at a time (and so it's quite slow).
  
  In contrast, the ppdev driver (accessed via /dev/parport0) allows you to:
  
  examine status lines,
  
  set control lines,
  
  set/examine data lines (and control the direction of the data lines),
  
  wait for an interrupt (triggered by one of the status lines),
  
  find out how many new interrupts have occurred,
  
  set up a response to an interrupt,
  
  use IEEE 1284 negotiation (for telling peripheral which transfer mode, to use)
  
  transfer data using a specified IEEE 1284 mode.
  
  Programming interface
  
  The ppdev interface is largely the same as that of other character special devices, in that it supports open, close, read, write, and ioctl. The constants for the ioctl commands are in include/linux/ppdev.h.
  
  Starting and stopping: open and close
  
  The device node /dev/parport0 represents any device that is connected to parport0, the first parallel port in the system. Each time the device node is opened, it represents (to the process doing the opening) a different device. It can be opened more than once, but only one instance can actually be in control of the parallel port at any time. A process that has opened /dev/parport0 shares the parallel port in the same way as any other device driver. A user-land driver may be sharing the parallel port with in-kernel device drivers as well as other user-land drivers.
  
  Control: ioctl
  
  Most of the control is done, naturally enough, via the ioctl call. Using ioctl, the user-land driver can control both the ppdev driver in the kernel and the physical parallel port itself. The ioctl call takes as parameters a file descriptor (the one returned from opening the device node), a command, and optionally (a pointer to) some data.
  
  PPCLAIM
  
  Claims access to the port. As a user-land device driver writer, you will need to do this before you are able to actually change the state of the parallel port in any way. Note that some operations only affect the ppdev driver and not the port, such as PPSETMODE; they can be performed while access to the port is not claimed.
  
  PPEXCL
  
  Instructs the kernel driver to forbid any sharing of the port with other drivers, i.e. it requests exclusivity. The PPEXCL command is only valid when the port is not already claimed for use, and it may mean that the next PPCLAIM ioctl will fail: some other driver may already have registered itself on that port.
  
  Most device drivers don't need exclusive access to the port. It's only provided in case it is really needed, for example for devices where access to the port is required for extensive periods of time (many seconds).
  
  Note that the PPEXCL ioctl doesn't actually claim the port there and then---action is deferred until the PPCLAIM ioctl is performed.
  
  PPRELEASE
  
  Releases the port. Releasing the port undoes the effect of claiming the port. It allows other device drivers to talk to their devices (assuming that there are any).
  
  PPYIELD
  
  Yields the port to another driver. This ioctl is a kind of short-hand for releasing the port and immediately reclaiming it. It gives other drivers a chance to talk to their devices, but afterwards claims the port back. An example of using this would be in a user-land printer driver: once a few characters have been written we could give the port to another device driver for a while, but if we still have characters to send to the printer we would want the port back as soon as possible.
  
  It is important not to claim the parallel port for too long, as other device drivers will have no time to service their devices. If your device does not allow for parallel port sharing at all, it is better to claim the parallel port exclusively (see PPEXCL).
  
  PPNEGOT
  
  Performs IEEE 1284 negotiation into a particular mode. Briefly, negotiation is the method by which the host and the peripheral decide on a protocol to use when transferring data.
  
  An IEEE 1284 compliant device will start out in compatibility mode, and then the host can negotiate to another mode (such as ECP).
  
  The ioctl parameter should be a pointer to an int; values for this are in incluce/linux/parport.h and include:
  
  IEEE1284_MODE_COMPAT
  
  IEEE1284_MODE_NIBBLE
  
  IEEE1284_MODE_BYTE
  
  IEEE1284_MODE_EPP
  
  IEEE1284_MODE_ECP
  
  The PPNEGOT ioctl actually does two things: it performs the on-the-wire negotiation, and it sets the behaviour of subsequent read/write calls so that they use that mode (but see PPSETMODE).
  
  PPSETMODE
  
  Sets which IEEE 1284 protocol to use for the read and write calls.
  
  The ioctl parameter should be a pointer to an int.
  
  PPGETMODE
  
  Retrieves the current IEEE 1284 mode to use for read and write.
  
  PPGETTIME
  
  Retrieves the time-out value. The read and write calls will time out if the peripheral doesn't respond quickly enough. The PPGETTIME ioctl retrieves the length of time that the peripheral is allowed to have before giving up.
  
  The ioctl parameter should be a pointer to a struct timeval.
  
  PPSETTIME
  
  Sets the time-out. The ioctl parameter should be a pointer to a struct timeval.
  
  PPGETMODES
  
  Retrieves the capabilities of the hardware (i.e. the modes field of the parport structure).
  
  PPSETFLAGS
  
  Sets flags on the ppdev device which can affect future I/O operations. Available flags are:
  
  PP_FASTWRITE
  
  PP_FASTREAD
  
  PP_W91284PIC
  
  PPWCONTROL
  
  Sets the control lines. The ioctl parameter is a pointer to an unsigned char, the bitwise OR of the control line values in include/linux/parport.h.
  
  PPRCONTROL
  
  Returns the last value written to the control register, in the form of an unsigned char: each bit corresponds to a control line (although some are unused). The ioctl parameter should be a pointer to an unsigned char.
  
  This doesn't actually touch the hardware; the last value written is remembered in software. This is because some parallel port hardware does not offer read access to the control register.
  
  The control lines bits are defined in include/linux/parport.h:
  
  PARPORT_CONTROL_STROBE
  
  PARPORT_CONTROL_AUTOFD
  
  PARPORT_CONTROL_SELECT
  
  PARPORT_CONTROL_INIT
  
  PPFCONTROL
  
  Frobs the control lines. Since a common operation is to change one of the control signals while leaving the others alone, it would be quite inefficient for the user-land driver to have to use PPRCONTROL, make the change, and then use PPWCONTROL. Of course, ea




上一篇:SourceInsight3.0:Linux源代码阅读   下一篇:C++中关于指针入门的最好的文章


收藏于收藏夹】 【评论】 【推荐】 【打印】 【关闭
相关文档
·C++中关于指针入门的最好的文章
·SourceInsight3.0:Linux源代码阅读
·新手学习之浅析一下c/c++中的指针
·SMS中用Unicode编码发送中文的办法
·关于C++代码优化的方法总结(一)
·Linux下C++异常处理技巧-实例讲解
·轻松就能让Linux下的C编程从头来
·GNUC库----调试系统问题并进行修订
·C++常用字符串处理函数及使用示例
·用C与脚本的混合编程来处理配置文件
·使用profile来得到程序运行信息
·Linux下C语言编程--时间概念
·在CMP实体BEAN中使用BLOB数据类型
·linux下命令行下编译c程序
·在C/C++中如何构造通用的对象链表
·GCC内嵌汇编之语法详解
发表评论
密码: 匿名评论
评论内容:

(不超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)
 
  最新文档
·用Eclipse平台进行C/C 开发
·在 Linux 中使用共享对象
·VS:针对Java开发人员的C#编程语言
·使用智能设备扩展在C#中开发自定义控件
·Visual C# 常见问题
·二级C语言实例解答
·一种被忽视的构造和整数溢出重现
·轻轻松松C to C
·与用于 C 的ISO标准保持一致
·用C 的托管扩展针对Windows编程
·运行时和编译时的安全性检查
·轻轻松松C to C (二)
  阅读排行
·c/c++ 学习-read 函数和 write 函数
·程序员眼中的qmail(qmail源代码分析)
·Awk 基础入门:Awk 实例编程
·autoconf 和automake生成Makefile文件
·Linux下的多进程编程
·使用 GDB 调试多进程程序
·入门文章:教你学会编写Linux设备驱动
·C++自动化(模板元)编程基础与应用
·Qt 不规则窗体的实现
·嵌入式程序员应知道的几个基本问题
·Linux操作系统中GCC的应用介绍
·C语言中的指针和内存泄漏
·用GNU profiler提高代码运行速度
·vi 中的正则表达式 (Regular Expressio
·Linux 套接字编程中的 5 个隐患
网摘收藏: