Login    Register
Forum rules
NAS4Free Official SiteSet-Up GuideFAQsForum Rules
  • Author
    Message

LPT port as phisical in/out

Postby alexey123 » 09 Aug 2013 06:24

Current build work on motherboard or PCI connected LPT port

If your hardware have LPT port, you can use it for input or output electrical signals from/to NAS4Free server.
Lpt port pinout
Image

LPT Hardware address and mode configure on CMOS setting, I configure my LPT port address as 378.
We have:
Pins from 2 to 9 (DATA port) - output pins for default config. If we send data from NAS to port, we have 1 => 5 volt, 0 => 0V. Port have address 378
Pins 10, 11, 12,13, 15 (Status port) - input pins. In normal state, its have 5v potential and if pin shorted to GND, we have logical 1 into his register. Also pin 15 is inverted. Register have address 379
Pins 1, 14, 16, 17 (control port) - bidirectional pins. This pins have output as Open Collector , pulled up with resistor 4.7 kiloohm. Pins 1, 14, 17 are inverted for standart config, so if we send 1 into his register we have 0V as output voltage into his pin.

Sample Programm for control it on C
Code: Select all
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <machine/cpufunc.h>
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

char* DecBin(int chislo, int size)
{
    int i;
    char* bin = new char[size];
    for(i = size - 1; i >= 0; --i)
    {
        (bin[size-1-i] = ((chislo>>i)&1) + 48);
    }
    return bin;
}

int str2int(string str) {
  char* cc;
  int len;
  len=str.size();
  for (int i=0;i!=len;i++) {
    if (isdigit(str[i])) {
   cc[i]=str[i];  }
  }
  len=atoi(cc);
  return len;
}

int BinDec(string str) {
char* cc;
int len,c1,rez,l1;
rez=0;
l1=0;
c1=0;
len = str.size();
    for (int i=0;i!=len;i++) {   
   if (str[i]=='0') { c1=0;} else {c1=1;}
   l1=len-i-1;
   c1=c1*pow(2,l1);
   rez=rez+c1;
    }
  return rez;
}

int main(int argc, char* argv[])
{
    int pin, addr, part;
    pin=atoi(argv[1]);
    if ((pin<=9) && (pin>1)) { addr=0x378; part=1;}
    if ((pin==10) || (pin==11) || (pin==12) || (pin==13) || (pin==15)) {addr=0x379; part=2; }
    if ((pin==1) || (pin==14) || (pin==16) || (pin==17)) { addr=0x37A; part=3; }   
   
    int fd = open("/dev/io", O_RDWR);
    unsigned int x;   
    string r;   
    string b;
    bool pin15,pin13,pin12,pin11,pin10 = false;   
 
    if (fd==-1) {
   printf("not open lpt\n");
   exit(-1);
    }
    x=inb(addr);
    b=DecBin(x,8);

    switch(part) {
   case 1:
       if (argc==2) {
       if (pin==2) {if (b[7]=='1') {cout<<"on";} else {cout<<"off";}}
       if (pin==3) {if (b[6]=='1') {cout<<"on";} else {cout<<"off";}}      
       if (pin==4) {if (b[5]=='1') {cout<<"on";} else {cout<<"off";}}
       if (pin==5) {if (b[4]=='1') {cout<<"on";} else {cout<<"off";}}
       if (pin==6) {if (b[3]=='1') {cout<<"on";} else {cout<<"off";}}      
       if (pin==7) {if (b[2]=='1') {cout<<"on";} else {cout<<"off";}}             
       if (pin==8) {if (b[1]=='1') {cout<<"on";} else {cout<<"off";}}             
       if (pin==9) {if (b[0]=='1') {cout<<"on";} else {cout<<"off";}}             
       cout<<"\n";
       }
       if (argc==3) {
         if (pin==2) { if (strstr(argv[2],"on")) { b[7]='1'; outb(addr,BinDec(b));} else {b[7]='0'; outb(addr,BinDec(b));}}
         if (pin==3) { if (strstr(argv[2],"on")) { b[6]='1'; outb(addr,BinDec(b));} else {b[6]='0'; outb(addr,BinDec(b));}}      
         if (pin==4) { if (strstr(argv[2],"on")) { b[5]='1'; outb(addr,BinDec(b));} else {b[5]='0'; outb(addr,BinDec(b));}}        
         if (pin==5) { if (strstr(argv[2],"on")) { b[4]='1'; outb(addr,BinDec(b));} else {b[4]='0'; outb(addr,BinDec(b));}}        
         if (pin==6) { if (strstr(argv[2],"on")) { b[3]='1'; outb(addr,BinDec(b));} else {b[3]='0'; outb(addr,BinDec(b));}}        
         if (pin==7) { if (strstr(argv[2],"on")) { b[2]='1'; outb(addr,BinDec(b));} else {b[2]='0'; outb(addr,BinDec(b));}}        
         if (pin==8) { if (strstr(argv[2],"on")) { b[1]='1'; outb(addr,BinDec(b));} else {b[1]='0'; outb(addr,BinDec(b));}}        
         if (pin==9) { if (strstr(argv[2],"on")) { b[0]='1'; outb(addr,BinDec(b));} else {b[0]='0'; outb(addr,BinDec(b));}}        
       }
       break;      
   case 2:
       if (argc==2) {
       if (pin==15) {if (b[4]=='0') {cout<<"on";} else {cout<<"off";}}
       if (pin==13) {if (b[3]=='0') {cout<<"on";} else {cout<<"off";}}      
       if (pin==12) {if (b[2]=='0') {cout<<"on";} else {cout<<"off";}}
       if (pin==11) {if (b[0]=='1') {cout<<"on";} else {cout<<"off";}}
       if (pin==10) {if (b[1]=='0') {cout<<"on";} else {cout<<"off";}}
       cout<<"\n";
       }
       break;
   case 3:
       if (argc==2) {   
       if (pin==1) {if (b[7]=='0') {cout<<"on";} else {cout<<"off";}}      
       if (pin==17) {if (b[4]=='0') {cout<<"on";} else {cout<<"off";}}
       if (pin==14) {if (b[6]=='0') {cout<<"on";} else {cout<<"off";}}
       if (pin==16) {if (b[5]=='1') {cout<<"on";} else {cout<<"off";}}
       cout<<"\n";
       }
      
       if (argc==3) {
         if (pin==1) { if (strstr(argv[2],"on")) { b[7]='0'; outb(addr,BinDec(b));} else {b[7]='1'; outb(addr,BinDec(b));}}
         if (pin==14) { if (strstr(argv[2],"on")) { b[6]='0'; outb(addr,BinDec(b));} else {b[6]='1'; outb(addr,BinDec(b));}}
         if (pin==17) { if (strstr(argv[2],"on")) { b[4]='0'; outb(addr,BinDec(b));} else {b[4]='1'; outb(addr,BinDec(b));}}        
         if (pin==16) { if (strstr(argv[2],"on")) { b[5]='1'; outb(addr,BinDec(b));} else {b[5]='0'; outb(addr,BinDec(b));}}        
       }
        break;
   
    }
   
   
   close(fd);
   return 0;
}


I find this programm here ( Russian source ) Many thanks for autor

For compile C sources I use my jail proto. I create working folder /mnt/disk/app/thebrig/proto/usr/home/alexey/lptprog and copy into C source.
Simple under root user
Code: Select all
#CC pr1.c -o pr1

Image

When I compile, I have binary file, named pr1
I can run from NAS4Free main root ( lpt not accessable from jail!)
Code: Select all
./pr1 <Pin_Number>
- for test pin state. On state for output pin present if pin have 5 Volt, off state if pin have ) Volt. On state on input port present if pin shorted to GND

Code: Select all
./pr1 <Pin_Number> on/off
for send to output port Ov or 5V. Value on send 5Volt, off send 0 Volt.

Image

Programm can be run from any sh or php scripts.

Now I can connect to LPT port any device, such motion sensor , push button, 220 volt sensor for old UPS (will be continued), and many another input devices
As output I can on or off electrical tens, lamps, water-heater......
Example diagram input-output devices
Image

For triac power output please use scematic

I continue work on LPT port application and add LPT expander with P8255 popular chip. I plan build analog to digital converter on it.

I attach source file and binary for x86.

UPDATE
For light work with lpt I create webgui extension ( only for x86 CPU. If anybody need x64, just recompile source and rename file to pr23 )
Code: Select all
#CC pr1.c -o pr23

Image
Small pictures works as swiches for port DATA and port CONTROL. Push on it for change pin state.

Install procedure. My extension folder is /mnt/disk/app. Connect as root

Code: Select all
cd /mnt/disk/app
fetch https://sites.google.com/site/aganimkarmiel/home/free-time/file-sharing/lpt_ext.tar.gz
tar xvf lpt_ext.tar.gz
lpt/install.php


And add startup Postinit script
Code: Select all
/mnt/disk/app/lpt/install.php
You do not have the required permissions to view the files attached to this post.
Last edited by alexey123 on 12 Aug 2013 14:48, edited 5 times in total.
Lab 9.1.0.1 - Sandstorm (revision 847) / x86-embedded on HP D530 / 2G RAM / CF IDE as system
Home9.2.0.1 - Sandstorm (revision 943) / x64-embedded on SAPPHIRE Pure Mini E350 / 4G RAM
Moderator
alexey123

User avatar
 
Posts: 1298
Joined: 19 Aug 2012 06:22
Location: Israel

Re: LPT port as phisical in/out

Postby alexey123 » 09 Aug 2013 06:34

Reserved for expander
Image
Last edited by alexey123 on 09 Aug 2013 06:36, edited 1 time in total.
Lab 9.1.0.1 - Sandstorm (revision 847) / x86-embedded on HP D530 / 2G RAM / CF IDE as system
Home9.2.0.1 - Sandstorm (revision 943) / x64-embedded on SAPPHIRE Pure Mini E350 / 4G RAM
Moderator
alexey123

User avatar
 
Posts: 1298
Joined: 19 Aug 2012 06:22
Location: Israel

Re: LPT port as phisical in/out

Postby alexey123 » 09 Aug 2013 06:35

Reserved for old ups drive
Image
Lab 9.1.0.1 - Sandstorm (revision 847) / x86-embedded on HP D530 / 2G RAM / CF IDE as system
Home9.2.0.1 - Sandstorm (revision 943) / x64-embedded on SAPPHIRE Pure Mini E350 / 4G RAM
Moderator
alexey123

User avatar
 
Posts: 1298
Joined: 19 Aug 2012 06:22
Location: Israel

Return to Do It Yourself Projects

Who is online

Users browsing this forum: No registered users and 1 guest