Parallel Port #3

Blitz3D Forums/Blitz3D Beginners Area/Parallel Port #3

None(Posted 2004) [#1]
I am making progress with WinIo. I did the following:
1. loaded winio.dll, .sys, and .vdx into the userlibs folder;
2. created the .decls file (with just the declarations that I think I will use) and placed it in the userlibs folder;
3. created a .bb program.

Following is the .decls file then the .bb file. The program executes, but the value shown in the output window always scans 0. It seems that I am not properly capturing the DWORD (I do not know how to express a 4-byte DWORD in the parameters -- as I said, I am a newbie!). By the way, the I/O board that I am using does work with the VB example sent with WinIo package. More basic help, please?

(Thanks soja!)

**********************************************************

DECLARATION FILE

.lib "winio.dll"


;Initializes the WinIo library. Does not have to be used with Windowe XP.
InitializeWinIo():"InitializeWinIo"

;Performs shutdown of the WinIo library. Must be called before terminiating an application or when no longer using the WinIo library.
ShutdownWinIo():"ShutdownWinIo"

;Calls the WinIo driver. Parameters: (Points to a null-terminated string to specify the winio.sys driver path, Set to false).
InstallWinIoDriver(WinIoDriverPath$,IsDemandLoaded):"InstallWinIoDriver"

;Removes the WinIo driver.
RemoveWinIoDriver():"RemoveWinIoDriver"

;Reads a 1, 2, or 4 byte value from specifies I/O port. Parameters: (Input port $379, DWORD variable, BYTE = 1).
GetPortVal(PortAddr,PortVal,Size):"GetPortVal"

;Writes a 1, 2, or 4 byte value to an I/O port. Parameters: (Output port $378, Hex value written to port, BYTE = 1).
SetPortVal(PortAddr, PortVal, Size):"GetPortVal"


THE BLITZBASIC PROGRAM

;The BlitzBASIC code for:
;Direct Port access from BlitzBASIC using WinIo.

;Set graphics mode.
Graphics 800, 600, 16, 2
SetBuffer BackBuffer()

;Initializes the WinIo library.
;InitializeWinIo()

;Call the WinIo driver
InstallWinIoDriver(".\", False)

Repeat

;Read a 1 byte value from the specified port and place in variable DWORD.
GetPortVal($379, ?????, 1)

;Print variable
Print ?????

Until KeyDown(1)

;Removes the WinIo driver.
RemoveWinIoDriver()

;Shutsdown the WinIo library.
;ShutdownWinIo()

End


soja(Posted 2004) [#2]
I have some suggestions.

1) To be as correct as possible, specify the return types in the declarations. For instance, the docs say that InstallWinIoDriver returns a bool variable, so in your .decls file, you should state:
InstallWinIoDriver%(WinIoDriverPath$, IsDemandLoaded):"InstallWinIoDriver"
...instead of:
InstallWinIoDriver(WinIoDriverPath$, IsDemandLoaded):"InstallWinIoDriver"

(This will allow you to test the return value.)

2) You should test the return value for these functions. If it's false (fails), don't continue, or recover from the error.

3) A DWORD (doubleword) in C just signifies a two-word segments. A word is 16 bits (2 bytes), therefore a dword is 4 bytes, or to Blitz, the exact same thing as an int%. So anywhere where you see a DWORD, you know in Blitz it's just an int.

4) But if you look at the documentation for GetPortVal, you'll see that the second parameter (pdwPortVal) is a *pointer* to a dword. So in this case, we must declare it as a pointer in the .decls file (use *, not %) and then in the blitz program, we must pass in either (a) a bank, and then use peekbyte to read the data, or (b) a custom type instance. I prefer the latter, simply because it's more readable. Something like this:
Type PortVal
	Field val
End Type
p.PortVal = New PortVal
;...
GetPortVal($379, p, 4)
DebugLog p\val

'p' represents (to Blitz) a pointer to a block in 4 bytes in memory. But because we declared as part of the GetPortVal function in the .decls file "pdwPortVal*" instead of "pdwPortVal%", Blitz will pass an actual windows pointer of the 4-byte chunk of memory to the DLL. Then we read it simply with the "p\val" syntax.

I hope that makes sense. It kind of gets into more advanced stuff.

PS: Watch out; you've declared SetPortVal to call GetPortVal in the .decls file...

PPS: Use <code> and </code> or <quote> and </quote> (except replace <> with []) to format your special text and retain indentation.