Under Linux to construct an application software, no matter which programming language is used, it may not be an easy thing if an amateur at the beginning. In contrast, under Microsoft Windows enviroment, there has more resources about the freeware or "no cost" software, they can be used to control the external devices through printer port or LPT1.

This is another article of described how to control LPT I/O under Windows XP with uses Excel VBA programming language (嚐試以 Excel VBA 及 Win32 的功能讀寫 LPT PORT),

revisit this article, this configuration could be used as a simple tool to discover other feasibility, for example, a project of computer control with PC + Windows XP, at least, no need other programming tools but Excel VBA is exception. However, this costly OS and spread sheet programm of Microsoft Office, they are not free, the payment has to be settled before it runs in your PC. The illegal copies without pay the full charge is not the subject in this scope.


This 2008, the Linux (Unix for PC) is still free for distrubtion, and almost, the build-in GCC complier is helpful to compile C program without extra cost. What else the need to acheive the same purpose if I/O control via LPT1, it is no longer the Windows XP this time but Unbuntu 8.04, it is just the evaluation platform of 3G connection & experiment for few days ago.

An initial reading came along from web serach,




Parallel port controlling in Linux
http://www.epanorama.net/circuits/parallel_output.html#input


 


Linux I/O port programming mini-HOWTO
http://www.faqs.org/docs/Linux-mini/IO-Port-Programming.html#s9


result of this reading, an early written for the purpose as it is stated, and the last is about a snippet of the C code for demo purpose, it has been quoted here as well for easy reference as following,


/*
* example.c: very simple example of port I/O
*
* This code does nothing useful, just a port write, a pause,
* and a port read. Compile with `gcc -O2 -o example example.c',
* and run as root with `./example'.
*/




#include < stdio.h >
#include < unistd.h >
#include < asm/io.h >


#define BASEPORT 0x378 /* lp1 */


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 low (0) */
outb(0, BASEPORT);

/* Sleep for a while (100 ms) */
usleep(100000);

/* Read from the status port (BASE+1) and display the result */
printf("status: %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 example.c */




This well documented C code, seems very promising to our purpose, but this could be too earlier to jump to conclusion.

Once deploy this code and used to compile, the error come to attention as those header files (stdio.h, unistd.h io.h) does not existed.

This may be the result of partial of GCC compiler installed as Ubuntu does, the another web search gave the hints, to get a software package calls build-essential, it may helps to solve this issue.


open a Terminal and commanding following:
sudo apt-get install build-essential

Few minutes later, this software package is ready and installed, however, this time, the another message for that, io.h is still missing from compilation.

Look for the web serach again if more hints, at least to know if this GCC compiler is able to run under this Ubuntu 8.04, because Linux has to be able to compile it's kernal on the machine itself. The following web page and code snippet, confirmed to be work in our Ubuntu machine.

http://www.edaboard.com/ftopic305744.html


/* HelloLinux.c */
#include < stdio.h >

int main(void)
{
  printf("Hello Linux!\n");
  return 0;
}



Compilation is done without error, and the program is able to run once issue the command in the bracket below,

[./a.out]

what to do with this command,
[./] = in this foler
[a.out] = the output file in executable generated by GCC compilation.


the result, a printing to screen as "Hello Linux". With this successful hello_linux programming, looking around somewhere the local hard drive, we found some io.h existed and resultant of the modified code snippet following of which works well to on-off an LED attached to LPT1. (ignored some warning about exit(), because it does not affects the code or our purpose so far)


/*
 * 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 */



 


Upon completion of compilation, it is time to witness what is the real feeling from computer comtrol. One LED (Light Emitting Diode) and one resistor could be used to visualize the effect of this computer control.


See the picture from the heading, twist one leg of LED and one leg of resistor together, the others should be insert to printer port directly for electrical connection of this experiment. Ensure the anode of LED (+) inserted into any pin hole from pin2 to pin9, these are output pins of D0 - D7 respectively. The other leg of resistor has to be connected to hole of pin25, pin24 & pin23 are ok becuase they are all connected to ground. Once this program is invoked, the LED will light about one second and then turn off. Repeat the program invoke, the LED will light another one second.


Do you have any idea to prolong the time for LED lighting? yes, the simple thing is to extend the delay time for LED on, remember, this delay time is 1000000 uS (Microsecond) now and it is almost one second, when change to 2000000, it will be about 2 seconds, of course, the program has to be compiled again before this effect is visible. Once this visible LED lighting & time can be changed by your programming, do you be able to see what is real for COMPUTER I/O CONTROL, it is so simple, but please, do not send me question about the stuff of 電腦減肥 or 電腦塑身, we are completely lost with that terminology about COMPUTER or 電腦 if it could be helpful for body shaping or body weight lost, perhaps, advertisement is trickery, but superstitious behaviour is another fact that of no knowing computer or computer control.


Conclusion -
under Linux, no initial cost for C compiler to generate application program, no cost for OS, no cost for I/O port access (remember the LPT port access with our Windows XP, it has to bypass ring-3 with DLL to gain access to privilege I/O control)

了解網量

創作者介紹
創作者 xiaolabaDIY 的部落格 的頭像
xiaolabaDIY

xiaolabaDIY 的部落格

xiaolabaDIY 發表在 痞客邦 留言(0) 人氣( 44 )