Home -- Michael's Blog :: Just Do IT -- Manuals

9.3. 基本设置

要想在LPD打印池 系统上使用打印机, 你需要 设置打印机硬件和 LPD软件。 这个 文档描述了这两级设置:

9.3.1. 简单打印机设置

这部分讲解怎样配置打印机硬件和 LPD使之与打印机配合。 讲解的基础知识有:

如果你正在设置一台通过网络协议 接收数据来打印而不是通过串行或者并行界面的打印机, 参见使用 网络数据流界面的打印机

尽管这部分叫``简单打印机 设置'', 但还是相当复杂的。 使打印机 配合和LPD 打印池在计算机上正常运转是最难的 部分。 一旦你的打印机可以正常工作后,那些高级选项, 比如报文页和记帐, 是相当简单的。

9.3.1.1. 硬件设置

这部分讲述了打印机连接到计算机的多种 途径。 主要讨论了多种接口和 连接线, 还有允许 FreeBSD 与打印机通讯所需的 内核配置。

如果你已经连接好了你的打印机而且已经 用它在另外一个操作系统下成功的打印, 你 或许可以跳到这个部分软件设置

9.3.1.1.1. 端口和连接电缆

现在所出售的在 PC 上使用的打印机通常至少有 以下三种接口中的一个:

  • 串行接口, 也叫 RS232C, RS232D 或者 COM 口, 使用你计算机上的 串口来发送数据到打印机。 串行 接口在计算机上已经非常普遍, 而且电缆 也非常容易买到且容易制作。 串行 接口有时需要特殊的电缆, 而且可能需要 你去配置稍微有点儿复杂的通讯 选项。 大多数 PC 的串口的最高 传输速度只有 115200 bps, 这使得打印 很大的图像需要的时间很长。

  • 并行界面使用 计算机上的并口来发送数据到 打印机。 并行接口在计算机上也已经非常普遍 而且速度高于 RS232 串行界面。 电缆非常容易买到, 但很难手工 制作。 并行接口通常没有 通讯选项, 这使得 配置它相当简单。

    并口按打印机上的接头来命名也叫做 ``Centronics''接口 。

  • USB 接口, 即通用串行 总线, 可以达到比并口和串口 高很多的速度。 而电缆简单又便宜。 USB 用来打印比串口和并口 更有优势, 但是 UNIX® 系统不能很好的支持它。 避免这个问题的方法就是购买一台 像大多数打印机一样的即有 USB 接口又有并口的 打印机。

一般来说并口只提供 单向通讯( 计算机到打印机), 而串口 和 USB 则可以提供双向通讯。 新的并口( EPP and ECP) 和打印机 在使用了 IEEE1284 标准的电缆之后, 可以在FreeBSD下 双向通讯。

与打印机通过并口双向通讯 通常由这两种方法中的一种来完成。 第一个方法是 使用为 FreeBSD 编写的可以通过打印机使用的语言 与打印机通讯的驱动程序。 这通常 用在喷墨打印机上, 并且可以用来报告 剩余墨水多少和其他状态信息。 第二种方法 使用在支持 PostScript® 的打印机上。

PostScript 任务 事实上由程序发送给打印机; 但它并不进行打印 而是直接将结果返回给计算机。 PostScript 也采取双向通讯来将打印中的问题 报告给计算机, 比如 PostScript 程序中的错误 或者打印机卡纸。 这些信息对于用户来说也许是非常 有价值的。 此外, 最好的在支持 PostScript 的打印机上记帐的方法需要双向 通讯: 询问打印机打印总页数 ( 打印机 从出厂一共打印过多少页 ), 然后发送 用户的任务, 之后再次查询总打印页数。 将打印前后得到 的两个值相减就可以得到该用户要付多少 纸钱。

9.3.1.1.2. 并口

用并口连接打印机需要用 Centronics 电缆把打印机与计算机连接起来。 具体说明指导应该在打印机, 计算机的说明书上有, 或者 干脆两个上面都有。

记住你用的计算机上的哪个并口。 第一个并口在 FreeBSD 上叫 /dev/ppc0 ; 第二个叫 /dev/ppc1, 依此 类推。 打印机设备也用同样的方法命名: /dev/lpt0 是连接在第一个并口 上的打印机, 依此类推。

9.3.1.1.3. 串口

用串口连接打印机需要用 合适的串口电缆把打印机与计算机连接起来。 具体 说明指导应该在打印机, 计算机的说明书上有, 或者 同样干脆两个上面都有。

如果你不确定什么样儿的电缆才是 `` 合适的串口 电缆 '' , 你可以尝试以下几种不同的 电缆:

  • 调制解调器 电缆每一端的 每一根引脚都直接连接到另一端 相应的引脚 上。 这种电缆也叫做 ``DTE-to-DCE'' 电缆。

  • 非调制解调器电缆上每一端的有些引脚 是与另一端相应引脚直接连接的, 而有一些则是交叉连接的 (比如, 发送数据引脚连接到 接收数据引脚 ), 还有一些引脚直接在电缆连接头儿内 短接。 这种电缆也叫做 ``DTE-to-DTE'' 电缆。

  • 一些特殊的打印机需要的串行打印机 电缆, 是一种和非调制解调器电缆类似的电缆, 只是一些信号还是送到了另一端, 而 不是直接在连接头儿内短路。

当然, 你还得为打印机设置通讯参数。 一般是通过打印机面板上的按钮或者 DIP 开关进行设置。 在计算机和打印机上都选择它们所支持的最高 波特 (每秒多少比特, 有时也叫 波特率) 的传输速率。 选择7或者8个数据位; 选择不校验, 偶校验或者奇校验; 选择1个或2个停止位。 还要选择流量 控制协议: 无, XON/XOFF (也叫做 ``in-band'' 或 ``软件'') 流量控制。 记住你的软件配置中的参数也要设成上面的 数值。

9.3.1.2. 软件设置

这部分描述了要使用FreeBSD系统中的 LPD 打印池进行打印所需的软件设置。

包括这几个步骤:

  1. 在需要的时候配置内核来允许你连接 打印机的端口; 配置内核 部分会告诉你 需要做什么。

  2. 如果你使用并口, 则需要设置一下 并口的通讯模式; 设置 并口通讯模式 部分会告诉你具体的 细节。

  3. 测试操作系统是否能够发送数据到打印机。 检测打印机 联机状况 部分会告诉你要怎样 做。

  4. LPD 设置与打印机匹配的参数则 通过修改 /etc/printcap 这个文件来完成。 这章后面 的部分将讲解如何来完成设置。

9.3.1.2.1. 配置内核

操作系统的内核为了使某些特殊设备工作需要重新 编译。 打印机所用的串口、 并口就属于那些特殊设备。 因此, 可能需要 添加对串口或并口的支持, 如果内核并没有配置它们的话。

要想知道你现在使用的内核是否支持串口, 输入:

# grep sioN /var/run/dmesg.boot

其中 N 是串口的 编号, 从0开始。 如果你看到 类似下面的输出:

sio2 at port 0x3e8-0x3ef irq 5 on isa
sio2: type 16550A

则说明你现在使用的内核支持串口。

要想知道你现在使用的内核是否支持并口, 输入:

# grep ppcN /var/run/dmesg.boot

其中 N 是并口的 编号, 同样从0开始。 如果得到类似 下面的输出:

ppc0: <Parallel port> at port 0x378-0x37f irq 7 on isa0
ppc0: SMC-like chipset (ECP/EPP/PS2/NIBBLE) in COMPATIBLE mode
ppc0: FIFO with 16/16/8 bytes threshold

那么你现在使用的内核支持并口。

你可能必须为了使操作系统支持你打印机需要的串口或 并口而 重新配置内核。

要增加对串口的支持, 参见 内核配置这部分。 要增加对并口的支持, 除了参见 上面提到的那部分之外, 还要 参见下面的 部分。

9.3.1.3. 在 /dev 中为端口添加 设备节点

注: FreeBSD 5.0 支持 devfs 这种在需要的时候自动添加设备节点 的文件系统。 如果你正在使用的 FreeBSD 开启了 devfs 文件系统, 那么你可以安全的跳过 这部分。

尽管内核可能已经支持通过串口或者并口 通讯, 但你还是需要一个软件接口 来使你系统上的程序通过它发送和接收 数据。 这也是 /dev 目录中那些设备节点的作用。

要在 /dev 中为端口添加 一个设备节点:

  1. 使用 su(1) 命令切换到 root 帐号。 在出现提示的时候输入 root 帐号的密码。

  2. 将当前目录改变到 /dev 目录:

    # cd /dev
    
  3. 输入:

    # ./MAKEDEV port
    

    其中 port 替换成 你需要添加端口的节点名。 当打印机连接在第一个并口上时, 替换成 lpt0 , 连接在第二个并口上时, 替换成 lpt1 , 依此类推; 当打印机连接在第一个串口上时, 替换成 ttyd0 , 连接在第二个串口上时, 替换成 ttyd1 , 依此类推。

  4. 输入:

    # ls -l port
    

    来检查设备节点是否正常建立。

9.3.1.3.1. 设置并口的通讯 模式

在使用并口时, 你可以选择 让 FreeBSD 用中断方式还是轮询方式来 与打印机通讯。 在 FreeBSD 4.X and 5.X 上, 通用的打印机驱动 (lpt(4)) 使用 ppbus(4) 系统, 它由 ppc(4) 驱动来控制端口芯片。

  • 中断 方式是 GENERIC 核心的默认方式。 在这种方式下, 操作系统占用一条中断请求线来检测什么时候 打印机做好接收数据的准备。

  • 轮询 方式是 操作系统反复不断的询问打印机是否做好 接收数据的准备。 当它返回准备好时, 核心开始发送 下面要发送的数据。

中断方式速度通常会快一些, 但却占用了一条宝贵的中断请求线。 一些新出的 HP 打印机 不能正常的工作在中断模式下, 是由于一些定时问题 (还没正确的理解) 造成的。 这些打印机需要使用轮询方式。 你应该使用 任何一种方式, 只要它能正常工作就行。 一些打印机虽然在两种模式下都可以 工作, 但在中断模式下会慢的要命。

你可以用以下两种方法设定通讯模式: 通过 配置内核或者使用 lptcontrol(8) 这个程序。

要通过配置内核的方法设置 通讯模式:

  1. 修改内核配置文件。 找到 一个叫 ppc0 的记录。 如果你想要设置的是 第二个并口, 那么用 ppc1 代替。 使用第三个并口的时候用 ppc2 代替, 依此类推。

    • 如果你想要使用中断方式, 在 FreeBSD 4.X 下的方法是指定一个 irq

      device ppc0 at isa? irq N
      

      其中 N 是计算机并口 的中断编号。

      在 FreeBSD 5.X 下, 修改下面这一行:

      hint.ppc.0.irq="N"
      

      它在 /boot/device.hints 这个文件中, 其中 N 用正确的中断 编号代替。 同时, 核心配置文件也必须 包括 ppc(4) 的驱动:

      device ppc
      
    • 如果你想要使用轮询方式, 则不用指定 irq

      在 FreeBSD 4.X 下, 在内核配置文件中使用下面 这行:

      device ppc0 at isa?
      

      在 FreeBSD 5.X 下, 只需要把 /boot/device.hints 这个文件中的下面这行 删除掉:

      hint.ppc.0.irq="N"
      

      在 FreeBSD 5.X 下, 有时上面的方法并不能使并口工作在轮询方式。 大多数 情况是由于 acpi(4) 驱动造成的, 它 可以自动侦测到设备并将其挂载到系统上, 但也因此, 它控制着打印机端口的访问模式. 你 得检查 acpi(4) 的配置来 解决这个问题。

  2. 保存文件。 然后配置, 建立, 并安装刚配置的 内核, 最后重新启动。 参见 内核配置 这章来获得 更多细节。

使用 lptcontrol(8) 设置通讯模式

  1. 输入:

    # lptcontrol -i -d /dev/lptN
    

    lptN 设置成中断方式。

  2. 输入:

    # lptcontrol -p -d /dev/lptN
    

    lptN 设置成轮询方式。

你可以把这些命令加入到 /etc/rc.local 这个文件中, 这样每次启动系统 时都会设置成你想要的方式。 参见 lptcontrol(8) 来获得 更多信息。

9.3.1.3.2. 检测打印机的通讯

在设置打印池系统之前, 你 应该确保你的计算机可以把数据 发送到打印机上。 分别独立调试打印机的通讯和打印池系统 会更简单。

我们为了测试打印机,将发送一些文本给它。 一个叫 lptest(1) 的程序能胜任这项工作, 它可以让打印机立即打印出程序发给它的 字符: 它在每行打出 可以打印的 96 个 ASCII 中的一个。

当我们使用的是一台 PostScript ( 或者以其他语言为基础的 ) 打印机, 那么 需要更仔细的检测。 一段小小的 PostScript 程序足以完成检测的任务, 比如下面这段程序:

%!PS
100 100 moveto 300 300 lineto stroke
310 310 moveto /Helvetica findfont 12 scalefont setfont
(Is this thing working?) show
showpage

可以把上面这段 PostScript 代码写进一个文件里, 并且像下面部分的例子里那样 使用。

注: 上面的小程序是基于 PostScript 写成的, 而不是惠普 的 PCL 。 尽管 PCL 拥有强大的功能, 比如 你可以在打印纯文本的同时夹带着特殊的命令。 PostScript 不能直接打印纯文本, 所以 这类打印机语言是我们要特殊照顾的 对象。

9.3.1.3.2.1. 检测并口打印机

这部分内容将指导你怎样检测FreeBSD是否 可以与一台已经连接在并口上的打印机 通讯。

要测试并口上的 打印机:

  1. su(1) 命令转换到 root 用户。

  2. 发送数据到打印机。

    • 如果打印机可以直接打印纯文本, 可以用 lptest(1) 。 输入:

      # lptest > /dev/lptN
      

      其中 N 是并口的 编号, 从0开始。

    • 如果打印机支持 PostScript 或者 其他打印机语言, 可以发送一段小程序到 打印机。 输入:

      # cat > /dev/lptN
      

      然后, 一行一行的 输入 输入 这段程序。 因为在按下 换行 或者 回车 之后, 这一行就不能再修改了。 当你输入完 这段程序之后, 按 CONTROL+D, 或者其他 表示文件结束的键。

      另外一种办法, 你可以把这段程序写在一个文件 里并输入:

      # cat file > /dev/lptN
      

      其中 file 是 包含这你要发给打印机程序的 文件名。

之后, 你应该看到打印出了一些东西。 如果打印出的东西 看起来并不正确, 请不要着急; 我们将在后面指导你怎样 解决这类问题。

9.3.1.3.2.2. 检测串行打印机

这部分将告诉你如何检测FreeBSD是否可以与 连接在串口上的打印机通讯。

要测试一个连接在串口上 的打印机:

  1. 通过 su(1) 命令转换成 root 用户。

  2. 修改 /etc/remote 这个文件。 增加 下面这些内容:

    printer:dv=/dev/port:br#bps-rate:pa=parity
    

    其中 port 是 串口的设备节点 (ttyd0, ttyd1, 等等。 ), bps-rate 是 与打印机通讯时使用的波特, parity 是通讯时打印机要求 的校验方法 (either even, odd, none, or zero)。

    这儿有一个串口打印机的例子, 它连接在第三个串口上, 速度为 19200   波特, 不进行校验:

    printer:dv=/dev/ttyd2:br#19200:pa=none
    
  3. tip(1) 连接打印机。 输入:

    # tip printer
    

    如果没能成功, 则要再次修改 /etc/remote 这个文件, 并且试试用 /dev/cuaaN 代替 /dev/ttydN

  4. 发送数据到打印机。

    • 如果打印机可以直接打印纯文本, 则用 lptest(1) 。 输入:

      % $lptest
      
    • 如果打印机支持 PostScript 或者其他 打印机语言, 则发送一段小程序到 打印机。 一行一行的输入程序, 必须 非常仔细 因为像退格 或者其他编辑键也许对打印机来说有它的 意义。 你同样也需要按一个特殊的 文件结束键, 让打印机知道它已经 接收了整个程序。 对于 PostScript 打印机, 按 CONTROL+D

      或者, 你同样也可以把程序存储在一个文件里 并输入:

      % >file
      

      其中 file 是 包含要发送程序的文件名。 在 tip(1) 发送这个文件之后, 按代表 文件结束的键。

你应该看到打印出了一些东西。 如果它们看起来 并不正确也不要着急; 我们将在以后的部分中解决它。

9.3.1.4. 开启打印池: 文件 /etc/printcap

目前, 你的打印机应该已经连好了线, 系统内核 也为与打印机联机而重新配置好 ( 如果需要的话 ), 而且你也 已经可以发送一些简单的数据到打印机。 现在, 我们要 配置 LPD 来使其控制你的 打印机。

配置 LPD 要修改 /etc/printcap 这个文件。 LPD 打印池系统 在每次打印池被使用的时候都会读取这个文件, 所以对这个文件 的修改会立即生效。

printcap(5) 这个文件的格式很简单。 你可以用你最喜欢的文本编辑器来修改 /etc/printcap 这个文件。 这种格式和其他的 像 /usr/share/misc/termcap/etc/remote 这类文件是一样的。 要得到完整的 关于这种格式的信息, 参见 cgetent(3)

简单的打印池配置包括下面的几 步:

  1. 为打印机起一个名字 ( 和一些方便的别名 ) , 然后把它们写进文件 /etc/printcap ; 参见 Naming the Printer 这章来得到更多的关于起名的帮助。

  2. 关掉报头页 ( 它默认是打开的 ), 通过 增加 sh 项目; 参见 Suppressing Header Pages 部分来得到更多信息。

  3. 建立一个打印池缓冲的目录, 并且通过 sd 项目指定它的位置; 参见 Making the Spooling Directory 部分来得到更多信息。

  4. /dev 下设置打印机设备 节点, 并且在写在 /etc/printcap 文件中 lp 项目里; 参见 识别打印机 设备 这部分得到更多信息。 还有, 如果打印机连接在 串口上, 设置通讯参数要写在 ms# 项目中。 这些参数在 配置打印池 通讯参数 这部分已经讨论过。

  5. 安装纯文本过滤器; 参见 安装文本 过滤器 这章来得到细节。

  6. lpr(1) 命令来测试设置。 想得到更多信息可以参见 测试 and 疑难问题解答 部分。

注: 基于打印机语言的打印机, 比如 PostScript 打印机, 不能直接打印纯文本。 简单的设置方法在上面 提到并且下面的部分也要讨论, 假如你正在 安装这样一台只能打印它明白的文件的 打印机。

用户总是希望他们能在任何一个安装在他们系统的打印机上打印 纯文本。 使用 LPD 接口的程序也通常这样 希望。 如果你正在安装这样一台打印机, 并且想要它能打印 基于它支持的打印机语言的任务 并且 还可以打印纯文本的任务, 那么强烈建议你在上面提到 的简单设置的步骤上增加一步: 安装一个 自动纯文本到 PostScript ( 或者其他打印机语言 ) 的 转化程序。 参见 PostScript 打印机上打印纯文本 将得到 更多有关信息。

9.3.1.4.1. Naming the Printer

The first (easy) step is to pick a name for your printer It really does not matter whether you choose functional or whimsical names since you can also provide a number of aliases for the printer.

At least one of the printers specified in the /etc/printcap should have the alias lp. This is the default printer's name. If users do not have the PRINTER environment variable nor specify a printer name on the command line of any of the LPD commands, then lp will be the default printer they get to use.

Also, it is common practice to make the last alias for a printer be a full description of the printer, including make and model.

Once you have picked a name and some common aliases, put them in the /etc/printcap file. The name of the printer should start in the leftmost column. Separate each alias with a vertical bar and put a colon after the last alias.

In the following example, we start with a skeletal /etc/printcap that defines two printers (a Diablo 630 line printer and a Panasonic KX-P4455 PostScript laser printer):

#
#  /etc/printcap for host rose
#
rattan|line|diablo|lp|Diablo 630 Line Printer:

bamboo|ps|PS|S|panasonic|Panasonic KX-P4455 PostScript v51.4:

In this example, the first printer is named rattan and has as aliases line, diablo, lp, and Diablo 630 Line Printer. Since it has the alias lp, it is also the default printer. The second is named bamboo, and has as aliases ps, PS, S, panasonic, and Panasonic KX-P4455 PostScript v51.4.

9.3.1.4.2. Suppressing Header Pages

The LPD spooling system will by default print a header page for each job. The header page contains the user name who requested the job, the host from which the job came, and the name of the job, in nice large letters. Unfortunately, all this extra text gets in the way of debugging the simple printer setup, so we will suppress header pages.

To suppress header pages, add the sh capability to the entry for the printer in /etc/printcap. Here is an example /etc/printcap with sh added:

#
#  /etc/printcap for host rose - no header pages anywhere
#
rattan|line|diablo|lp|Diablo 630 Line Printer:\
        :sh:

bamboo|ps|PS|S|panasonic|Panasonic KX-P4455 PostScript v51.4:\
        :sh:

Note how we used the correct format: the first line starts in the leftmost column, and subsequent lines are indented with a single TAB. Every line in an entry except the last ends in a backslash character.

9.3.1.4.3. Making the Spooling Directory

The next step in the simple spooler setup is to make a spooling directory, a directory where print jobs reside until they are printed, and where a number of other spooler support files live.

Because of the variable nature of spooling directories, it is customary to put these directories under /var/spool. It is not necessary to backup the contents of spooling directories, either. Recreating them is as simple as running mkdir(1).

It is also customary to make the directory with a name that is identical to the name of the printer, as shown below:

# mkdir /var/spool/printer-name

However, if you have a lot of printers on your network, you might want to put the spooling directories under a single directory that you reserve just for printing with LPD. We will do this for our two example printers rattan and bamboo:

# mkdir /var/spool/lpd
# mkdir /var/spool/lpd/rattan
# mkdir /var/spool/lpd/bamboo

注: If you are concerned about the privacy of jobs that users print, you might want to protect the spooling directory so it is not publicly accessible. Spooling directories should be owned and be readable, writable, and searchable by user daemon and group daemon, and no one else. We will do this for our example printers:

# chown daemon:daemon /var/spool/lpd/rattan
# chown daemon:daemon /var/spool/lpd/bamboo
# chmod 770 /var/spool/lpd/rattan
# chmod 770 /var/spool/lpd/bamboo

Finally, you need to tell LPD about these directories using the /etc/printcap file. You specify the pathname of the spooling directory with the sd capability:

#
#  /etc/printcap for host rose - added spooling directories
#
rattan|line|diablo|lp|Diablo 630 Line Printer:\
        :sh:sd=/var/spool/lpd/rattan:

bamboo|ps|PS|S|panasonic|Panasonic KX-P4455 PostScript v51.4:\
        :sh:sd=/var/spool/lpd/bamboo:

Note that the name of the printer starts in the first column but all other entries describing the printer should be indented with a tab and each line escaped with a backslash.

If you do not specify a spooling directory with sd, the spooling system will use /var/spool/lpd as a default.

9.3.1.4.4. Identifying the Printer Device

In the Adding /dev Entries for the Ports section, we identified which entry in the /dev directory FreeBSD will use to communicate with the printer. Now, we tell LPD that information. When the spooling system has a job to print, it will open the specified device on behalf of the filter program (which is responsible for passing data to the printer).

List the /dev entry pathname in the /etc/printcap file using the lp capability.

In our running example, let us assume that rattan is on the first parallel port, and bamboo is on a sixth serial port; here are the additions to /etc/printcap:

#
#  /etc/printcap for host rose - identified what devices to use
#
rattan|line|diablo|lp|Diablo 630 Line Printer:\
        :sh:sd=/var/spool/lpd/rattan:\
        :lp=/dev/lpt0:

bamboo|ps|PS|S|panasonic|Panasonic KX-P4455 PostScript v51.4:\
        :sh:sd=/var/spool/lpd/bamboo:\
        :lp=/dev/ttyd5:

If you do not specify the lp capability for a printer in your /etc/printcap file, LPD uses /dev/lp as a default. /dev/lp currently does not exist in FreeBSD.

If the printer you are installing is connected to a parallel port, skip to the section entitled, Installing the Text Filter. Otherwise, be sure to follow the instructions in the next section.

9.3.1.4.5. Configuring Spooler Communication Parameters

For printers on serial ports, LPD can set up the bps rate, parity, and other serial communication parameters on behalf of the filter program that sends data to the printer. This is advantageous since:

  • It lets you try different communication parameters by simply editing the /etc/printcap file; you do not have to recompile the filter program.

  • It enables the spooling system to use the same filter program for multiple printers which may have different serial communication settings.

The following /etc/printcap capabilities control serial communication parameters of the device listed in the lp capability:

br#bps-rate

Sets the communications speed of the device to bps-rate, where bps-rate can be 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, or 115200 bits-per-second.

ms#stty-mode

Sets the options for the terminal device after opening the device. stty(1) explains the available options.

When LPD opens the device specified by the lp capability, it sets the characteristics of the device to those specified with the ms# capability. Of particular interest will be the parenb, parodd, cs5, cs6, cs7, cs8, cstopb, crtscts, and ixon modes, which are explained in the stty(1) manual page.

Let us add to our example printer on the sixth serial port. We will set the bps rate to 38400. For the mode, we will set no parity with -parenb, 8-bit characters with cs8, no modem control with clocal and hardware flow control with crtscts:

bamboo|ps|PS|S|panasonic|Panasonic KX-P4455 PostScript v51.4:\
        :sh:sd=/var/spool/lpd/bamboo:\
        :lp=/dev/ttyd5:ms#-parenb cs8 clocal crtscts:

9.3.1.4.6. Installing the Text Filter

We are now ready to tell LPD what text filter to use to send jobs to the printer. A text filter, also known as an input filter, is a program that LPD runs when it has a job to print. When LPD runs the text filter for a printer, it sets the filter's standard input to the job to print, and its standard output to the printer device specified with the lp capability. The filter is expected to read the job from standard input, perform any necessary translation for the printer, and write the results to standard output, which will get printed. For more information on the text filter, see the Filters section.

For our simple printer setup, the text filter can be a small shell script that just executes /bin/cat to send the job to the printer. FreeBSD comes with another filter called lpf that handles backspacing and underlining for printers that might not deal with such character streams well. And, of course, you can use any other filter program you want. The filter lpf is described in detail in section entitled lpf: a Text Filter.

First, let us make the shell script /usr/local/libexec/if-simple be a simple text filter. Put the following text into that file with your favorite text editor:

#!/bin/sh
#
# if-simple - Simple text input filter for lpd
# Installed in /usr/local/libexec/if-simple
#
# Simply copies stdin to stdout.  Ignores all filter arguments.

/bin/cat && exit 0
exit 2

Make the file executable:

# chmod 555 /usr/local/libexec/if-simple

And then tell LPD to use it by specifying it with the if capability in /etc/printcap. We will add it to the two printers we have so far in the example /etc/printcap:

#
#  /etc/printcap for host rose - added text filter
#
rattan|line|diablo|lp|Diablo 630 Line Printer:\
        :sh:sd=/var/spool/lpd/rattan:\ :lp=/dev/lpt0:\
        :if=/usr/local/libexec/if-simple:

bamboo|ps|PS|S|panasonic|Panasonic KX-P4455 PostScript v51.4:\
        :sh:sd=/var/spool/lpd/bamboo:\
        :lp=/dev/ttyd5:ms#-parenb cs8 clocal crtscts:\
        :if=/usr/local/libexec/if-simple:

注: A copy of the if-simple script can be found in the /usr/share/examples/printing directory.

9.3.1.4.7. Turn on LPD

lpd(8) is run from /etc/rc, controlled by the lpd_enable variable. This variable defaults to NO. If you have not done so already, add the line:

lpd_enable="YES"

to /etc/rc.conf, and then either restart your machine, or just run lpd(8).

# lpd

9.3.1.4.8. Trying It Out

You have reached the end of the simple LPD setup. Unfortunately, congratulations are not quite yet in order, since we still have to test the setup and correct any problems. To test the setup, try printing something. To print with the LPD system, you use the command lpr(1), which submits a job for printing.

You can combine lpr(1) with the lptest(1) program, introduced in section Checking Printer Communications to generate some test text.

To test the simple LPD setup:

Type:

# lptest 20 5 | lpr -Pprinter-name

Where printer-name is a the name of a printer (or an alias) specified in /etc/printcap. To test the default printer, type lpr(1) without any -P argument. Again, if you are testing a printer that expects PostScript, send a PostScript program in that language instead of using lptest(1). You can do so by putting the program in a file and typing lpr file.

For a PostScript printer, you should get the results of the program. If you are using lptest(1), then your results should look like the following:

!"#$%&'()*+,-./01234
"#$%&'()*+,-./012345
#$%&'()*+,-./0123456
$%&'()*+,-./01234567
%&'()*+,-./012345678

To further test the printer, try downloading larger programs (for language-based printers) or running lptest(1) with different arguments. For example, lptest 80 60 will produce 60 lines of 80 characters each.

If the printer did not work, see the Troubleshooting section.

Michael's Blog :: Just Do IT -- manuals