|
Post by alwichita on Jan 17, 2022 18:54:46 GMT -5
Hi guys, Has anyone tried using a TrueRNG3 device as a True Random Number Generator? If so can you supply a simple code example in how to read the Com port ? Thanks Al
|
|
|
Post by Rod on Jan 18, 2022 3:03:41 GMT -5
Info was a bit hard to find. But it appears to present as a standard com port. It streams random 8 bit numbers and is controlled by DTR pin. So you should be able to use any example posted that reads the serial port. You will need to read Accessing the Serial port on the LBPE to see how to toggle the DTR port. It may just start as soon as you open the port so you might not need DTR, experiment your can’t break anything. One thing you will need to watch out for is to handle any numbers you capture as binary. Because it is 0-255 the Windows control characters are in the stream, characters below asc 32 will play havoc with displaying the numbers in a text box for example. Look at the numbers by printing the asc() value rather than printing the character itself. alycesrestaurant.com/lbpe/AccessingSerialPort.htmlSo plug it in, find the port number with Windows device manager, open it with the standard Liberty open com command line and immediately start reading your number stream. Here is some basic code that might get you started, use the correct com port number. open "com2:9600,n,8,1,ds0,cs0,rs" for random as #commHandle timer 100, [bufferread] wait [bufferread] If lof(#commHandle) >= 0 then dataRead$ = input$(#commHandle, lof(#comHandle)) print dataRead$ end if wait DataRead$ will contain a set of random numbers, see how many you get every 100ms then fine tune how you suck in the numbers either by time or time and a set number of characters.
|
|
|
Post by alwichita on Jan 18, 2022 11:45:40 GMT -5
Thank You Rod ! I'll give it a try...
|
|
|
Post by alwichita on Jan 19, 2022 14:17:07 GMT -5
It resides on Com port 4 and I used the baud rate that the terminal emulator used (although it say that doesnt matter as it outputs random bytes as fast as it can )
open "com4:57600,n,8,1,ds0,cs0" for random as #commHandle
calldll #kernel32,"Sleep",1000 as ulong,r as void
CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _SETDTR as long,_
result as long
'calldll #kernel32,"Sleep",1000 as ulong,r as void
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
timer 100, [bufferread]
wait
[bufferread]
If lof(#commHandle) >= 0 then
dataRead$ = input$(#commHandle, lof(#commHandle))
print dataRead$
end if
wait
close #commHandle
|
|
|
Post by Rod on Jan 19, 2022 15:03:26 GMT -5
Is that success or failure? Try it without caring what DTR is. Use one timer, sleep or timer but not both. If it works the number of characters you get probably is driven by the baud rate.
|
|
|
Post by alwichita on Jan 19, 2022 20:43:21 GMT -5
It was failure... I tried it without DTR on and it doesn't work, The device instructions state that it requires DTR on, but I cant figure out how to do that. alycesrestaurant.com/lbpe/AccessingSerialPort.html states how to do it but that's the part that I don't get, I need to make an API call...
I copied Alyces example and combined it with your code but it still doesn't work, and I don't know if DTR is actually set ?
There are no errors, but dataRead$ is null character(s) and lof(#com) is always 0.
This is what I tried-
''''''''''''''''' 'Open the port briefly using an API call to determine the handle given by Windows 'We will use this handle later in Liberty, its the only way to get it. 'substitute your own port number lpFileName$ = "Com4" dwCreationDistribution = _OPEN_EXISTING hTemplateFile = _NULL calldll #kernel32, "CreateFileA", _ lpFileName$ as ptr, _ dwDesiredAccess as ulong, _ dwShareMode as ulong, _ lpSecurityAttributes as ulong, _ dwCreationDistribution as ulong, _ dwFlagsAndAttributes as ulong, _ hTemplateFile as ulong, _ hFileHandle as ulong
calldll #kernel32, "CloseHandle", _ hFileHandle as ulong, _ result as long
'hFileHandle now contains 'the #handle, a number, that identifies the port
'Create a struct to recieve the incoming handshaking data 'this data contains CTS DSR RI and RLSD info. 'For more detail go to 'http://msdn.microsoft.com/en-us/library/aa363258(VS.85).aspx 'struct modem,DSRCTS as long
'Now open the com port in Liberty and use the hFileHandle value in API calls open lpFileName$;":9600,n,8,1,ds0,cs0,rs" for random as #com
print "setting DTR" CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _SETDTR as long,_ result as long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' timer 300, [bufferread] wait
[bufferread]
If lof(#com) >= 0 then dataRead$ = input$(#com, lof(#com)) print dataRead$ end if
wait
calldll #kernel32, "CloseHandle", _ hFileHandle as ulong, _ result as long
close #commHandle
|
|
|
Post by tenochtitlanuk on Jan 20, 2022 7:14:44 GMT -5
I'll be interested in your progress. I nearly bought one of these purely for interest. When we found LB's rnd() was showing a systematic ( but tiny) bias I investigated all sorts of RNGs. Have you tried it with software like Teraterm/Minicom/w.h.y. to see the data stream? The DTR seems to just turn off/on the stream of bytes- best viewed in hex mode I'd guess. There's relevant stuff in reviews of the device at www.amazon.com/TrueRNG-V3-Hardware-Random-Generator/product-reviews/B01KR2JHTA?reviewerType=all_reviewsNot all the users seem to understand what it is doing.... PS Possibly relevant- I had an ADC that supplied serial data via USB- looking at the datastream via a terminal got me going with that.. See my page at www.diga.me.uk/serialADCreadLinux.html
|
|
|
Post by Rod on Jan 20, 2022 7:16:44 GMT -5
Well the code seems ok. I have tweaked a little. One thing to try is re setting DTR instead of setting DTR so rem out one set and un rem the other, test one then the other. If that still does not work use the debugger to step through the code a line at a time. Watch the hFileHandle variable, does it get filled with a handle? The other thing to play with is the baud rate, do they recommend a value?
These things usually ship with support software is that running the device ok?
Try this and play with the set/reset and baud rate parameters. It will stop itself if no data is produced.
''''''''''''''''' 'Open the port briefly using an API call to determine the handle given by Windows 'We will use this handle later in Liberty, its the only way to get it. 'substitute your own port number lpFileName$ = "Com4" dwCreationDistribution = _OPEN_EXISTING hTemplateFile = _NULL calldll #kernel32, "CreateFileA", _ lpFileName$ as ptr, _ dwDesiredAccess as ulong, _ dwShareMode as ulong, _ lpSecurityAttributes as ulong, _ dwCreationDistribution as ulong, _ dwFlagsAndAttributes as ulong, _ hTemplateFile as ulong, _ hFileHandle as ulong
print "Serial port handle is ";hFileHandle
calldll #kernel32, "CloseHandle", _ hFileHandle as ulong, _ result as long
'hFileHandle now contains 'the #handle, a number, that identifies the port
'Now open the com port in Liberty and use the hFileHandle value in API calls open lpFileName$;":9600,n,8,1,ds0,cs0,rs" for random as #com
'give the port some time to establish timer 250,[delay] wait
[delay] timer 0 print "Port opened in Liberty"
print "setting DTR" CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _SETDTR as long,_ result as long
'print "re-setting DTR" 'CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _CLRDTR as long,_ 'result as long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' timer 300, [bufferread] wait
[bufferread] count =count+1
If lof(#com) > 0 then dataRead$ = input$(#com, lof(#com)) print dataRead$ count =0 end if
if count >10 then timer 0 Print "No data!" Print "Closing com port" close #com end end if wait
|
|
|
Post by Rod on Jan 20, 2022 7:19:07 GMT -5
I see from the links posted that baud rate is 57600 and that RTS may be required as well. So just insert these switch on off code lines as required. I would try set DTR and set RTS first. Very few devices use these handshaking lines, which is why Liberty has no native support.
'Open the port briefly using an API call to determine the handle given by Windows 'We will use this handle later in Liberty, its the only way to get it. 'substitute your own port number lpFileName$ = "Com4" dwCreationDistribution = _OPEN_EXISTING hTemplateFile = _NULL calldll #kernel32, "CreateFileA", _ lpFileName$ as ptr, _ dwDesiredAccess as ulong, _ dwShareMode as ulong, _ lpSecurityAttributes as ulong, _ dwCreationDistribution as ulong, _ dwFlagsAndAttributes as ulong, _ hTemplateFile as ulong, _ hFileHandle as ulong
print "Serial port handle is ";hFileHandle
calldll #kernel32, "CloseHandle", _ hFileHandle as ulong, _ result as long
'hFileHandle now contains 'the #handle, a number, that identifies the port
'Now open the com port in Liberty and use the hFileHandle value in API calls open lpFileName$;":57600,n,8,1,ds0,cs0,rs" for random as #com
'give the port some time to establish timer 250,[delay] wait
[delay] timer 0 print "Port opened in Liberty"
print "setting DTR" CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _SETDTR as long,_ result as long
print "setting RTS" CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _SETRTS as long,_ result as long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' timer 300, [bufferread] wait
[bufferread] count =count+1
If lof(#com) >= 0 then dataRead$ = input$(#com, lof(#com)) print dataRead$ count =0 end if
if count >10 then timer 0 Print "No data!" Print "Closing com port" close #com end end if wait
|
|
|
Post by Rod on Jan 20, 2022 11:51:08 GMT -5
walt I keep saying the only way to find the windows handle for an open com port is to open it with API and then open it with Liberty which seems to reuse the com handle. Is there another way to get the handle of the open com port. So we open the com port with a standard Liberty open com command line. After that how do we discern the handle so we can use the DTR and RTS API calls. Or do we really need the open via API code.
|
|
|
Post by alwichita on Jan 23, 2022 14:53:54 GMT -5
Dang, I posted a detailed reply and I dont see it ! Does an admin need to review and approve?
|
|
|
Post by Brandon Parker on Jan 23, 2022 14:58:11 GMT -5
There should not be any approval required...
If you still have the exact same tab on the browser open, just keep hitting the back button and see if you can get back to the edit screen where the content was at. This works for me sometimes when I accidentally hit one of the side mouse buttons and the browser changes pages.
{:0)
Brandon Parker
|
|
|
Post by alwichita on Jan 23, 2022 16:04:19 GMT -5
Thanks Rod !
Well we have a little random success!
I sat down and ran this code and it started working !
It, however, doesn’t usually work. If I run the code 10 or 12 times it starts working ! Once working it runs forever, until I manually stop it !
Note: the port handle is different every time and its always different when it works or doesn’t work.
I did modify the code a little –
If lof(#com) >0 then ‘ FROM: >=0 TO: >0 dataRead$ = input$(#com, 1) ‘ FROM: input$(#com, lof(#com)) TO: input$(#com ,1) to read 1 ascii char otherwise it a long string of characters ! print hFileHandle, dataRead$, asc( dataRead$ ) ‘ prints asci value count =0 end if
I also commented out RTS.
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ Result when not working…
Serial port handle is 908 Port opened in Liberty setting DTR No data! Closing com port ‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
Result when working…
Serial port handle is 1020 Port opened in Liberty setting DTR
1020 = 61 1020 ë 235 1020 “ 147 1020 s 115 1020 29 1020 Á 193 1020 Í 205 1020 ™ 153 1020 18 1020 10 1020 ü 252 1020 10
Etc. ‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
Full code below –
'Open the port briefly using an API call to determine the handle given by Windows 'We will use this handle later in Liberty, its the only way to get it. 'substitute your own port number lpFileName$ = "Com4" dwCreationDistribution = _OPEN_EXISTING hTemplateFile = _NULL calldll #kernel32, "CreateFileA", _ lpFileName$ as ptr, _ dwDesiredAccess as ulong, _ dwShareMode as ulong, _ lpSecurityAttributes as ulong, _ dwCreationDistribution as ulong, _ dwFlagsAndAttributes as ulong, _ hTemplateFile as ulong, _ hFileHandle as ulong
print "Serial port handle is ";hFileHandle
calldll #kernel32, "CloseHandle", _ hFileHandle as ulong, _ result as long
'hFileHandle now contains 'the #handle, a number, that identifies the port
'Now open the com port in Liberty and use the hFileHandle value in API calls open lpFileName$;":57600,n,8,1,ds0,cs0,rs" for random as #com
'give the port some time to establish timer 250,[delay] wait
[delay] timer 0 print "Port opened in Liberty"
print "setting DTR" CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _SETDTR as long,_ result as long
'print "setting RTS" 'CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _SETRTS as long,_ 'result as long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' timer 300, [bufferread] wait
[bufferread] count =count+1
If lof(#com) >0 then dataRead$ = input$(#com, 1) print hFileHandle, dataRead$, asc( dataRead$ ) count =0 end if
if count >10 then timer 0 Print "No data!" Print "Closing com port" close #com end end if wait
|
|
|
Post by alwichita on Jan 23, 2022 16:09:47 GMT -5
Thanks Brandon ! I just recreated it !
|
|
|
Post by Rod on Jan 24, 2022 3:47:15 GMT -5
I suspect because we are testing that the port is being left open. That Is probably why it starts on a new port number. You will need to use the debugger and see exactly what makes it work, step through manually and watch the return values for each call, which work which fail and what timing makes it work. It is probably a timing issue about when the DTR is toggled. Is there any advice in the manual about controlling the flow?
|
|