Programs (.BAS), Tutorials and Libraries ($INCLUDE, DECLARE LIBRARY, ...) > Libraries

Direct Port Access

(1/2) > >>

Galleon:
Step 1: Download and install NTport
http://www.zealsoftstudio.com/ntport/ntportev.zip

Step 2: Download the following files and place them in your QB64 folder:
http://www.qb64.net/ntport/ntport.lib
http://www.qb64.net/ntport/ntport.h
http://www.qb64.net/ntport/ntport.dll

Step 3: Add this to your QB64 program

--- Code: ---DEFLNG A-Z
DECLARE LIBRARY "ntport"
' Returns a value indicating the last operation is successful or not.
    FUNCTION GetLastState (s$)
    ' Enable your application to read or write specific ports.
    SUB EnablePorts (BYVAL PortStart, BYVAL PortStop)
    ' Disable your application to read or write specific ports.
    SUB DisablePorts (BYVAL PortStart, BYVAL PortStop)
    ' Returns a value indicates whether the application is running under Windows NT/2000/XP/Server 2003 system.
    FUNCTION IsWinNT
    ' Returns a value indicates whether the application is running under 64-bit Windows system.
    FUNCTION IsWin64
    ' Returns a value from specific ports.
    FUNCTION Inpb ALIAS Inp (BYVAL PortNum)
    FUNCTION Inportb ALIAS Inport (BYVAL PortNum)
    FUNCTION Inpw (BYVAL PortNum)
    FUNCTION InportW (BYVAL PortNum)
    FUNCTION Inpd (BYVAL PortNum)
    FUNCTION InportD (BYVAL PortNum)
    ' Write a value to specific ports.
    SUB Outpb ALIAS Outp (BYVAL PortNum, BYVAL PortData)
    SUB Outportb ALIAS Outport (BYVAL PortNum, BYVAL PortData)
    SUB Outpw (BYVAL PortNum, BYVAL PortData)
    SUB OutportW (BYVAL PortNum, BYVAL PortData)
    SUB Outpd (BYVAL PortNum, BYVAL PortDData)
    SUB OutportD (BYVAL PortNum, BYVAL PortDData)
    ' Set the registration information.
    SUB LicenseInfo (sUserName$, BYVAL DdwKey)
    ' Returns the version of NTPort Library.
    FUNCTION GetNTPortVersion
    ' Set the setting of fast mode
    SUB SetFastMode (BYVAL bOption)
    ' Get the current setting of fast mode
    FUNCTION GetFastMode
    ' Get the base address of LPT port
    FUNCTION GetLPTPortAddress (BYVAL PortNo)
END DECLARE
CONST ERROR_NONE = 0
CONST ERROR_DRIVER = 2
CONST ERROR_SCM_CANT_CONNECT = 9998
--- End code ---

Step 4: Try this example...

--- Code: ---EnablePorts 1, 65535
PRINT GetNTPortVersion
DO
    PRINT Inpb(&H3DA);
    _LIMIT 30
LOOP

--- End code ---

Clippy:
Tried to reverse my LPT port, and it works with a small delay for the switch over.


--- Code: ---DEFLNG A-Z
DECLARE LIBRARY "ntport"
' Returns a value indicating the last operation is successful or not.
  FUNCTION GetLastState (s$)
  ' Enable your application to read or write specific ports.
  SUB EnablePorts (BYVAL PortStart, BYVAL PortStop)
  ' Disable your application to read or write specific ports.
  SUB DisablePorts (BYVAL PortStart, BYVAL PortStop)
  ' Returns a value indicates whether the application is running under Windows NT/2000/XP/Server 2003 system.
  FUNCTION IsWinNT
  ' Returns a value indicates whether the application is running under 64-bit Windows system.
  FUNCTION IsWin64
  ' Returns a value from specific ports.
  FUNCTION Inpb ALIAS Inp (BYVAL PortNum)
  FUNCTION Inportb ALIAS Inport (BYVAL PortNum)
  FUNCTION Inpw (BYVAL PortNum)
  FUNCTION InportW (BYVAL PortNum)
  FUNCTION Inpd (BYVAL PortNum)
  FUNCTION InportD (BYVAL PortNum)
  ' Write a value to specific ports.
  SUB Outpb ALIAS Outp (BYVAL PortNum, BYVAL PortData)
  SUB Outportb ALIAS Outport (BYVAL PortNum, BYVAL PortData)
  SUB Outpw (BYVAL PortNum, BYVAL PortData)
  SUB OutportW (BYVAL PortNum, BYVAL PortData)
  SUB Outpd (BYVAL PortNum, BYVAL PortDData)
  SUB OutportD (BYVAL PortNum, BYVAL PortDData)
  ' Set the registration information.
  SUB LicenseInfo (sUserName$, BYVAL DdwKey)
  ' Returns the version of NTPort Library.
  FUNCTION GetNTPortVersion
  ' Set the setting of fast mode
  SUB SetFastMode (BYVAL bOption)
  ' Get the current setting of fast mode
  FUNCTION GetFastMode
  ' Get the base address of LPT port
  FUNCTION GetLPTPortAddress (BYVAL PortNo)
END DECLARE
CONST ERROR_NONE = 0
CONST ERROR_DRIVER = 2
CONST ERROR_SCM_CANT_CONNECT = 9998



'PRINT "NTPort version:"; GetNTPortVersion

LPT = GetLPTPortAddress(1) 'LPT 1

PRINT "LPT base address: &H" + HEX$(LPT)

EnablePorts LPT, LPT + 2

Printer LPT
k$ = INPUT$(1)
CLS
OutportW LPT + 2, 32 'or 44 reverse port bit 5(32)
_DELAY 2
Printer LPT
k$ = INPUT$(1)
CLS
OutportW LPT + 2, 12

Printer LPT

SUB Printer (port)
PRINT "Base:"; Inpb(port)
PRINT "Status:"; Inpb(port + 1)
PRINT "Control:"; Inpb(port + 2)
PRINT GetLastState(s$)
END SUB

--- End code ---

What kind of string value does the GetLastState function need?

I can reverse the port using my VB program. it uses INPOUT32.DLL. Perhaps we could use that one for free.

Ted

Clippy:
I got this to work and it is SIMPLE AND FREE! You must have a parallel port for this code to work!


--- Code: ---DECLARE DYNAMIC LIBRARY "inpout32"
  FUNCTION Inp32% (BYVAL PortAddress AS INTEGER)
  SUB Out32 (BYVAL PortAddress AS INTEGER, BYVAL Value AS INTEGER)
END DECLARE

Printer
_DELAY 2

Out32 &H37A, 32
_DELAY 2
Printer

_DELAY 2

Out32 890, 12 'control port is normally set to 12
Out32 888, 227 'any value from 0 to 255

Printer

SUB Printer
basedata% = Inp32%(888)
PRINT "Data:"; basedata%; CHR$(basedata%) 'read or write
PRINT "Status:"; Inp32%(889) 'read only!
PRINT "Control:"; Inp32%(890) 'read or write 32 or over reverses port
PRINT
END SUB

--- End code ---


Download this DLL and put it in the QB64 folder:

http://dl.dropbox.com/u/8440706/inpout32.zip

Try it on a COM port and see what happens. You should have a delay when reading ports after a change has been made in some cases. The above code will reverse the port so that there is no data in the base port. That is used to accept data from a device.

Ted

Pete:
Two of you working on port? That's okay, I'll go off in the opposite direction, and take a bow! Oh well, I'll stop talking ship now.

Pete  ;D

 - Hey, besides the robotics people, what other uses will this port ability open, if the port opens, of course? I mean we already have TCP/IP, and we can use USB printers, right? So I'm not seeing the possibilities related to this addition.

Clippy:
Actually it started out as a need to read a COM port using INP and OUT for a program that reads the port registers.

The LPT port is easier to read if you have one. COM port registers all return 255 when nothing is connected to them. LPT doesn't.

So we need a COM port user that has something connected to it.

Navigation

[0] Message Index

[#] Next page

Go to full version