News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Undocumented? Unstable? or Undiscovered?

Started by dexter, July 10, 2005, 10:52:54 AM

Previous topic - Next topic

dexter

I was looking for a way to access my GPS unit using the com ports and it was recommended I used the Windows API.  

I done a bit of research regarding the API but to be honest I've never used it before and initially it seems a bit daunting so I looked for free serial/rs232 dll's thinking all I would have to do was load the dll then call it.

I found a few but struggled to get any of them to work, I'm not sure if this was due to them being incompatible or I was calling them wrong, anyway after trying what seemed like the 20th dll I stated wishing I could just access the ports the way I use to way back when e.g.

Openfile "COM1",1

So out of shear desperation/frustration I tried;

ReadFile "COM4",1

And It Worked!!!  :o

The only problem is, it seems to think the end of a line is the end of the file  :blink:

So I used the following;


ReadFile "COM4",1
For lupe=0 To 50
If EndOfFile(1) <> 1
reed$=ReadString$(1)
Print reed$
EndIf
Sync
Next lupe
CloseFile 1
WaitKey
End


Can any tell me if this "Undocumented? Unstable? or Undiscovered?"

kevin


dexter

QuoteIt's Undocumented.

Why?  :blink:


I know not many people creating games need access to serial ports, but surely the more useful commands the better?

I had been holding back purchasing Play Basic until I could find a way to use serial ports.
Now I've worked out how, I'll be placing my order on Friday (when I get paid!)
That's assuming it is a stable way of accessing serial ports?

empty

It works because Windows treats files and COM ports pretty much the same. It is undocumented because Windows provides more functions for COM port access and configuration that are not natively supported by PlayBasic. However you can access the Windows API with PlayBasic and write your own routines to read from and write to the COM ports. I'll throw a little example together during this week.

dexter

Thanks for the reply and explaination.
I will keep a look out for your example thanks
As I said, I haven't got a clue when it comes the Windows API.

empty

QuoteAs I said, I haven't got a clue when it comes the Windows API.
Well the WinAPI is rather inconsistent, but at least MS provides a quite good documentation. :)

empty

#6
Here's an example of how to use the com ports. As I have no serial devices I wasn't able to test it, though. At least it didn't give me any errors. :)


; Load Dll
Global kernel32 = GetFreeDll()
LoadDll "kernel32.dll", kernel32

; Time-Out Struct
Type COMMTIMEOUTS
 ReadIntervalTimeout, ReadTotalTimeoutMultiplier
   ReadTotalTimeoutConstant, WriteTotalTimeoutMultiplier
   WriteTotalTimeoutConstant
EndType



Psub OpenCom(Comport$)
; Open a com port
Local hcom = CallDll(kernel32, "CreateFileA", Comport$, $C0000000, 0, 0, 3, $80, 0)

; set the default time out
Dim timeout As COMMTIMEOUTS
timeout.ReadIntervalTimeout = 0
timeout.ReadTotalTimeoutMultiplier = 0
timeout.ReadTotalTimeoutConstant = 1000
timeout.WriteTotalTimeoutMultiplier = 0
timeout.WriteTotalTimeoutConstant = 1000
CallDll(kernel32, "SetCommTimeouts", hcom, timeout.COMMTIMEOUTS)
EndPsub hcom

Psub ConfigureCom(Handle, ConfigString$)
; set the config string (baud rate, parity etc)
Local DCB = GetFreeBank()
CreateBank DCB, 28
CallDll(kernel32, "GetCommState", Handle, GetBankPtr(DCB))
CallDll(kernel32, "BuildCommDCBA", ConfigString$, GetBankPtr(DCB))
CallDll(kernel32, "SetCommState", Handle, GetBankPtr(DCB))
DeleteBank DCB
EndPsub

Psub ComBufferSize(Handle, InBuffer, OutBuffer)
; buffer size of the rx and tx data (256 bytes is common value)
CallDll(kernel32, "SetupComm", Handle, InBuffer, OutBuffer)
EndPsub

Psub ReadCom(Handle)
; read the string
Local result$ = Make$(" ", 256)
BytesRead = GetFreeBank()
CreateBank BytesRead, 4
CallDll(kernel32, "ReadFile", Handle, GetBankPtr(Buffer), 256, GetBankPtr(BytesRead), 0)
Local i = PeekBankInt(BytesRead, 0)
DeleteBank BytesRead
EndPsub result$, i

Psub CloseCom(Handle)
CallDll(kernel32, "CloseHandle", Handle)
EndPsub



; TEST

; Open the port
handle = OpenCom("COM2")
; Set the Buffer sizes
ComBufferSize(Handle, 256, 256)
; configure the port (modify the string to your needs)
ConfigureCom(Handle, "baud=19200 parity=n data=8 stop=1")
; read a string and the number of bytes that were actually read
; you can repeat that function call in a loop
s$, bytesread = ReadCom(handle)
; close the port
CloseCom(Handle)

Print "Result String       : " + s$
Print "Number of Bytes read: " + Str$(bytesread)
Sync
WaitKey

DeleteDll(kernel32)



kevin

Might as well make that an PB lib at some point also..

empty

Yup, that's what I was thinking too (if it works ;) )

dexter

Thanks alot empty for the example, unfortunatley I don't seem to be able to get it to work.  I changed the following lines;

handle = OpenCom("COM4")
.
.
.
.
ConfigureCom(Handle, "baud=4800 parity=n data=8 stop=1")

But no data is being received, do I need to change anything else?

As you've got no serial device (and that fact that you are doing all this for my benefit  :D)  I don't mind testing the code.

I am quite proficient at copying pasting F5ing  :lol:

kevin

QuoteI am quite proficient at copying pasting F5ing

Me too :)

empty

Actually that's all you should need to change.
Is there any technical data of your device available online?


QuoteI am quite proficient at copying pasting F5ing
Hehe, that's my favourite hobby too. :)

dexter

PS to Kevin,

I Purchased PlayBasic this morning (it's pay day :))

Thank you for creating it!

kevin

No worries, what are you working on anyway ?  Im just a little curious !

dexter

QuoteActually that's all you should need to change.
Is there any technical data of your device available online?

It's a BU-303 GPS Unit

I've found a link for the manual here;

http://www.prairie.mb.ca/bu303.htm

I don't know if this is any use?

(I didn't get mine from them or have anything to do with them)