Platforms to show: All Mac Windows Linux Cross-Platform
/USB/HID/USB HID Test Mac
Feedback.
Function:
You find this example project in your Plugins Download as a Realbasic project file within the examples folder: /USB/HID/USB HID Test Mac
This example is the version from Thu, 3rd Feb 2010.
Notes: Last modified: Thu, 3rd Feb 2010
Feedback.
Function:
You find this example project in your Plugins Download as a Realbasic project file within the examples folder: /USB/HID/USB HID Test Mac
This example is the version from Thu, 3rd Feb 2010.
Notes: Last modified: Thu, 3rd Feb 2010
Class App
Inherits Application
// Properties
Protected Dim h As myhid
Dim quitting As boolean
// Event implementations
Sub Close()
app.quitting=true
End Sub
Sub Open()
if TargetMachO then
MsgBox "This example does not work unless you modify it for your device!"
else
MsgBox "This example requires a Mac OS X target."
end if
if not InitInstance then
quit
end if
End Sub
// Methods
Protected Function FindDevice() As MyHID
dim h as MyHID
dim p as string
h=new MyHID
if h.FindFirstDevice then
// compare here Product name, ProductID and VendorID
p=h.Product
if left(p,3)="xxx" then
Return h
end if
while h.FindNextDevice
p=h.Product
if left(p,3)="xxx" then
Return h
end if
wend
end if
End Function
Function InitInstance() As boolean
If OpenUSBDevice =false then
Return false
else
CheckFirmware
end if
Return true
End Function
Sub CheckFirmware()
dim s as string
dim m as MemoryBlock
dim firmwareversion as integer
// you can send data
h.Send "P"
// and read data
m=h.Read(16)
if m<>nil then
// for this example device, the device returns firmware version after you sent P command.
s=m.CString(0) // this device returns a C String in a 16byte package
'MsgBox "firmwareversion: "+s
firmwareversion=val(s)
if firmwareversion<10 then
MsgBox "Please reset the USB device!"
app.quitting=true
quit
Return
elseif firmwareversion<15 then
MsgBox "Firmware Version xxx below V1.5!"
app.quitting=true
quit
Return
end if
else
MsgBox "Failed to query Firmware version"
app.quitting=true
quit
end if
End Sub
Function OpenUSBDevice() As boolean
dim i as integer
h=FindDevice
if h=nil then
MsgBox "No xxx device found."
quit
Return false
end if
h.Connect
if h.Lasterror<>0 then
MsgBox "Failed to connect to xxx Device."
quit
Return false
end if
h.InstallCallback
Return true
End Function
End Class
Class MyHID
Inherits Object
// Properties
Dim datas(-1) As memoryBlock
Protected Dim EventRecord As memoryBlock
// Event implementations
Sub ReceivedData(data as string, size as integer)
dim m as MemoryBlock
dim s as string
dim i as integer
m=data
// debug output
for i=0 to 15
s=s+str(m.Byte(i))+" "
next
s=s+"= "+data
System.DebugLog s
// and store it for the read command
datas.Append m
End Sub
// Methods
Sub Constructor()
EventRecord=NewMemoryBlock(100)
TimeOut=2222
End Sub
Sub Send(data as string)
// this is a special send command for this device.
// For this device we need to package data into a 8 byte package.
dim m as MemoryBlock
dim n as integer
dim d,s as string
dim i as integer
if app.quitting then
quit
end if
// this device wants 8 byte per command
n=lenb(data)
if n>8 then
n=8
end if
m=NewMemoryBlock(8)
m.StringValue(0,n)=data
for i=0 to 7
s=s+str(m.Byte(i))+" "
next
d=m
s=s+"= "+d
System.DebugLog s
me.SendMessageMemory m,0,8
'System.DebugLog "SendMessageMemory lasterror: "+Format(me.Lasterror,"-0")
if me.Lasterror=-536854447 then
app.quitting=true
MsgBox "The USB Receiver failed with a timeout to send an answer. Please restart application and device."
quit
end if
End Sub
Function Read(count as integer) As memoryBlock
// This read command is to read syncronous.
// Normally you should handle data in the event and work asyncronous, but this is not always possible.
const DelayDontMPYield = 1
const DelayDontRBYield = 2
const DelayDontThreadYield = 4
const DelayDontQuickTimeYield = 8
const DelayDontWait = 16
const DelayDontWaitNextEvent = 32
const DelayDontSleep = 64
dim t as integer
dim m as MemoryBlock
t=Ticks+60
// quitting already?
if app.quitting then
quit
end if
while t>ticks
// delay some time
MyWait
if app.quitting then
quit
end if
// and if we got data we return it.
if UBound(datas)>=0 then
Return datas.Pop
end if
wend
Return nil // timeout
End Function
Protected Sub MyWait()
// Boolean WaitNextEvent(EventMask eventMask,EventRecord * theEvent,UInt32 sleep,RgnHandle mouseRgn)
declare function WaitNextEvent lib "Carbon" (eventmask as integer, EventRecord as Ptr, sleep as integer, mouse as integer) as Boolean
call WaitNextEvent(0,EventRecord,1,0)
End Sub
End Class
See also:
Links
MBS Filemaker Plugins - Pfarrgemeinde Ministranten Nickenich