Hardware I/O now for Windows AND Linux

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Hardware I/O now for Windows AND Linux

Post by infratec »

Hi together,

since no one gave me a reply, I thought that no one knows the solution.
Here is it.

A small library (for windows a wrapper) for hardware I/O.
As written, the same commands works in Windows and in Linux.
(Sorry, I don't know MACs)

The only disadvantage for Linux:

You need root rights for I/O access.
So compile the program and run it as root or use sudo.

Here is the code:

save the code as InpOut.pbi

Code: Select all

;
; Wrapper for the well known inpout32.dll
;
; http://logix4u.net
;
; and an implementation for Linux
;


Global InpOutLib = #False

Global *Inp
Global *Out



Procedure InitInpOut()

 Result = #False

CompilerIf #PB_Compiler_OS = #PB_OS_Linux

 InpOutLib = OpenLibrary(#PB_Any, "libc.so.6")
 If InpOutLib
  *iopl = GetFunction(InpOutLib, "iopl")
  Result = CallCFunctionFast(*iopl, 3)
  If Result = 0 : Result = #True : EndIf
 EndIf
 
CompilerElse

 InpOutLib = OpenLibrary(#PB_Any, "inpout32.dll")
 
 If InpOutLib
  *Inp = GetFunction(InpOutLib, "Inp32")
  *Out = GetFunction(InpOutLib, "Out32")
  Result = #True
 EndIf
 
CompilerEndIf
 
 ProcedureReturn Result
EndProcedure


Procedure CloseInpOut()
 If InpOutLib : CloseLibrary(InpOutLib) : EndIf
EndProcedure


Procedure Inp(Port.w)
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
 EnableASM
 mov dx, Port
 in ax, dx
 DisableASM
 ProcedureReturn
CompilerElse
 ProcedureReturn CallFunctionFast(*Inp, Port)
CompilerEndIf
EndProcedure


Procedure Out(Port.w, Value.w)
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
 EnableASM
  mov dx, Port
  mov ax, Value
  out dx, ax
 DisableASM
CompilerElse
 CallFunctionFast(*Out, Port, Value)
CompilerEndIf
EndProcedure

Here a small test:

Code: Select all

;
; A small test for InpOut32.pbi
;
; it simply switches all databits of LPT1 off and on again
; and between it reads the values back (depending of the mode of the LPT,
; this is only the value of the register and not the value of the port pin)
;
; For a fast test put a LED between pin 9 (D7) and pin 21 or 22 (GND)
; the pins direct below pin 9
; Best result, when you use a 2mA LED (low current LED)
;
; With a standard LPT, you have 12 outputs and 5 inputs:
;
; Baseaddress     : 8 outputs (D0...D7)        pin 2...9
;
; Baseaddress + 2 : 5 outputs D0 - STROBE      pin 1
;                             D1 - AUTO        pin 14
;                             D2 - INIT        pin 16
;                             D3 - SELECT IN   pin 17
;
; Baseaddress + 1 : 4 inputs  D3 - ERROR       pin 15
;                             D4 - SELECT      pin 13
;                             D5 - PAPEREMPTY  pin 12
;                             D6 - ACKNOWLEDGE pin 10
;                             D7 - BUSY        pin 11
;
;                                  GND         pin 18...25
;
; Baseaddress : LPT1 = $378, LPT2 = $278, LPT1 = $3BC (old Hercules grafic cards :) )
;


IncludeFile "InpOut.pbi"

If OpenConsole()

 If InitInpOut()
 
  StartTime = ElapsedMilliseconds()
  BlinkTime = StartTime

  For i = 0 To 100000
  
   Out($378, $00)
   If Inp($379) & $10 : PrintN("Off") : Delay(500) : EndIf
 
   Out($378, $FF) 
   If Inp($379) & $10 : PrintN("On") : Delay(500) : EndIf
  
  Next i

  EndTime = ElapsedMilliseconds()

  PrintN ("Time: " + Str(EndTime - StartTime))

  CloseInpOut()
 
 Else

  PrintN("Init of the IO library failed")

 EndIf
 
EndIf
 
CloseConsole()
Simply plug a LED from a databit to GND and see it blinking.
If you connect SELECT (latest pin in the top row of the connector)
to GND, than you can meassure the time for the loop.

Best regards,

Bernd
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Stop, please, and append these ones here:
http://www.purebasic.fr/english/viewtopic.php?t=25792
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Post by infratec »

Hi Psychophanta,

it makes no sense to append it to the old topic,
since I can not change the subject of the old topic.

The main improvement of my solution is, that it works also with Linux.
Which is noted in the subject of this topic.

It is not only a speed improvement (25% with CallFunctionFast) of the subject in the other topic.

The Linux users may not read the other topic, since it is only 'XP friendly'.

Best regards,

Bernd
Post Reply