I got it working. Slowly I am stripping down the original and bending it to my other functions.
It writes random bytes to a file and the screen. The bytes generated by this usb device appear to be more random than what is normally available on pc's. I haven't tested that personally yet.
Thanks for the help.
Attached is sample output.
Code is below.
Rem Project: _newser_serialcom
Rem Created: Wednesday, May 14, 2014
Rem Project: newser
Rem Created: 20/01/2005 22:06:50
Rem with help from VisualProg.
Rem I am in the process of stripping down the original program found at:
Rem http://forum.thegamecreators.com/?m=forum_view&t=87450&b=1
#CONSTANT GENERIC_READ = 0x80000000
#CONSTANT OPEN_EXISTING = 3
#CONSTANT FILE_ATTRIBUTE_NORMAL = 0x80
#CONSTANT NOPARITY = 0
#CONSTANT ONESTOPBIT = 0
#CONSTANT MAXDWORD = 4294967295
#CONSTANT SETXOFF = 1 ` Simulate XOFF received
#CONSTANT SETXON = 2 ` Simulate XON received
#CONSTANT SETRTS = 3 ` Set RTS high
#CONSTANT CLRRTS = 4 ` Set RTS low
#CONSTANT SETDTR = 5 ` Set DTR high
#CONSTANT CLRDTR = 6 ` Set DTR low
#CONSTANT RESETDEV = 7 ` Reset device if possible
#CONSTANT SETBREAK = 8 ` Set the device break line.
#CONSTANT CLRBREAK = 9 ` Clear the device break line.
#CONSTANT BUFFER_SIZE = 257
#CONSTANT KERNEL_DLL_ID = 1
#CONSTANT DCB_MEMBLOCK_ID = 1
#CONSTANT TIMEOUTS_MEMBLOCK_ID = 2
#CONSTANT IO_BUFFER_MEMBLOCK_ID = 3
#CONSTANT BYTES_READ_WRITE_ID = 4
Type T_DCB
DCBlength as dword
BaudRate as dword
fBinary as dword
fParity as dword
fOutxCtsFlow as dword
fOutcDsrFlow as dword
fDtrControl as dword
fDsrSensitivity as dword
fTXContinueONXoff as dword
fOutX as dword
fInX as dword
fErrorChar as dword
fNull as dword
fRtsControl as dword
fAbortOnError as dword
fDummy as dword
XonLim as word
XoffLim as word
ByteSize as byte
Parity as byte
StopBits as byte
XonChar as byte
XoffChar as byte
ErrorChar as byte
EofChar as byte
EvtChar as byte
wReservedl as word
endtype
type T_COMMTIMEOUTS
ReadIntervalTimeout as dword
ReadTotalTimeoutMultiplier as dword
ReadTotalTimeOutConstant as dword
endtype
`These Global variables will have to be inserted into you main source file
global hPort as dword
global DCB as T_DCB
global CommTimeouts as T_COMMTIMEOUTS
global Buffer as string
global Success as dword
// programming starts here
DISABLE ESCAPEKEY
Success = INITDISP(800,600)
print Success
Potential as INTEGER = 1280000
x as integer = 0
xx as string
ass as integer = 0
hh as string
DIM pw$(Potential+1) as string
pwcount as integer = 0
reset as INTEGER = 0
Initialize_Serial_Driver() ` Initialization Driver
hPort = CreateFile("COM8", GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) ` Initialize port
// print "Successfully open COM8"
Success = EscapeCommFunction(hPort,SETRTS) // SETRTS. but why?
Success = EscapeCommFunction(hPort,SETDTR) // SETDTR == open gate
// Read From Serial Port - big loop
print "Reading from Serial Port..."
if file exist ("test.txt")
delete file "test.txt"
endif
//The file must not exist or the command will fail.
open to write 1,"test.txt"
one = file open(1)
print one
//if one = 1
// Byebye("file closed")
//endif -- had a tough time making this work consistently
print "press anykey"
wait key
repeat
Success = ReadFile(hPort, BUFFER_SIZE - 1)
//is buffer empty?
if Buffer = ""
print
print "-";
Byebye("buffer empty")
ENDIF
//print "String :";Buffer
//check to see if a reset is needed == experimental so far
reset = 0
for x = 1 to 3
xx = MID$(Buffer,x)
if xx = "Z"
reset=reset+1
ENDIF
NEXT x
IF reset = 3
// RESETDEV = attempt
Success = EscapeCommFunction(hPort,RESETDEV)
Byebye("zzzs filled in buffer")
ENDIF
for x = 1 to BUFFER_SIZE - 1
xx = MID$(Buffer,x)
xxx = xxx +1
IF ASC(xx) > 32 AND ASC(xx) < 127 // if the output is a character we want...
pwcount= pwcount +1
ass = ASC(xx)
hh = HEX$(ass)
pw$(pwcount) = xx
print xxx;" "; xx ;" ";" asc: "; ass ; " hex: "; hh ; " ";pwcount
write byte 1,ass
IF pwcount = Potential
FOR Y = 1 TO Potential
PRINT pw$(y);
next y
print
Byebye("filled!")
ENDIF
ENDIF
next x
print
until controlkey()
//leave prog
print "leaving VARCON_SERCOM press anykey"
wait key
Success = CloseHandle(hPort) ` Close the COM port
print "Done!"
close file 1
Cleanup_Memory()
wait 500
end
// Internal Functions
//
function INITDISP(x,y)
SET WINDOW POSITION 0,0
SET WINDOW LAYOUT 1,1,3
SET WINDOW TITLE "MDJ"
MAXIMIZE WINDOW
CLS RGB(0,0,0)
INK RGB(255,255,255), RGB(0,0,0)
SET BITMAP FORMAT 21
SET IMAGE COLORKEY 255,0,255
IF CHECK DISPLAY MODE(x,y,32) = 1
SET DISPLAY MODE x,y,32,1 // vsync on to prevent tearing
CLS RGB(0,0,0) // black
INK RGB(255,255,255), RGB(0,0,0)
fswitch = 1 // this function was successful
ENDIF
D3D_Init
D3D_Color 255,255,0,255
D3D_Font 1,"Arial",13,0,0,1
D3D_Font 2,"Arial",11,0,0,1
ENDFUNCTION fswitch
function byebye(Answer AS STRING)
print " **********"
print Answer
CloseHandle(hPort)
Cleanup_Memory()
print "press anykey"
wait key
close file 1
end
ENDFUNCTION
function Initialize_Serial_Driver()
load dll "c:\windows\system32\kernel32.dll", KERNEL_DLL_ID
if dll exist(KERNEL_DLL_ID) = 0
byebye("Could not find kernel32.dll")
endif
if dll call exist(KERNEL_DLL_ID, "CreateFileA") = 0
byebye("Could not find CreateFile command in kernel32.dll")
endif
if dll call exist(KERNEL_DLL_ID, "GetCommState") = 0
byebye("Could not find GetCommState command in kernel32.dll")
endif
if dll call exist(KERNEL_DLL_ID, "SetCommState") = 0
byebye("Could not find SetCommState command in kernel32.dll")
endif
if dll call exist(KERNEL_DLL_ID, "GetCommTimeouts") = 0
byebye("Could not find GetCommTimeouts command in kernel32.dll")
endif
if dll call exist(KERNEL_DLL_ID, "SetCommTimeouts") = 0
byebye("Could not find SetCommTimeouts command in kernel32.dll")
endif
if dll call exist(KERNEL_DLL_ID, "ReadFile") = 0
byebye("Could not find ReadFile command in kernel32.dll")
endif
if dll call exist(KERNEL_DLL_ID, "CloseHandle") = 0
byebye("Could not find CloseHandle command in kernel32.dll")
endif
if dll call exist(KERNEL_DLL_ID, "EscapeCommFunction") = 0
byebye("Could not find EscapeCommFunction command in kernel32.dll")
endif
Make memblock DCB_MEMBLOCK_ID, 80 : ` For DCB
Make memblock TIMEOUTS_MEMBLOCK_ID, 20 : ` For TimeOuts
Make memblock IO_BUFFER_MEMBLOCK_ID, BUFFER_SIZE : `I/O Buffer
Make memblock BYTES_READ_WRITE_ID, 4 : `Bytes Written or Read
endfunction
function CreateFile(lpFileName as String, dwDesiredAccess as Dword, dwShareMode as dword, lpSecurityAttributes as dword, dwCreationDisposition as Dword, dwFlagsAndAttributes as dword, hTemplateFile as Dword)
ret = call dll (KERNEL_DLL_ID, "CreateFileA",lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile)
if ret = -1
byebye("Could not retrieve handle for COM port.(Is something else using the Port?)")
endif
endfunction ret
function GetCommState(nCid as dword)
ret = call dll (KERNEL_DLL_ID, "GetCommState", nCid, get memblock ptr(DCB_MEMBLOCK_ID))
if ret = 0
byebye("Could not get Com port state")
endif
DCB.DCBlength = memblock dword(DCB_MEMBLOCK_ID,0)
DCB.BaudRate = memblock dword(DCB_MEMBLOCK_ID,4)
DCB.fBinary = memblock dword(DCB_MEMBLOCK_ID,8)
DCB.fParity = memblock dword(DCB_MEMBLOCK_ID,12)
DCB.fOutxCtsFlow = memblock dword(DCB_MEMBLOCK_ID,16)
DCB.fOutcDsrFlow = memblock dword(DCB_MEMBLOCK_ID,20)
DCB.fDtrControl = memblock dword(DCB_MEMBLOCK_ID,24)
DCB.fDsrSensitivity = memblock dword(DCB_MEMBLOCK_ID,28)
DCB.fTXContinueONXoff = memblock dword(DCB_MEMBLOCK_ID,32)
DCB.fOutX = memblock dword(DCB_MEMBLOCK_ID,36)
DCB.fInX = memblock dword(DCB_MEMBLOCK_ID,40)
DCB.fErrorChar = memblock dword(DCB_MEMBLOCK_ID,44)
DCB.fNull = memblock dword(DCB_MEMBLOCK_ID,48)
DCB.fRtsControl = memblock dword(DCB_MEMBLOCK_ID,52)
DCB.fAbortOnError = memblock dword(DCB_MEMBLOCK_ID,56)
DCB.fDummy = memblock dword(DCB_MEMBLOCK_ID,60)
DCB.XonLim = memblock word(DCB_MEMBLOCK_ID,64)
DCB.XoffLim = memblock word(DCB_MEMBLOCK_ID,66)
DCB.ByteSize = memblock byte(DCB_MEMBLOCK_ID,68)
DCB.Parity = memblock byte(DCB_MEMBLOCK_ID,69)
DCB.StopBits = memblock byte(DCB_MEMBLOCK_ID,70)
DCB.XonChar = memblock byte(DCB_MEMBLOCK_ID,71)
DCB.XoffChar = memblock byte(DCB_MEMBLOCK_ID,72)
DCB.ErrorChar = memblock byte(DCB_MEMBLOCK_ID,73)
DCB.EofChar = memblock byte(DCB_MEMBLOCK_ID,74)
DCB.EvtChar = memblock byte(DCB_MEMBLOCK_ID,75)
DCB.wReservedl = memblock word(DCB_MEMBLOCK_ID,76)
endfunction ret
function SetCommState(nCid as dword)
Write memblock dword DCB_MEMBLOCK_ID,0,DCB.DCBlength
Write memblock dword DCB_MEMBLOCK_ID,4,DCB.BaudRate
Write memblock dword DCB_MEMBLOCK_ID,8,DCB.fBinary
Write memblock dword DCB_MEMBLOCK_ID,12,DCB.fParity
Write memblock dword DCB_MEMBLOCK_ID,16,DCB.fOutxCtsFlow
Write memblock dword DCB_MEMBLOCK_ID,20,DCB.fOutcDsrFlow
Write memblock dword DCB_MEMBLOCK_ID,24,DCB.fDtrControl
Write memblock dword DCB_MEMBLOCK_ID,28,DCB.fDsrSensitivity
Write memblock dword DCB_MEMBLOCK_ID,32,DCB.fTXContinueONXoff
Write memblock dword DCB_MEMBLOCK_ID,36,DCB.fOutX
Write memblock dword DCB_MEMBLOCK_ID,40,DCB.fInX
Write memblock dword DCB_MEMBLOCK_ID,44,DCB.fErrorChar
Write memblock dword DCB_MEMBLOCK_ID,48,DCB.fNull
Write memblock dword DCB_MEMBLOCK_ID,52,DCB.fRtsControl
Write memblock dword DCB_MEMBLOCK_ID,56,DCB.fAbortOnError
Write memblock dword DCB_MEMBLOCK_ID,60,DCB.fDummy
Write memblock word DCB_MEMBLOCK_ID,64,DCB.XonLim
Write memblock word DCB_MEMBLOCK_ID,66,DCB.XoffLim
Write memblock byte DCB_MEMBLOCK_ID,68,DCB.ByteSize
Write memblock byte DCB_MEMBLOCK_ID,69,DCB.Parity
Write memblock byte DCB_MEMBLOCK_ID,70,DCB.StopBits
Write memblock byte DCB_MEMBLOCK_ID,71,DCB.XonChar
Write memblock byte DCB_MEMBLOCK_ID,72,DCB.XoffChar
Write memblock byte DCB_MEMBLOCK_ID,73,DCB.ErrorChar
Write memblock byte DCB_MEMBLOCK_ID,74,DCB.EofChar
Write memblock byte DCB_MEMBLOCK_ID,75,DCB.EvtChar
Write memblock word DCB_MEMBLOCK_ID,76,DCB.wReservedl
ret = call dll (KERNEL_DLL_ID, "SetCommState", nCid, get memblock ptr(DCB_MEMBLOCK_ID))
if ret = 0
byebye("Could not set Com port state")
endif
endfunction ret
function GetCommTimeouts(nCid as dword)
ret = call dll (KERNEL_DLL_ID, "GetCommTimeouts", nCid, get memblock ptr(TIMEOUTS_MEMBLOCK_ID))
if ret = 0
byebye("Could not get Com Timeouts.")
endif
CommTimeouts.ReadIntervalTimeout = memblock dword(TIMEOUTS_MEMBLOCK_ID,0)
CommTimeouts.ReadTotalTimeoutMultiplier = memblock dword(TIMEOUTS_MEMBLOCK_ID,4)
CommTimeouts.ReadTotalTimeOutConstant = memblock dword(TIMEOUTS_MEMBLOCK_ID,8)
endfunction ret
function SetCommTimeouts(nCid as dword)
Write memblock dword TIMEOUTS_MEMBLOCK_ID,0,CommTimeouts.ReadIntervalTimeout
Write memblock dword TIMEOUTS_MEMBLOCK_ID,4,CommTimeouts.ReadTotalTimeoutMultiplier
Write memblock dword TIMEOUTS_MEMBLOCK_ID,8,CommTimeouts.ReadTotalTimeOutConstant
ret = call dll (KERNEL_DLL_ID, "SetCommTimeouts", nCid, get memblock ptr(TIMEOUTS_MEMBLOCK_ID))
if ret = 0
byebye("Could not set Com Timeouts.")
endif
endfunction ret
function ReadFile(hFile as dword, nNumberOfBytesToRead as dword)
ret = call dll(KERNEL_DLL_ID, "ReadFile", hFile, get memblock ptr(IO_BUFFER_MEMBLOCK_ID), nNumberOfBytesToRead, get memblock ptr(BYTES_READ_WRITE_ID),0)
if ret = 0
print "Could not read from com port."
wait key
endif
Buffer = ""
for i = 0 to memblock dword(BYTES_READ_WRITE_ID,0) - 1
Buffer = Buffer + chr$(memblock byte(IO_BUFFER_MEMBLOCK_ID,i))
next i
endfunction ret
function CloseHandle(hObject as dword)
ret = call dll(KERNEL_DLL_ID, "CloseHandle", hObject)
if ret = 0
byebye("Could not close handle.")
endif
endfunction ret
function EscapeCommFunction(hFile as dword, dwFunc as dword)
ret = call dll(KERNEL_DLL_ID, "EscapeCommFunction", hFile, dwFunc)
if ret = 0
byebye("EscapeCommFunction Failed.")
endif
endfunction ret
function Cleanup_Memory()
delete memblock DCB_MEMBLOCK_ID
delete memblock TIMEOUTS_MEMBLOCK_ID
delete memblock IO_BUFFER_MEMBLOCK_ID
delete memblock BYTES_READ_WRITE_ID
delete dll KERNEL_DLL_ID
endfunction