close
Ubuntu 8.04, I/O control via LPT port
things happened are more than expected. Today, another issue if a replication for the gcc complier to work with the original C-code followings.
/*
* IO_LPT1.c: very simple example of port I/O
*
* This code does nothing useful,
* write 0xFF to LPT1, LED on, a pause, read, display
* write 0x00 to LPT1, LED off, a pause, read, display
* and a port read.
* how to compile : gcc -O2 -o IO_LPT1 IO_LPT1.c
* how to run : sudo ./IO_LPT1
*/
#include < stdio.h >
#include < unistd.h >
#include < /usr/src/linux-headers-2.6.24-17-generic/include/asm-x86/io.h >
#define BASEPORT 0x378 /* LPT1 */
int main()
{
/* Get access to the ports */
if (ioperm(BASEPORT, 3, 1)) {perror("ioperm"); exit(1);}
/* Set the data signals (D0-7) of the port to all high (1), LED on */
outb(0xff, BASEPORT);
/* Sleep for a while (1000000 us = 1 sec, LED on) */
usleep(1000000);
/* Read from the date port (BASE) and display the result */
printf("Printer port 0x378 = %d\n", inb(BASEPORT));
/* Set the data signals (D0-7) of the port to all low (0), LED off */
outb(0, BASEPORT);
/* Sleep for a while (1 sec) */
usleep(1000000);
/* Read from the date port (BASE) and display the result */
printf("Printer port 0x378 = %d\n", inb(BASEPORT));
/* Read from the status port (BASE+1) and display the result */
printf("status port: %d\n", inb(BASEPORT + 1));
/* We don't need the ports anymore */
if (ioperm(BASEPORT, 3, 0)) {perror("ioperm"); exit(1);}
exit(0);
}
/* end of IO_LPT1.c */
error messages received as something like the followings,
/usr/src/linux-headers-2.6.27-9-generic/include/asm-x86/io.h:45: 錯誤: expected 「;」, 「,」 or 「)」 before 「*」 token
/usr/src/linux-headers-2.6.27-9-generic/include/asm-x86/io.h:46: 錯誤: expected 「;」, 「,」 or 「)」 before 「*」 token
In file included from IO_LPT1.c:14:
/usr/src/linux-headers-2.6.27-9-generic/include/asm-x86/io.h:87: 錯誤: expected 「=」, 「,」, 「;」, 「asm」 or 「__attribute__」 before 「*」 token
/usr/src/linux-headers-2.6.27-9-generic/include/asm-x86/io.h:99: 錯誤: expected 「=」, 「,」, 「;」, 「asm」 or 「__attribute__」 before 「*」 token
IO_LPT1.c: In function 「main」:
IO_LPT1.c:22: 警告: 隱含宣告與內建函式 「exit」 不相容
IO_LPT1.c:48: 警告: 隱含宣告與內建函式 「exit」 不相容
IO_LPT1.c:50: 警告: 隱含宣告與內建函式 「exit」 不相容
problem again, ok... web search again
http://forums.opensuse.org/programming-scripting/392969-cannot-find-asm-io-h.html
the solution here,
omitt this
#include < /usr/src/linux-headers-2.6.24-17-generic/include/asm-x86/io.h >
include these,
#include < sys/io.h >
#include < stdlib.h >
result, compilation successful, no any warning messages.
ASUS S5A laptop (hard disk is removed)
Ubuntu 8.10
Transcend 4G USB FLASH THUMB for BOOT and RUN
for easiness and note, the complete code is following,
/*
* IO_LPT1.c: very simple example of port I/O
*
* This code does nothing useful,
* write 0xFF to LPT1, LED on, a pause, read, display
* write 0x00 to LPT1, LED off, a pause, read, display
* and a port read.
* how to compile : gcc -O2 -o IO_LPT1 IO_LPT1.c
* how to run : sudo ./IO_LPT1
*/
#include < stdio.h >
#include < unistd.h >
//#include < /usr/src/linux-headers-2.6.24-17-generic/include/asm-x86/io.h >
#include < sys/io.h >
#include < stdlib.h >
#define BASEPORT 0x378 /* LPT1 */
int main()
{
/* Get access to the ports */
if (ioperm(BASEPORT, 3, 1)) {perror("ioperm"); exit(1);}
/* Set the data signals (D0-7) of the port to all high (1), LED on */
outb(0xff, BASEPORT);
/* Sleep for a while (1000000 us = 1 sec, LED on) */
usleep(1000000);
/* Read from the date port (BASE) and display the result */
printf("Printer port 0x378 = %d\n", inb(BASEPORT));
/* Set the data signals (D0-7) of the port to all low (0), LED off */
outb(0, BASEPORT);
/* Sleep for a while (1 sec) */
usleep(1000000);
/* Read from the date port (BASE) and display the result */
printf("Printer port 0x378 = %d\n", inb(BASEPORT));
/* Read from the status port (BASE+1) and display the result */
printf("status port: %d\n", inb(BASEPORT + 1));
/* We don't need the ports anymore */
if (ioperm(BASEPORT, 3, 0)) {perror("ioperm"); exit(1);}
exit(0);
}
/* end of IO_LPT1.c */
全站熱搜
留言列表