Senden und Empfangen via Parallelport
|
Dez Bin Taster Pin Funktion Hinweis 120 01111000 keiner 112 01110000 S3 15 ERROR 104 01101000 S4 13 SELECT 88 01011000 S5 12 PAPER-END 56 00111000 S6 10 ACK 248 11111000 S7 11 BUSY invertierend
Code 0-1: Ergebnis bei Register 0x379 beim Testen von Tastern, die an den 5 Eingängen des Parallelports angeschlossen sind.
|
#include<stdio.h> #include<sys/io.h> int lese() { ioperm(0x379, 1, 1); return inb(0x379); } int main(int anz,char** args) { int zahl_alt=0; int zahl=0; int z; while(1) { zahl = lese(); if(zahl!=zahl_alt) { zahl_alt = zahl; printf("%4i ",(int)zahl); z=zahl; if(z>=128) printf("1"); else printf("0"); z%=128; if(z>=64) printf("1"); else printf("0"); z%=64; if(z>=32) printf("1"); else printf("0"); z%=32; if(z>=16) printf("1"); else printf("0"); z%=16; if(z>=8) printf("1"); else printf("0"); z%=8; if(z>=4) printf("1"); else printf("0"); z%=4; if(z>=2) printf("1"); else printf("0"); z%=2; if(z>=1) printf("1"); else printf("0"); z%=1; printf("\n"); } } }
Code 0-2: Quellcode lesemuster.c, Kompilieren mit: gcc -O2 -o lesemuster lesemuster.c
Einbinden in Java
|
public class Parallelport { static { System.load("/mnt-system/parallellib.so"); } public static native void sende(int zahl); public static native int lese(); }
Code 0-3: Quellcode von Parallelport.java
#include<jni.h> #include "parallel.h" #include<sys/io.h> JNIEXPORT void JNICALL Java_Parallelport_sende(JNIEnv *env, jclass clazz, jint zahl) { ioperm(0x378, 3, 1); outb(zahl,0x378); } JNIEXPORT jint JNICALL Java_Parallelport_lese(JNIEnv *env, jclass clazz) { ioperm(0x379, 1, 1); return inb(0x379); }
Code 0-4: Quellcode von parallel.c
|
public class Main { public static void main(String[] args) { int zahl; while(true) { zahl = Parallelport.lese(); Parallelport.sende(zahl); try { Thread.sleep(10); } catch(Exception e) { } } } }
Code 0-5: Quellcode zu Main.java
Übung - Hinarbeit auf einen PC/Linux/Java-geregelten Antrieb
|