SiioLaboratory

Linuxでパラレルポート(プリンタポート)を読み書きする

Linuxで、ppdevを利用して、 パラレルポート(プリンタポート)(/dev/parport0) を読み書きする方法のメモ。

参考ホームページ

/dev/parport0

/dev/lp0はハンドシェークしながらパラレルポートにデータを出すのに対して、 /dev/parport0を利用するとパラレルポートのピンを直接読み書きすることができる。

通常, /dev/parport0は一般ユーザが読み書きできない設定なので、

su
chmod a+rw /dev/parport0

などして読み書き可能にしておく。

ポートを読み書きするサンプル

以下のプログラムで、 データラインのピンを順番に1にして、 つぎにステータスピン(ackとかbusyなどのピン)の値を読んでいる。 (逆スラッシュが?に文字化けしているので注意)

#include <stdio.h>

#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/ppdev.h>
#include <linux/parport.h>

main()
{
int fd;
unsigned char byte;
int i, rc, count;

//open the printer port
fd = open("/dev/parport0", O_RDWR);
       if(fd==-1) {
               printf("open error?n");
               return 1;
       }
if(ioctl(fd,PPCLAIM)) {
       printf("PPCLAIM error?n");
       close(fd);
       return 1;
}

//write a byte to data pins
for(i=1;i<256;i=i<<1) {
       usleep(100000);
       ioctl(fd, PPWDATA, &i);
}

//read the status pins
       ioctl(fd, PPRSTATUS, &byte);
       printf("status is %2x?n", byte);

//close the printer port
       rc=ioctl(fd, PPRELEASE);
       close(fd);
}

解説

http://people.redhat.com/twaugh/parport/html/x623.html


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2020-11-18 (水) 20:52:08