Link to home
Start Free TrialLog in
Avatar of smegghead
smeggheadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

port output in VB

Is it possible, using VB, to output a byte to a port address.

We've got a piece of hardware which controls a relay, this hardware sits on base address &h0300 - All we want to do is the equivalent of.

OUT 0300,255

or

OUT 0300,0

Is there an API function call I can call from within VB ??
Avatar of pjknibbs
pjknibbs

Unfortunately the Win32 API is designed to be machine-independent, which means there's no API which does what you want. In addition, accessing hardware directly like this isn't allowed under Windows NT, although you might get away with it for a while under Win9x.

I think you'd have to write your own Windows driver for the hardware in question, and that's definitely outside the realm of Visual BASIC!
ASKER CERTIFIED SOLUTION
Avatar of PBuck
PBuck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
smegghead,

If you can get your hands on a copy of Microsoft's GWBasic, you can use it to do what you want...  It has the OUT command, and it works just like you want.


Cheers!
Use the _outp command from a c DLL:

//~~~C DLL~~~
#include <windows.h>
#include <conio.h>

int CALLBACK LibMain (HANDLE hInstance, WORD wDataSeg,
                                WORD wHeapSize, LPSTR lpszCmdLine)
{
      return 1;
}

void __stdcall PortOut(unsigned short cPort, char cValue)
{
      _outp(cPort, cValue);
}

'~~~~Call it from VB~~~~
Option Explicit

Private Declare Sub PortOut Lib "dlltest" _
    (ByVal cPort As Integer, ByVal cValue As Byte)

Private Sub Command1_Click()
    Call PortOut(300, 128)
End Sub
Make that:

Call PortOut(&H300, 128)
Using the API 'CreateFile' it can be done!
Still researching.....

D'Mzzl!
RoverM
Avatar of smegghead

ASKER

Ok,

I'm SURE I added a comment here this morning.... I must have closed the window without sumbitting..

Basically, I asked if the 'c' function will work under WinNT. I tried yesterday with some delphi code which includede assembly language to output to the port.

I've also looked at the web-site PBuck suggested.. It looks like that probably has what I want, although I've not got it working yet !!

Smeg.
PS. The delphi code came up with an error saying 'priveleged instruction'.

I've also tried it using 'qbasic' is this the same as the suggested 'gwbasic' ?? With Qbasic, the command OUT &H0300,&HFF doesn't error, but doesn't perform the desired effect either.
Accessing I/O ports from user mode under NT is not permitted, which is why you're getting "privileged instruction" errors. I suspect the QBasic OUT() instructions are being routed to a virtual machine and never getting anywhere near the *physical* ports. I don't think you can do what you want without going into kernel mode.
Hi,

Direct from their documentation:
NTPort Library is also an ideal replacement of old BASIC IN or OUT statement.

Try:
http://www.zealsoftstudio.com/ntport/

NTPort Library provides support for Windows 95/98 and Windows NT. Cost $ 30 USD

Hope this helps
ED
From:
http://msdn.microsoft.com/library/backgrnd/html/msdn_realtime.htm

Q. Does Windows NT support direct I/O and memory access?

A. In general, Windows NT does not allow an application direct access to I/O ports. If this is needed, a driver needs to be
written.
Hi,

I've used MMPort in the past to access I/O's directly.  This works under NT.  The site says that it's NT only but I think I may have had it running under 9x before...  Unfortunately it doesn't come with a VB Header file, but you could probably derive this from the VC++ header file.

It's available at

http://sunst50.einev.ch/references/mondada/mmp/
Also,

http://www.lvr.com/parport.htm

has many utilites for parallel port / I/O access.
Thanks PBuck, that did the trick.. I used something called DirectIO.

Thanks also to everyone else for their comments.
Super!  I am glad that you got it working.  Now I know who to go to when I need to use that control.  :-)
I'll expect a 100 pointer !!!